FunctionList.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. template<typename Signature>
  15. class CFunctionList
  16. {
  17. public:
  18. std::vector<boost::function<Signature> > funcs;
  19. CFunctionList(int){};
  20. CFunctionList(){};
  21. template <typename Functor>
  22. CFunctionList(const Functor &f)
  23. {
  24. funcs.push_back(boost::function<Signature>(f));
  25. }
  26. CFunctionList(const boost::function<Signature> &first)
  27. {
  28. funcs.push_back(first);
  29. }
  30. CFunctionList(boost::function<Signature> &first)
  31. {
  32. funcs.push_back(first);
  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. funcs2[i]();
  60. }
  61. }
  62. template <typename Arg>
  63. void operator()(const Arg & a) const
  64. {
  65. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  66. for(int i=0;i<funcs2.size(); i++)
  67. {
  68. funcs2[i](a);
  69. }
  70. }
  71. };
  72. template<typename Signature>
  73. class CFunctionList2
  74. {
  75. public:
  76. std::vector<boost::function<Signature> > funcs;
  77. CFunctionList2(int){};
  78. CFunctionList2(){};
  79. template <typename Functor>
  80. CFunctionList2(const Functor &f)
  81. {
  82. funcs.push_back(boost::function<Signature>(f));
  83. }
  84. CFunctionList2(const boost::function<Signature> &first)
  85. {
  86. funcs.push_back(first);
  87. }
  88. CFunctionList2(boost::function<Signature> &first)
  89. {
  90. funcs.push_back(first);
  91. }
  92. CFunctionList2 & operator+=(const boost::function<Signature> &first)
  93. {
  94. funcs.push_back(first);
  95. return *this;
  96. }
  97. void clear()
  98. {
  99. funcs.clear();
  100. }
  101. operator bool() const
  102. {
  103. return funcs.size();
  104. }
  105. template <typename Arg>
  106. void operator()(const Arg & a) const
  107. {
  108. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  109. for(size_t i=0;i<funcs2.size(); ++i)
  110. {
  111. funcs2[i](a);
  112. }
  113. }
  114. };
  115. #endif //FUNCTIONLISt_H