title: Modeling a pipeline in C++ with replaceable stages
body: <p>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.:</p>
  
  <pre><code>class Pipeline {
  protected:
    virtual void processA(const In&amp; in, BType&amp; B) = 0;
    virtual void processB(const BType&amp; B, BType&amp; C) = 0;
    virtual void processC(const CType&amp; C, BType&amp; D) = 0;
    virtual void processD(const DType&amp; D, Out&amp; out) = 0;
  public:
    void process(const In&amp; in, Out&amp; out) {
      Btype B;
      processA(in, B);
      Ctype C;
      processB(B, C);
      Btype D;
      processC(C, D);
      processD(D,out);
    }
  };
  </code></pre>
  
  <p>The problem with this approach, if each of the N stages can be interchanged with M processes you have N*M possible subclasses.</p>
  
  <p>Another idea is to store function objects:</p>
  
  <pre><code>class Pipeline {
  public:
    std::function&lt;void(const In&amp; in, BType&amp; B)&gt; processA;
    std::function&lt;void(const In&amp; B, CType&amp; C)&gt; processB;
    std::function&lt;void(const In&amp; C, DType&amp; D)&gt; processC;
    std::function&lt;void(const In&amp; D, Out&amp; out)&gt; processD;
    void process(const In&amp; in, Out&amp; out) {
      Btype B;
      processA(in, B);
      Ctype C;
      processB(B, C);
      Btype D;
      processC(C, D);
      processD(D,out);
    }
  };
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>