FunctionList.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. /*
  3. * FunctionList.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. /// List of functions that share the same signature - can be used to call all of them easily
  12. template<typename Signature>
  13. class CFunctionList
  14. {
  15. public:
  16. std::vector<boost::function<Signature> > funcs;
  17. CFunctionList(int){};
  18. CFunctionList(){};
  19. template <typename Functor>
  20. CFunctionList(const Functor &f)
  21. {
  22. funcs.push_back(boost::function<Signature>(f));
  23. }
  24. CFunctionList(const boost::function<Signature> &first)
  25. {
  26. funcs.push_back(first);
  27. }
  28. CFunctionList(boost::function<Signature> &first)
  29. {
  30. funcs.push_back(first);
  31. }
  32. CFunctionList(std::nullptr_t)
  33. {}
  34. CFunctionList & operator+=(const boost::function<Signature> &first)
  35. {
  36. funcs.push_back(first);
  37. return *this;
  38. }
  39. void add(const CFunctionList<Signature> &first)
  40. {
  41. for (size_t i = 0; i < first.funcs.size(); i++)
  42. {
  43. funcs.push_back(first.funcs[i]);
  44. }
  45. }
  46. void clear()
  47. {
  48. funcs.clear();
  49. }
  50. operator bool() const
  51. {
  52. return funcs.size();
  53. }
  54. void operator()() const
  55. {
  56. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  57. for(size_t i=0;i<funcs2.size(); ++i)
  58. {
  59. if (funcs2[i])
  60. funcs2[i]();
  61. }
  62. }
  63. template <typename Arg>
  64. void operator()(const Arg & a) const
  65. {
  66. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  67. for(int i=0;i<funcs2.size(); i++)
  68. {
  69. if (funcs2[i])
  70. funcs2[i](a);
  71. }
  72. }
  73. // Me wants variadic templates :(
  74. template <typename Arg1, typename Arg2>
  75. void operator()(Arg1 & a, Arg2 & b) const
  76. {
  77. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  78. for(int i=0;i<funcs2.size(); i++)
  79. {
  80. if (funcs2[i])
  81. funcs2[i](a, b);
  82. }
  83. }
  84. };
  85. template<typename Signature>
  86. class CFunctionList2
  87. {
  88. public:
  89. std::vector<boost::function<Signature> > funcs;
  90. CFunctionList2(int){};
  91. CFunctionList2(){};
  92. template <typename Functor>
  93. CFunctionList2(const Functor &f)
  94. {
  95. funcs.push_back(boost::function<Signature>(f));
  96. }
  97. CFunctionList2(const boost::function<Signature> &first)
  98. {
  99. funcs.push_back(first);
  100. }
  101. CFunctionList2(boost::function<Signature> &first)
  102. {
  103. funcs.push_back(first);
  104. }
  105. CFunctionList2 & operator+=(const boost::function<Signature> &first)
  106. {
  107. funcs.push_back(first);
  108. return *this;
  109. }
  110. void clear()
  111. {
  112. funcs.clear();
  113. }
  114. operator bool() const
  115. {
  116. return funcs.size();
  117. }
  118. template <typename Arg>
  119. void operator()(const Arg & a) const
  120. {
  121. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  122. for(size_t i=0;i<funcs2.size(); ++i)
  123. {
  124. funcs2[i](a);
  125. }
  126. }
  127. };