How to consistently execute unary function objects of different types of parameters?

I am developing a mechanism that will consistently execute a set of unary function objects. These functional objects are assigned at runtime, and the problem is this: the parameter type of these function objects is different.

I want to do something like this:

class command_sequence {
private:

/* some kind of container */

public:
    void add( FUNC_OBJ &func, PARAM val );
    void run(void);
};

class check_temperature {
public:
    void operator() (int celsius) {
        if(celsius > 26) {
            cooler.switch_on();
        }
    }
};

class log_usage {
public:
    void operator() (std::string username) {
        username.append(" logged in");
        syslog(LOG_NOTICE,username.c_str());
    }
};

command_sequence sequence;
log_usage logger;
check_temperature checker;

sequence.add(logger, std::string("administrator"));
sequence.add(checker, lobbyMeter.read_temperature());
sequence.add(logger, std::string("lecture"));
sequence.add(checker, classroomMeter.read_temperature());
sequence.run();

If I write C code, I have no choice but to the pointer to the callback function, which takes the void * parameter as a parameter. But now I am working with C ++, there should be an elegant way to handle this.

The best way I can think now is to declare a template class that actually inherits from the abstract wrapper class:

class command_sequence {
private:

    class runner {
    public:
        virtual void execute(void) = 0;
    };

    template <class FUNC, typename T> class func_pair : public runner {
    private:
        FUNC &func;
        T param;
    public:
        func_pair(FUNC &f, const T &t) : func(f),param(t) { }
        void execute(void) {
            func(param);
        }
    };

    std::vector<runner*> funcQueue;

public:

    template <class FUNC, typename T> void add(FUNC &obj, const T &t) {
        funcQueue.push_back( new func_pair<FUNC,T>(obj,t) );
    }

    void run(void) {
        std::vector<runner*>::iterator itr=funcQueue.begin();
        for(;itr!=funcQueue.end();++itr) {
            (*itr)->execute();
            delete (*itr);
        }
    }
};

, template_pair . , , .

?

+5
2

, , , boost:: function, boost:: bind ,

class command_sequence {
public:
    void add( boost::function<void(void)> functor );
};

/* ... as before ... */

log_usage logger;
check_temperature checker;

sequence.add( boost::bind<void>(logger, "administrator") );
sequence.add( boost::bind<void>(checker, lobbymeter.read_temperature()) );

, <void> boost::bind, . , typedef result_type , ,

class log_usage
{
public:
    typedef void result_type;
    void operator() (const std::string& message)
    {
        // do stuff ...
    }
};

/* ... */

sequence.add(boost::bind(logger, "blah")); // will now compile
+4

? boost::bind, :

void check_temperature( int celsius )
{
    if(celsius > 26) {
        cooler.switch_on();
    }
};

void log_usage( std::string username ) 
{
    username.append(" logged in");
    syslog(LOG_NOTICE,username.c_str());
};

// keep actions
typedef std::vector< boost::function<void()> > func_arr_t;
func_arr_t actions;
actions.push_back( boost::bind( &log_usage, "administrator" ) );
actions.push_back( boost::bind( &check_temperature, lobbyMeter.read_temperature() ) );
actions.push_back( boost::bind( &log_usage, "lecture" ) );
actions.push_back( boost::bind( &check_temperature, classroomMeter.read_temperature() ) );

// run all
for ( func_arr_t::const_iterator it = actions.begin(); it != actions.end(); ++it )
    (*it)();

command_sequence .

+7

All Articles