Skip to content

Commit 5f965d1

Browse files
committedApr 19, 2024
FibblerCollection::toString()
Prints the entire collection, good for code reuse.
1 parent 7ba2981 commit 5f965d1

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed
 

‎core.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ FlibberCollection::flibber()
3232
myFlibbers.push_back(makeFlibber());
3333
}
3434

35+
std::string
36+
FlibberCollection::toString(uint8_t tabs) const
37+
{
38+
if (myFlibbers.size() == 0)
39+
return {};
40+
std::string tab;
41+
for (uint8_t i = 0; i < tabs; ++i)
42+
tab += '\t';
43+
44+
std::string res = tab + "flibbers:\n";
45+
for (const Flibber& fli : myFlibbers)
46+
res += tab + '\t' + fli.myKey + ": " + fli.myValue + '\n';
47+
return res;
48+
}
49+
3550
Flibber
3651
makeFlibber()
3752
{

‎core.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct Flibber
1212
struct FlibberCollection
1313
{
1414
void flibber();
15+
std::string toString(uint8_t tabs) const;
1516

1617
std::vector<Flibber> myFlibbers;
1718
};

‎moduleBar.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ ObjectBar::toString(uint8_t tabs) const
2525
std::string res;
2626
res += tab + "splinx: " + mySplinx + '\n';
2727
res += tab + "yibble: " + std::to_string(myYibble) + '\n';
28-
if (myFlibbers.size() > 0)
29-
{
30-
res += tab + "flibbers:\n";
31-
for (const Flibber& fli : myFlibbers)
32-
res += tab + '\t' + fli.myKey + ": " + fli.myValue + '\n';
33-
}
28+
res += FlibberCollection::toString(tabs);
3429
return res;
3530
}
3631

‎moduleFoo.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ ObjectFoo::toString(uint8_t tabs) const
1616
std::string res;
1717
res += tab + "wibble: " + std::to_string(myWibble) + '\n';
1818
res += tab + "zorble: " + myZorble + '\n';
19-
if (myFlibbers.size() > 0)
20-
{
21-
res += tab + "flibbers:\n";
22-
for (const Flibber& fli : myFlibbers)
23-
res += tab + '\t' + fli.myKey + ": " + fli.myValue + '\n';
24-
}
19+
res += FlibberCollection::toString(tabs);
2520
return res;
2621
}
2722

0 commit comments

Comments
 (0)
Please sign in to comment.