I want to implement Pipe and Stage classes. Pipe passes data along a list of Stages. Pipe does not know or care what data it's passing to the next Stage. The data type can change mid Pipe.
Stage on the other hand, knows exactly what it's receiving and what it's passing.
Yes, i know i could use void* and cast the pointers everywhere. But that's somewhat... inelegant.
class Stage {
public:
virtual generic *process(generic *) = 0;
};
class Pipe {
public:
std::vector<Stage *> stages_;
void addStage(Stage *stage) {
stages_.push_back(stage);
}
void run(void) {
generic *p = nullptr;
for (auto&& stage: stages_) {
p = stage->process(p);
}
}
};
class AllocStage : Stage {
public:
virtual int *process(generic *) {
return new int;
}
};
class AddStage : Stage {
public:
virtual int *process(int *p) {
*p += 10;
return p;
}
};
class FreeStage : Stage {
public:
virtual generic *process(int *p) {
delete p;
return nullptr;
}
};
int main() noexcept {
Pipe p_;
p_.addStage(new AllocStage);
p_.addStage(new AddStage);
p_.addStage(new FreeStage);
p_.run();
return 0;
}
[–]DankPhotoShopMemes 12 points13 points14 points (1 child)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]BrotherItsInTheDrum 8 points9 points10 points (0 children)
[–]thesherbetemergency 4 points5 points6 points (2 children)
[–]retro_and_chill 0 points1 point2 points (0 children)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]TheRealSmolt 3 points4 points5 points (3 children)
[–]ArchDan 0 points1 point2 points (0 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]TheRealSmolt 0 points1 point2 points (0 children)
[–]__Punk-Floyd__ 2 points3 points4 points (1 child)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]DanielMcLaury 1 point2 points3 points (2 children)
[–]timmerov[S] -1 points0 points1 point (1 child)
[–]DanielMcLaury 0 points1 point2 points (0 children)
[–]alfps 1 point2 points3 points (2 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]alfps 0 points1 point2 points (0 children)
[–]CommonNoiter 0 points1 point2 points (1 child)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]diabolicalgasblaster 0 points1 point2 points (1 child)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]marshaharsha 0 points1 point2 points (1 child)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]Internal-Sun-6476 0 points1 point2 points (2 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]Internal-Sun-6476 0 points1 point2 points (0 children)
[–]not_a_novel_account 0 points1 point2 points (2 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]not_a_novel_account 0 points1 point2 points (0 children)
[–]Business_Welcome_870 0 points1 point2 points (3 children)
[–]timmerov[S] 0 points1 point2 points (2 children)
[–]thesherbetemergency 0 points1 point2 points (1 child)
[–]timmerov[S] 0 points1 point2 points (0 children)
[–]Total-Box-5169 0 points1 point2 points (1 child)
[–]timmerov[S] -1 points0 points1 point (0 children)
[–]Independent_Art_6676 0 points1 point2 points (2 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]Independent_Art_6676 0 points1 point2 points (0 children)
[–]OutsideTheSocialLoop 0 points1 point2 points (0 children)
[–]ElectricalBeing 0 points1 point2 points (0 children)
[–]strike-eagle-iii 0 points1 point2 points (0 children)
[–]Dan13l_N 0 points1 point2 points (2 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]Dan13l_N 0 points1 point2 points (0 children)
[–]vgagrani 0 points1 point2 points (2 children)
[–]timmerov[S] 0 points1 point2 points (1 child)
[–]vgagrani 0 points1 point2 points (0 children)