Skip to content

Commit 5d83581

Browse files
committed
Solution 12 WashProgramme - changed array from Step to Step*
1 parent 292b778 commit 5d83581

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

solutions/12_substitutable_objects/src/WashProgramme.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,52 @@ const char *get_type_name(const WMS::Step &step) {
1717
return string_map[unsigned(step.get_type())];
1818
};
1919

20-
void print_step_details(const WMS::Step &step) {
20+
void print_step_details(WMS::Step &step) {
2121
std::cout << std::fixed << std::setprecision(2);
2222
std::cout << "Step '" << get_type_name(step) << "' "
2323
<< "running for " << (step.get_duration() / 1000.0) << " seconds\n";
2424
}
25+
2526
} // namespace
2627

2728
namespace WMS {
2829

2930
bool WashProgramme::add(Step &step) {
30-
// if the array is not full, only add valid steps
31+
// If the array is not full and the step is valid, add it to the array
3132
if (next == std::end(steps) || !step.is_valid()) {
3233
return false;
3334
}
34-
*next = step;
35+
*next = &step;
3536
++next;
3637
return true;
3738
}
3839

3940
#if 1
4041
void WashProgramme::run() {
41-
for (auto &step : steps) {
42-
if (step.is_valid()) {
43-
print_step_details(step);
42+
for (auto step : steps) { // could be auto* step - very much a matter of style
43+
if (step) { // Check for nullptr array members
44+
print_step_details(*step);
4445
if (display) {
45-
display->display(unsigned(step.get_type()));
46+
display->display(unsigned(step->get_type()));
4647
}
47-
step.run();
48+
step->run();
4849
}
4950
}
5051
}
5152
#else
52-
// The run() method now uses the iterator 'next' to determine the end of the
53-
// steps array (called the sential value). This means that the array can be
54-
// partially filled and the programme will only run the steps that have been
55-
// added. Also we don;t have to check if the step is valid as we know that the
56-
// array is only partially filled with valid steps.
53+
// Alternative implementation using iterators
54+
// This does not need to check for nullptrs
5755
void WashProgramme::run() {
5856
for (auto step = std::begin(steps); step != next; ++step) {
59-
print_step_details(*step);
57+
Step &current_step = **step;
58+
print_step_details(current_step);
6059
if (display) {
61-
display->display(unsigned(step->get_type()));
60+
display->display(unsigned(current_step.get_type()));
6261
}
63-
step->run();
62+
current_step.run();
6463
}
6564
}
65+
6666
#endif
6767

6868
void connect(WashProgramme &wash, Devices::SevenSegment &sseg) {

solutions/12_substitutable_objects/src/WashProgramme.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class WashProgramme {
2525
friend void connect(WashProgramme &wash, Devices::SevenSegment &sseg);
2626

2727
constexpr static unsigned wash_size{16};
28-
using Container = std::array<Step, wash_size>;
28+
using Container = std::array<Step *, wash_size>;
2929

3030
Container steps{};
3131
Container::iterator next{std::begin(steps)};

0 commit comments

Comments
 (0)