c++ - How to execute unary function objects of different parameter type in sequence? -
c++ - How to execute unary function objects of different parameter type in sequence? -
i'm designing mechanism execute set of unary function objects in sequence. these function objects assigned during runtime, , problem is: parameter type of these function objects different.
what want this:
class command_sequence { private: /* 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'm writing c code, have no selection callback function pointer takes void* parameter. i'm working c++, there should elegant way deal it.
the best way can think declaring template class virtually inherit 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); } } }; this approach can fit needs, allocate , release template_pair each entry. i've no thought whether cause memory fragment, since procedure called quite during process.
is there improve way ?
since appears argument unary function fixed @ time add together sequence, create sequence take zero-argument function objects using boost::function, boost::bind required parameter, e.g.
class command_sequence { public: void add( boost::function<void(void)> functor ); }; /* ... before ... */ log_usage logger; check_temperature checker; sequence.add( boost::bind<void>(logger, "administrator") ); sequence.add( boost::bind<void>(checker, lobbymeter.read_temperature()) ); note have specify <void> template parameter boost::bind phone call since can't deduce homecoming type of function object automatically. alternatively, can expose public typedef called result_type in class definition avoids this, i.e.
class log_usage { public: typedef void result_type; void operator() (const std::string& message) { // stuff ... } }; /* ... */ sequence.add(boost::bind(logger, "blah")); // compile c++ function-object
Comments
Post a Comment