FunctionList.h 2.5 KB

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