Callback_16.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <array>
  2. #include <vector>
  3. #include <map>
  4. #include <iostream>
  5. #include <functional>
  6. template <typename T>
  7. std::function<void(T&)> callback;
  8. /*************************************************************************************************
  9. *
  10. */
  11. class page {
  12. public:
  13. page(int index, std::function<void()> func);
  14. void update_page(void);
  15. int get_index(void) { return index; };
  16. private:
  17. int index;
  18. std::function<void()> function;
  19. };
  20. page::page(int index, std::function<void()> func)
  21. {
  22. this->index = index;
  23. this->function = func;
  24. }
  25. void page::update_page(void)
  26. {
  27. this->function();
  28. }
  29. /*************************************************************************************************
  30. *
  31. */
  32. class the_sub {
  33. public:
  34. the_sub();
  35. void add_page(int index, std::function<void()> func);
  36. void update(int index);
  37. private:
  38. std::vector<page> pages;
  39. };
  40. the_sub::the_sub()
  41. {
  42. }
  43. void the_sub::add_page(int index, std::function<void()> func)
  44. {
  45. page tmp(index, func);
  46. pages.push_back(tmp);
  47. }
  48. void the_sub::update(int index)
  49. {
  50. for (int i = 0; i < (int)pages.size(); i++)
  51. {
  52. if (pages.at(i).get_index() == i)
  53. pages.at(i).update_page();
  54. }
  55. }
  56. /*************************************************************************************************
  57. *
  58. */
  59. class the_main : the_sub {
  60. public:
  61. uint8_t myAger = 12;
  62. enum {MALE, FEMALE, DIVERS} sex;
  63. the_main();
  64. void print_something(void);
  65. void display_the_class_text(void);
  66. void add_the_page(int index);
  67. void the_update_func(void);
  68. void update(int index) { the_sub::update(index); };
  69. private:
  70. };
  71. the_main::the_main(void) { }
  72. void the_main::add_the_page(int index)
  73. {
  74. //std::function<void(void)> t = std::bind(&the_main::display_the_class_text, this);
  75. add_page(index, std::bind(&the_main::display_the_class_text, this));
  76. }
  77. void the_main::print_something(void)
  78. {
  79. std::cout << "something more " << std::endl;
  80. }
  81. void the_main::display_the_class_text(void)
  82. {
  83. std::cout << "display the_main::text!" << std::endl;
  84. print_something();
  85. }
  86. /*************************************************************************************************
  87. *
  88. */
  89. void xyz(void)
  90. {
  91. std::cout << "display the text" << std::endl;
  92. }
  93. /*************************************************************************************************
  94. *
  95. */
  96. int main(void)
  97. {
  98. the_main tm;
  99. //callback t = (callback)&tm.display_the_class_text;
  100. tm.add_the_page(0);
  101. tm.update(0);
  102. std::cout << "main" << std::endl;
  103. return 0;
  104. }