Skip to content

Commit bc3fdb1

Browse files
committed
refactoring AlignedPrinter to accept string
Signed-off-by: Jian Jin Chen <jianjinchen59@gmail.com>
1 parent a495f02 commit bc3fdb1

9 files changed

Lines changed: 78 additions & 78 deletions

src/applications/bmqstoragetool/m_bmqstoragetool_cslprinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ void HumanReadableCslPrinter::printSummaryResult(
339339
d_ostream << '\n'
340340
<< recordCount.d_updateCount
341341
<< " update record(s) found, including:" << '\n';
342-
bsl::vector<const char*> fields(d_allocator_p);
342+
bsl::vector<bsl::string> fields(d_allocator_p);
343343
bmqp_ctrlmsg::ClusterMessageChoice clusterMessageChoice(
344344
d_allocator_p);
345345
for (CslUpdateChoiceMap::const_iterator it =
@@ -349,7 +349,7 @@ void HumanReadableCslPrinter::printSummaryResult(
349349
clusterMessageChoice.makeSelection(it->first);
350350
fields.push_back(clusterMessageChoice.selectionName());
351351
}
352-
bmqu::AlignedPrinter printer(d_ostream, &fields);
352+
bmqu::AlignedPrinter printer(d_ostream, fields);
353353
for (CslUpdateChoiceMap::const_iterator it =
354354
updateChoiceMap.begin();
355355
it != updateChoiceMap.end();

src/applications/bmqstoragetool/m_bmqstoragetool_cslrecordprinter.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
// BDE
3636
#include <bdlt_epochutil.h>
3737
#include <bsl_ostream.h>
38+
#include <bsl_string.h>
3839
#include <bsl_unordered_map.h>
3940
#include <bsl_vector.h>
4041
#include <bslma_allocator.h>
@@ -93,7 +94,7 @@ template <typename PRINTER_TYPE>
9394
class CslRecordPrinter {
9495
private:
9596
bsl::ostream& d_ostream;
96-
bsl::vector<const char*> d_fields;
97+
bsl::vector<bsl::string> d_fields;
9798
bslma::ManagedPtr<PRINTER_TYPE> d_printer_mp;
9899
bslma::Allocator* d_allocator_p;
99100

@@ -150,10 +151,10 @@ void CslRecordPrinter<PRINTER_TYPE>::printRecordDetails(
150151
d_fields.push_back("LeaderAdvisoryWords");
151152
d_fields.push_back("Timestamp");
152153
d_fields.push_back("Epoch");
153-
// It's ok to pass a vector by pointer and push elements after that as
154+
// It's ok to pass a vector by reference and push elements after that as
154155
// we've reserved it's capacity in advance. Hense, no reallocations will
155-
// happen and the pointer won't get invalidated.
156-
d_printer_mp.load(new (*d_allocator_p) PRINTER_TYPE(d_ostream, &d_fields),
156+
// happen and the reference remains valid.
157+
d_printer_mp.load(new (*d_allocator_p) PRINTER_TYPE(d_ostream, d_fields),
157158
d_allocator_p);
158159

159160
*d_printer_mp << header.recordType() << recId.offset() << recId.logId()
@@ -197,10 +198,10 @@ void CslRecordPrinter<PRINTER_TYPE>::printRecordsSummary(
197198
d_fields.push_back("CommitRecords");
198199
d_fields.push_back("AckRecords");
199200

200-
// It's ok to pass a vector by pointer and push elements after that as
201+
// It's ok to pass a vector by reference and push elements after that as
201202
// we've reserved it's capacity in advance. Hense, no reallocations will
202-
// happen and the pointer won't get invalidated.
203-
d_printer_mp.load(new (*d_allocator_p) PRINTER_TYPE(d_ostream, &d_fields),
203+
// happen and the reference remains valid.
204+
d_printer_mp.load(new (*d_allocator_p) PRINTER_TYPE(d_ostream, d_fields),
204205
d_allocator_p);
205206

206207
*d_printer_mp << recordCount.d_snapshotCount << recordCount.d_updateCount;

src/applications/bmqstoragetool/m_bmqstoragetool_printer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ void printDataFileMeta(bsl::ostream& ostream,
9393
{
9494
BSLS_ASSERT_SAFE(dataFile_p && dataFile_p->isValid());
9595

96-
const bsl::vector<const char*> fields = {"BlazingMQ File Header",
96+
const bsl::vector<bsl::string> fields = {"BlazingMQ File Header",
9797
"Data File Header"};
9898

99-
PRINTER_TYPE1 printer(ostream, &fields);
99+
PRINTER_TYPE1 printer(ostream, fields);
100100
{
101101
bmqu::MemOutStream s(allocator);
102102
s << '\n';
@@ -123,11 +123,11 @@ void printJournalFileMeta(bsl::ostream& ostream,
123123
{
124124
BSLS_ASSERT_SAFE(journalFile_p && journalFile_p->isValid());
125125

126-
const bsl::vector<const char*> fields = {"BlazingMQ File Header",
126+
const bsl::vector<bsl::string> fields = {"BlazingMQ File Header",
127127
"Journal File Header",
128128
"Journal SyncPoint"};
129129

130-
PRINTER_TYPE1 printer(ostream, &fields);
130+
PRINTER_TYPE1 printer(ostream, fields);
131131
{
132132
bmqu::MemOutStream s(allocator);
133133
s << '\n';
@@ -152,7 +152,7 @@ void printJournalFileMeta(bsl::ostream& ostream,
152152
s << '\n';
153153
{
154154
// Print journal-specific fields
155-
bsl::vector<const char*> fieldsSyncPoint(allocator);
155+
bsl::vector<bsl::string> fieldsSyncPoint(allocator);
156156
fieldsSyncPoint.reserve(12);
157157
fieldsSyncPoint.push_back("Last Valid Record Offset");
158158
fieldsSyncPoint.push_back("Record Type");
@@ -167,7 +167,7 @@ void printJournalFileMeta(bsl::ostream& ostream,
167167
fieldsSyncPoint.push_back("SyncPoint DataFileOffset (DWORDS)");
168168
fieldsSyncPoint.push_back("SyncPoint QlistFileOffset (WORDS)");
169169

170-
PRINTER_TYPE2 p(s, &fieldsSyncPoint);
170+
PRINTER_TYPE2 p(s, fieldsSyncPoint);
171171
bsls::Types::Uint64 lastRecPos =
172172
journalFile_p->lastRecordPosition();
173173
p << lastRecPos;
@@ -253,7 +253,7 @@ void printQueueDetails(bsl::ostream& ostream,
253253
const bsl::size_t appKeysCount = details.d_appDetailsMap.size();
254254

255255
// Setup fields to be displayed
256-
bsl::vector<const char*> fields(allocator);
256+
bsl::vector<bsl::string> fields(allocator);
257257
fields.reserve(8);
258258
fields.push_back("Queue Key");
259259
if (!details.d_queueUri.empty()) {
@@ -269,7 +269,7 @@ void printQueueDetails(bsl::ostream& ostream,
269269
fields.push_back("Num Delete Records");
270270

271271
{
272-
PRINTER_TYPE printer(ostream, &fields);
272+
PRINTER_TYPE printer(ostream, fields);
273273

274274
// Print Queue Key id: either Key or URI
275275
printer << queueKey;
@@ -576,13 +576,13 @@ void HumanReadablePrinter::printQueueOpSummary(
576576
d_ostream << "\nTotal number of queueOp records: "
577577
<< queueOpRecordsCount << '\n';
578578

579-
bsl::vector<const char*> fields(d_allocator_p);
579+
bsl::vector<bsl::string> fields(d_allocator_p);
580580
fields.reserve(4);
581581
fields.push_back("Number of 'purge' operations");
582582
fields.push_back("Number of 'creation' operations");
583583
fields.push_back("Number of 'deletion' operations");
584584
fields.push_back("Number of 'addition' operations");
585-
bmqu::AlignedPrinter printer(d_ostream, &fields);
585+
bmqu::AlignedPrinter printer(d_ostream, fields);
586586
printer << queueOpCountsVec[mqbs::QueueOpType::e_PURGE]
587587
<< queueOpCountsVec[mqbs::QueueOpType::e_CREATION]
588588
<< queueOpCountsVec[mqbs::QueueOpType::e_DELETION]
@@ -899,15 +899,15 @@ void JsonPrinter::printQueueOpSummary(
899899
{
900900
BSLS_ASSERT_SAFE(queueOpCountsVec.size() > mqbs::QueueOpType::e_ADDITION);
901901
closeBraceIfOpen();
902-
bsl::vector<const char*> fields(d_allocator_p);
902+
bsl::vector<bsl::string> fields(d_allocator_p);
903903
fields.reserve(5);
904904
fields.push_back("TotalQueueOperationsNumber");
905905
fields.push_back("PurgeOperationsNumber");
906906
fields.push_back("CreationOperationsNumber");
907907
fields.push_back("DeletionOperationsNumber");
908908
fields.push_back("AdditionOperationsNumber");
909909

910-
bmqu::JsonPrinter<true, false, 0, 2> printer(d_ostream, &fields);
910+
bmqu::JsonPrinter<true, false, 0, 2> printer(d_ostream, fields);
911911
printer << queueOpRecordsCount
912912
<< queueOpCountsVec[mqbs::QueueOpType::e_PURGE]
913913
<< queueOpCountsVec[mqbs::QueueOpType::e_CREATION]

src/applications/bmqstoragetool/m_bmqstoragetool_recordprinter.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
// BDE
4242
#include <bsl_ostream.h>
43+
#include <bsl_string.h>
4344
#include <bsl_utility.h>
4445
#include <bsl_vector.h>
4546
#include <bslma_allocator.h>
@@ -65,7 +66,7 @@ template <typename PRINTER_TYPE>
6566
class RecordDetailsPrinter {
6667
private:
6768
bsl::ostream& d_ostream;
68-
bsl::vector<const char*> d_fields;
69+
bsl::vector<bsl::string> d_fields;
6970
bslma::ManagedPtr<PRINTER_TYPE> d_printer_mp;
7071
bslma::Allocator* d_allocator_p;
7172

@@ -180,10 +181,10 @@ void RecordDetailsPrinter<PRINTER_TYPE>::printCommonHeader(
180181
d_fields.push_back("Timestamp");
181182
d_fields.push_back("Epoch");
182183

183-
// It's ok to pass a vector by pointer and push elements after that as
184+
// It's ok to pass a vector by reference and push elements after that as
184185
// we've reserved its capacity in advance. Hence, no reallocations will
185-
// happen and the pointer won't get invalidated.
186-
d_printer_mp.load(new (*d_allocator_p) PRINTER_TYPE(d_ostream, &d_fields),
186+
// happen and the reference remains valid.
187+
d_printer_mp.load(new (*d_allocator_p) PRINTER_TYPE(d_ostream, d_fields),
187188
d_allocator_p);
188189

189190
*d_printer_mp << details.d_record.header().type() << details.d_recordIndex

src/applications/bmqtool/m_bmqtool_storageinspector.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ void StorageInspector::processCommand(
464464

465465
// Print journal-specific fields
466466
BALL_LOG_OUTPUT_STREAM << "Journal SyncPoint:\n";
467-
bsl::vector<const char*> fields;
467+
bsl::vector<bsl::string> fields;
468468
fields.push_back("Last Valid Record Offset");
469469
fields.push_back("Record Type");
470470
fields.push_back("Record Timestamp");
@@ -478,7 +478,7 @@ void StorageInspector::processCommand(
478478
fields.push_back("SyncPoint DataFileOffset (DWORDS)");
479479
fields.push_back("SyncPoint QlistFileOffset (WORDS)");
480480

481-
bmqu::AlignedPrinter printer(BALL_LOG_OUTPUT_STREAM, &fields);
481+
bmqu::AlignedPrinter printer(BALL_LOG_OUTPUT_STREAM, fields);
482482
bsls::Types::Uint64 lastRecPos =
483483
d_journalFileIter.lastRecordPosition();
484484
printer << lastRecPos;
@@ -577,12 +577,12 @@ void StorageInspector::processCommand(
577577
BALL_LOG_OUTPUT_STREAM << "Queue #" << qnum << "\n";
578578
const QueueRecord& qr = cit->second;
579579

580-
bsl::vector<const char*> fields;
580+
bsl::vector<bsl::string> fields;
581581
fields.push_back("Queue URI");
582582
fields.push_back("QueueKey");
583583
fields.push_back("Number of AppIds");
584584

585-
bmqu::AlignedPrinter printer(BALL_LOG_OUTPUT_STREAM, &fields);
585+
bmqu::AlignedPrinter printer(BALL_LOG_OUTPUT_STREAM, fields);
586586
printer << cit->first << qr.d_queueKey << qr.d_appIds.size();
587587

588588
// 'printer' not to be used beyond this point
@@ -591,12 +591,12 @@ void StorageInspector::processCommand(
591591
for (unsigned int i = 0; i < appRecs.size(); ++i) {
592592
const AppIdRecord& ar = appRecs[i];
593593
BALL_LOG_OUTPUT_STREAM << " AppId #" << i + 1 << "\n";
594-
bsl::vector<const char*> f;
594+
bsl::vector<bsl::string> f;
595595
f.push_back("AppId");
596596
f.push_back("AppKey");
597597

598598
const int indent = 8;
599-
bmqu::AlignedPrinter p(BALL_LOG_OUTPUT_STREAM, &f, indent);
599+
bmqu::AlignedPrinter p(BALL_LOG_OUTPUT_STREAM, f, indent);
600600
p << ar.d_appId << ar.d_appKey;
601601
}
602602

src/groups/bmq/bmqu/bmqu_alignedprinter.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
///-----
2929
// First, specify field names for printer:
3030
//..
31-
// bsl::vector<const char*> fields;
31+
// bsl::vector<bsl::string> fields;
3232
// fields.push_back("Queue URI");
3333
// fields.push_back("QueueKey");
3434
// fields.push_back("Number of AppIds");
@@ -38,7 +38,7 @@
3838
//..
3939
// bsl::stringstream output;
4040
// const int indent = 8;
41-
// bmqu::AlignedPrinter printer(output, &fields, indent);
41+
// bmqu::AlignedPrinter printer(output, fields, indent);
4242
//..
4343
//
4444
// Last, print field values accordingly:
@@ -51,7 +51,6 @@
5151
//
5252

5353
// BDE
54-
#include <bsl_cstring.h>
5554
#include <bsl_iomanip.h>
5655
#include <bsl_ostream.h>
5756
#include <bsl_string.h>
@@ -70,7 +69,7 @@ class AlignedPrinter {
7069
private:
7170
// DATA
7271
bsl::ostream& d_ostream;
73-
const bsl::vector<const char*>* d_fields_p;
72+
const bsl::vector<bsl::string>& d_fields;
7473
int d_indent;
7574
int d_width;
7675
unsigned int d_counter;
@@ -88,7 +87,7 @@ class AlignedPrinter {
8887
/// is undefined unless `indent` >= 0 and at least one field is present
8988
/// in the `fields`.
9089
AlignedPrinter(bsl::ostream& stream,
91-
const bsl::vector<const char*>* fields,
90+
const bsl::vector<bsl::string>& fields,
9291
int indent = 4);
9392

9493
// MANIPULATORS
@@ -109,20 +108,20 @@ class AlignedPrinter {
109108
// --------------
110109

111110
inline AlignedPrinter::AlignedPrinter(bsl::ostream& stream,
112-
const bsl::vector<const char*>* fields,
111+
const bsl::vector<bsl::string>& fields,
113112
int indent)
114113
: d_ostream(stream)
115-
, d_fields_p(fields)
114+
, d_fields(fields)
116115
, d_indent(indent)
117116
, d_width(0)
118117
, d_counter(0)
119118
{
120119
BSLS_ASSERT_SAFE(0 <= d_indent);
121-
BSLS_ASSERT_SAFE(0 < d_fields_p->size());
120+
BSLS_ASSERT_SAFE(0 < d_fields.size());
122121

123-
int maxLen = static_cast<int>(bsl::strlen((*d_fields_p)[0]));
124-
for (unsigned int i = 1; i < d_fields_p->size(); ++i) {
125-
int len = static_cast<int>(bsl::strlen((*d_fields_p)[i]));
122+
int maxLen = static_cast<int>(d_fields[0].length());
123+
for (unsigned int i = 1; i < d_fields.size(); ++i) {
124+
int len = static_cast<int>(d_fields[i].length());
126125
if (maxLen < len) {
127126
maxLen = len;
128127
}
@@ -134,11 +133,11 @@ inline AlignedPrinter::AlignedPrinter(bsl::ostream& stream,
134133
template <typename TYPE>
135134
inline AlignedPrinter& AlignedPrinter::operator<<(const TYPE& value)
136135
{
137-
BSLS_ASSERT_SAFE(d_counter < d_fields_p->size());
136+
BSLS_ASSERT_SAFE(d_counter < d_fields.size());
138137

139-
d_ostream << bsl::setw(d_indent) << ' ' << (*d_fields_p)[d_counter]
138+
d_ostream << bsl::setw(d_indent) << ' ' << d_fields[d_counter]
140139
<< bsl::setw(static_cast<int>(
141-
d_width - bsl::strlen((*d_fields_p)[d_counter])))
140+
d_width - d_fields[d_counter].length()))
142141
<< ": " << value << '\n';
143142

144143
++d_counter;

0 commit comments

Comments
 (0)