FunctionList.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 & operator+=(const boost::function<Signature> &first)
  33. {
  34. funcs.push_back(first);
  35. return *this;
  36. }
  37. void add(const CFunctionList<Signature> &first)
  38. {
  39. for (size_t i = 0; i < first.funcs.size(); i++)
  40. {
  41. funcs.push_back(first.funcs[i]);
  42. }
  43. }
  44. void clear()
  45. {
  46. funcs.clear();
  47. }
  48. operator bool() const
  49. {
  50. return funcs.size();
  51. }
  52. void operator()() const
  53. {
  54. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  55. for(size_t i=0;i<funcs2.size(); ++i)
  56. {
  57. if (funcs2[i])
  58. funcs2[i]();
  59. }
  60. }
  61. template <typename Arg>
  62. void operator()(const Arg & a) const
  63. {
  64. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  65. for(size_t i=0; i<funcs2.size(); ++i)
  66. {
  67. if (funcs2[i])
  68. funcs2[i](a);
  69. }
  70. }
  71. // Me wants variadic templates :(
  72. template <typename Arg1, typename Arg2>
  73. void operator()(Arg1 & a, Arg2 & b) const
  74. {
  75. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  76. for(size_t i=0; i<funcs2.size(); ++i)
  77. {
  78. if (funcs2[i])
  79. funcs2[i](a, b);
  80. }
  81. }
  82. };
  83. template<typename Signature>
  84. class CFunctionList2
  85. {
  86. public:
  87. std::vector<boost::function<Signature> > funcs;
  88. CFunctionList2(int){};
  89. CFunctionList2(){};
  90. template <typename Functor>
  91. CFunctionList2(const Functor &f)
  92. {
  93. funcs.push_back(boost::function<Signature>(f));
  94. }
  95. CFunctionList2(const boost::function<Signature> &first)
  96. {
  97. funcs.push_back(first);
  98. }
  99. CFunctionList2(boost::function<Signature> &first)
  100. {
  101. funcs.push_back(first);
  102. }
  103. CFunctionList2 & operator+=(const boost::function<Signature> &first)
  104. {
  105. funcs.push_back(first);
  106. return *this;
  107. }
  108. void clear()
  109. {
  110. funcs.clear();
  111. }
  112. operator bool() const
  113. {
  114. return funcs.size();
  115. }
  116. template <typename Arg>
  117. void operator()(const Arg & a) const
  118. {
  119. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  120. for(size_t i=0;i<funcs2.size(); ++i)
  121. {
  122. funcs2[i](a);
  123. }
  124. }
  125. };