C
C
CaptainTrunky2012-01-17 13:34:57
C++ / C#
CaptainTrunky, 2012-01-17 13:34:57

C++, functor container?

Good afternoon!
There is the following task: it is necessary to implement a kind of pipeline that accepts some data as input, processes and outputs data of a different type. The trouble is that the stages of the pipeline accept and issue data of different types. In pseudocode, I see it like this:

class BaseStage
{
//Interface of stage
};
class Stage1: BaseStage
{
    int operator()(SomeType data);
};
class Stage2: BaseStage
{
    float operator()(SomeType data);
};

class Line
{
    std::vector<BaseStage> _line;
    std::vector<BaseStage>::iterator _it;
    void func()
    {
        _it = _line.begin();
        SomeType oldData, newData;
        while (_it != _line.end())
        {
            newData = (*it)(oldData);
            oldData = newData;
        }
    }
};

Is it correct to use void* and reinterpret_cast instead of SomeType (then each stage in operator() will perform the corresponding type casting)? Perhaps there are already implementations of a similar approach? Perhaps this is an overly complicated construction, which is nothing more than mental gymnastics? Thanks in advance for advice/criticism.)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xanep, 2012-01-17
@xanep

See Alexandrescu Modern C++ Design. Chapter 5 "Generalized Functors".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question