Advertisement
SilverTES

Dynamic Function reDefinition : C++

Jan 5th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <algorithm>
  4.  
  5. template <class M = std::string, class E = int>
  6. E log(M msg, E error = 0)
  7. {
  8.     std::cout << msg;
  9.     return error;
  10. }
  11.  
  12. class Entity
  13. {
  14.     public:
  15.  
  16.         Entity()
  17.         {
  18.  
  19.         }
  20.         virtual ~Entity()
  21.         {
  22.             _update = nullptr;
  23.             _render = nullptr;
  24.         }
  25.  
  26.         void setUpdate(std::function<void(Entity*)> update)
  27.         {
  28.             _update = update;
  29.         }
  30.  
  31.         void setRender(std::function<void(Entity*)> render)
  32.         {
  33.             _render = render;
  34.         }
  35.  
  36.  
  37.         void update()
  38.         {
  39.             if (_update != nullptr)
  40.                 _update(this);
  41.             else
  42.                 log("- Entity update() not defined !\n");
  43.         }
  44.         void render()
  45.         {
  46.             if (_render != nullptr)
  47.                 _render(this);
  48.             else
  49.                 log("- Entity render() not defined !\n");
  50.         }
  51.  
  52.         std::string name()
  53.         {
  54.             return _name;
  55.         }
  56.  
  57.         int _x = 0;
  58.         int _y = 0;
  59.  
  60.         std::string _name = "NONAME";
  61.  
  62.     protected:
  63.  
  64.         std::function<void(Entity*)> _update = nullptr;
  65.         std::function<void(Entity*)> _render = nullptr;
  66.  
  67.     private:
  68. };
  69.  
  70.  
  71. void updateTest(Entity *entity)
  72. {
  73.     entity->_name = "now use updateTest !";
  74.     entity->_x += 4;
  75.     entity->_y += 4;
  76. }
  77.  
  78. void updateTest2(Entity *entity)
  79. {
  80.     entity->_name = "now use updateTest2 !";
  81.     entity->_x += 8;
  82.     entity->_y += 8;
  83. }
  84.  
  85.  
  86. void renderTest(Entity *entity)
  87. {
  88.     log("=================================\n");
  89.     log(" Name = "+ entity->name()+"\n");
  90.     log(" X    = ");log(entity->_x);log("\n");
  91.     log(" Y    = ");log(entity->_y);log("\n");
  92.     log("=================================\n");
  93. }
  94.  
  95. int add(int first, int second)
  96. {
  97.     return first + second;
  98. }
  99.  
  100. int main()
  101. {
  102.     int vel = 2;
  103.  
  104.     Entity test;
  105.  
  106.     test.setRender(renderTest);
  107.  
  108.     test.setUpdate(updateTest);
  109.     test.update();
  110.     test.render();
  111.  
  112.     test.setUpdate(updateTest2);
  113.     test.update();
  114.     test.render();
  115.  
  116.     // use Lambda functions !
  117.     test.setUpdate(
  118.         [&](Entity*entity)   // all access (read & write)
  119.         //[=](Entity*entity) // read only
  120.         //[](Entity*entity)  // no access
  121.         {
  122.             vel = 10; // need [&]
  123.  
  124.             entity->_name = "now use lambda function !";
  125.             entity->_x += 666 + vel; // need [=]
  126.             entity->_y += 666;
  127.         }
  128.     );
  129.     test.update();
  130.     test.render();
  131.  
  132.     //std::function<int(int)> func = [](int i){return i*1980;};
  133.     auto func = [](int i, int j){return i+(j*2);};
  134.  
  135.     //Rearrange function add!
  136.     //auto newFunc = std::bind(func, std::placeholders::_2, std::placeholders::_1); // swap arguments order !
  137.     auto newFunc = std::bind(func, 10, std::placeholders::_1); // default value of first argument !
  138.  
  139.     std::cout << "Lambda function = "<< func(2,4) << "\n";
  140.     //std::cout << "new Lambda function = "<< newFunc(2,4) << "\n";
  141.     std::cout << "new Lambda function = "<< newFunc(2) << "\n";
  142.  
  143.     return log("- Program terminated OK ! \n");
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement