FunctionList.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(int i=0;i<funcs2.size(); i++)
  66. {
  67. if (funcs2[i])
  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. };