Private member functors for a C++ class -


i'm writing class have member methods have data associated them, mechanical systems of robot require use of. thought write them functors, (this isn't actual code):

class myrobot : public robot { public:   myrobot();   void runrobot(); private:   command do_something_,           do_another_thing_; } 

and initialize do_something_ lambda in constructor like:

do_something_( [] {   do_first_thing();   do_second_thing(); } ); 

and tell do_something_ requirements has:

do_something_.requires( system_a ); do_something_.requires( system_b ); 

and in runrobot() tell robot's scheduler execute commands:

void myrobot::runrobot() {   scheduler.add(do_something_);   scheduler.add(do_another_thing_); } 

but have come realize number of commands grows, less manageable constructor myrobot become, every command have body defined there. make corresponding private method each command , initialize them function pointer instead of lambda, seems more convoluted. subclass command each specific command , thereby have body , requirements in separate file each one, feels lot of overhead simple task. there way i'm not aware of?

you can define command class take std::function , initializer list of "requirements" have them. then, instead of using lambdas can make do_something , do_another_thing own private member functions don't have define bodies in constructor. finally, in constructor can construct command instances binding private member functions pointer of current myrobot instance while giving them list of requirements. command objects should able modify private state of myrobot instances. example below. also, see example output.

#include <functional> #include <iostream> #include <vector>  enum system { sys_a, sys_b, sys_c };  class command { public:   typedef std::function<void()> functype;    command(functype func, std::initializer_list<system> requirements)   :func_(func), requirements_(requirements) { }    void operator()() {     std::cout << "executing command:" << std::endl;     ( system s : requirements_ )       std::cout << "   requires " << static_cast<int>(s) << std::endl;     func_();   }  private:   functype            func_;   std::vector<system> requirements_; };  class scheduler { public:   void add(command c) {     c();   } };  class robot { public:   robot()   :do_something_    (std::bind(&robot::do_something, this),     {sys_a, sys_b}),    do_another_thing_(std::bind(&robot::do_another_thing, this), {sys_a, sys_c}) { }    void runrobot() {     s_.add(do_something_);     s_.add(do_another_thing_);   }  private:   void do_first_thing()  { std::cout << "   first thing!" << std::endl;  }   void do_second_thing() { std::cout << "   second thing!" << std::endl; }   void do_third_thing()  { std::cout << "   third thing!" << std::endl;  }    void do_something()     { do_first_thing(); do_second_thing(); }   void do_another_thing() { do_first_thing(); do_third_thing();  }    command   do_something_;   command   do_another_thing_;   scheduler s_; };  int main(int, char**) {   robot().runrobot(); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -