Newer
Older
Import / research / pipeline / Modules / SimpleModule.cpp
#include "Types/Module.hpp"
#include <list>

class SimpleModule : public Module {
public:
    SimpleModule() : next( 0 ) { }
    
    bool isBlocking() { return false; }
    std::list<Address> threadAffinity() { }
    
    bool supportsOutputType(Format type)
    {
        return outputFormat() == type;
    }
    
    virtual void init() = 0;
    
    void command( Commands command, const void *arg )
    {
        switch (command) {
            case Process:
                process( *((Frame *)arg) );
                break;
            case Simulate:
                simulate( *((Frame *)arg) );
                break;
            case Deref:
                ((Frame *)arg)->deref();
                break;
            case Init:
                init();
                break;
        }
    }

    void dispatch( Address address, Commands command, const void *arg )
    {
        if ( address )
            staticDispatch( address, command, arg );
        else if ( pipelineMgr && ( command == Process || command == Simulate ) )
            pipelineMgr->unconnectedRoute( this, *(const Frame *)arg );            
    }

    virtual void derefFrame( Frame *frame )
    {
        dispatch( prev, Deref, frame );
    }
    
    virtual void process( const Frame &frame )
    {
        dispatch( next, Process, &frame );
    }
    
    virtual void simulate( const Frame &frame )
    {
        process( frame );
    }
        
    void connectTo( Address n, const Frame &f )
    { 
        next = n;
    }   
    
    void connectedFrom( Address n, const Frame &f )
    { 
        prev = n;
    }   
    
    Frame *getAvailableFrame()
    {
        Frame *frame;
        std::list<Frame*>::iterator it;
        for ( it = used.begin(); it != used.end(); ++it ) {
            frame = *it;
            if ( frame->refcount() == 0 ) {
                reuseFrame( frame );
                frame->ref();
                return frame;
            }
        }        
        frame = createNewFrame();
        frame->ref();
        used.push_back( frame );
        return frame;
    }
        
    virtual Frame* createNewFrame()
    { 
        return new Frame;
    }
    
    virtual void reuseFrame( Frame *frame )
    { }

private:
    std::list<Frame*> used;
    Module *next;
    Module *prev;
};