Image Processing Pipeline I'm currently toying around with some Computer Vision algorithms and always wanted to learn more about templates, so I came up with the idea to build a templated Image Processing Pipeline.

I want the pipeline to be built of several task blocks which I want to plug into each other sequentially. For a specific block, its input and output types are fixed. I ultimately want to be able to inherently control for a block to only accept the next block if the output type of the current is identical to the input type of the next.




         Block_N                 Block_N+1
     _______________         _________________ 
int |               |  str  |                 | float
---&gt;| str = f1(int) |------&gt;| float = f2(str) |-------&gt;
    |_______________|       |_________________|



Consequently, Block_N+2 would have to accept float as input and may have an arbitrary output type. 

This is what I was able to come up with:

Abstract base classes:



template&lt;class I&gt;
class AbstractInput {

public:
    virtual ~AbstractInput() { }

    virtual void call( I _input ) = 0;
};


template&lt;class I, class O&gt;
class AbstractPipelineTask : public AbstractInput&lt;I&gt; {

public:
    typedef AbstractInput&lt;O&gt;* Ptr;

    AbstractPipelineTask( AbstractInput&lt;O&gt;* _nextTask = NULL) 
        : m_nextTask ( _nextTask ) { }

    virtual void call( I _input ) {

        m_input = _input;
        m_output = executeTask( m_input );

        if ( m_nextTask )
            m_nextTask-&gt;call( m_output );
    }

    virtual O executeTask( I&amp; _input ) = 0;

protected:  
    AbstractInput&lt;O&gt;* m_nextTask;

    I m_input;
    O m_output;
};


Example implementation:

class Str2IntPipelineTask : public AbstractPipelineTask&lt; std::string, int &gt; {

public:
    Str2IntPipelineTask( AbstractInput&lt;int&gt;* _nextTask = NULL ) 
        : AbstractPipelineTask( _nextTask ) { }
    virtual ~Str2IntPipelineTask() { }

    virtual int executeTask( std::string&amp; _input ) {

        return atoi( _input.c_str() );
    }
};

class Int2IntPipelineTask : public AbstractPipelineTask&lt; int, int&gt; {

public:
    Int2IntPipelineTask( AbstractInput&lt;int&gt;* _nextTask = NULL ) 
        : AbstractPipelineTask( _nextTask ) { }
    virtual ~Int2IntPipelineTask() { }

    virtual int executeTask( int&amp; _input ) {

        return ( _input * _input );
    }
};

class Int2FloatPipelineTask : public AbstractPipelineTask&lt; int, float &gt; {

public:
    Int2FloatPipelineTask( AbstractInput&lt;float&gt;* _nextTask = NULL ) 
        : AbstractPipelineTask( _nextTask ) { }
    virtual ~Int2FloatPipelineTask() { }

    virtual float executeTask( int&amp; _input ) {

        return ( static_cast&lt;float&gt;( _input ) / 42.f );
    }
};


Examples:

int _tmain(int argc, _TCHAR* argv[])
{

    Int2FloatPipelineTask* pipeTask2    = new Int2FloatPipelineTask( );
    Int2IntPipelineTask* pipeTask1      = new Int2IntPipelineTask( pipeTask2 );
    Str2IntPipelineTask* pipeTask0      = new Str2IntPipelineTask( pipeTask1 );

    pipeTask0-&gt;call( "42" );

    return 0;
}


What I would really like to know besides some general advice is:


How can I get rid of the raw pointers AbstractInput&lt;O&gt;* m_nextTask? I tried changing the typedef of Ptr to std::shared_ptr&lt;AbstractInput&lt;O&gt;&gt; but that broke the derived classes constructors.
Is it possible to get rid of AbstractInput?
Since none of these classes have private members, they can be structs instead.  As such, you'll no longer need to explicitly give them public inheritance.
Consider making the magic number 42.f a constant so that its intent is known.
This is specific to Visual Studio, thus is non-portable:


int _tmain(int argc, _TCHAR* argv[])



It should be changed to this to fit the standard:

int main(int argc, char* argv[])

In your various executeTask() functions, I believe you're meaning to pass by const&amp;, not just by reference.  You're not modifying the argument, so const is best with them.