I am trying to concoct a C++ data structure for modeling a simple N stage process where each stage can be replaced with a different function. One way is to use the OO approach and have an abstract base class with a virtual method for each stage; e.g.:
class Pipeline {
protected:
virtual void processA(const In& in, BType& B) = 0;
virtual void processB(const BType& B, BType& C) = 0;
virtual void processC(const CType& C, BType& D) = 0;
virtual void processD(const DType& D, Out& out) = 0;
public:
void process(const In& in, Out& out) {
Btype B;
processA(in, B);
Ctype C;
processB(B, C);
Btype D;
processC(C, D);
processD(D,out);
}
};
The problem with this approach, if each of the N stages can be interchanged with M processes you have N*M possible subclasses.
Another idea is to store function objects:
class Pipeline {
public:
std::function<void(const In& in, BType& B)> processA;
std::function<void(const In& B, CType& C)> processB;
std::function<void(const In& C, DType& D)> processC;
std::function<void(const In& D, Out& out)> processD;
void process(const In& in, Out& out) {
Btype B;
processA(in, B);
Ctype C;
processB(B, C);
Btype D;
processC(C, D);
processD(D,out);
}
};
The problem I am having with this approach is that the stages are not really independent and I would like a single object, in some cases, to store info concerning multiple stages.
Has anyone found a good data structure for a pipeline with replaceable parts? A bonus would be able to allow each stage to run concurrently.