FunctionList.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef FUNCTIONLIST_H
  2. #define FUNCTIONLIST_H
  3. #ifdef _MSC_VER
  4. #pragma once
  5. #endif
  6. #include <boost/function.hpp>
  7. template<typename Signature>
  8. class CFunctionList
  9. {
  10. public:
  11. std::vector<boost::function<Signature> > funcs;
  12. CFunctionList(int){};
  13. CFunctionList(){};
  14. template <typename Functor>
  15. CFunctionList(const Functor &f)
  16. {
  17. funcs.push_back(boost::function<Signature>(f));
  18. }
  19. CFunctionList(const boost::function<Signature> &first)
  20. {
  21. funcs.push_back(first);
  22. }
  23. CFunctionList(boost::function<Signature> &first)
  24. {
  25. funcs.push_back(first);
  26. }
  27. CFunctionList & operator+=(const boost::function<Signature> &first)
  28. {
  29. funcs.push_back(first);
  30. return *this;
  31. }
  32. void add(const CFunctionList<Signature> &first)
  33. {
  34. for (size_t i = 0; i < first.funcs.size(); i++)
  35. {
  36. funcs.push_back(first.funcs[i]);
  37. }
  38. }
  39. void clear()
  40. {
  41. funcs.clear();
  42. }
  43. operator bool() const
  44. {
  45. return funcs.size();
  46. }
  47. void operator()() const
  48. {
  49. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  50. for(size_t i=0;i<funcs2.size(); ++i)
  51. {
  52. funcs2[i]();
  53. }
  54. }
  55. template <typename Arg>
  56. void operator()(const Arg & a) const
  57. {
  58. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  59. for(int i=0;i<funcs2.size(); i++)
  60. {
  61. funcs2[i](a);
  62. }
  63. }
  64. };
  65. template<typename Signature>
  66. class CFunctionList2
  67. {
  68. public:
  69. std::vector<boost::function<Signature> > funcs;
  70. CFunctionList2(int){};
  71. CFunctionList2(){};
  72. template <typename Functor>
  73. CFunctionList2(const Functor &f)
  74. {
  75. funcs.push_back(boost::function<Signature>(f));
  76. }
  77. CFunctionList2(const boost::function<Signature> &first)
  78. {
  79. funcs.push_back(first);
  80. }
  81. CFunctionList2(boost::function<Signature> &first)
  82. {
  83. funcs.push_back(first);
  84. }
  85. CFunctionList2 & operator+=(const boost::function<Signature> &first)
  86. {
  87. funcs.push_back(first);
  88. return *this;
  89. }
  90. void clear()
  91. {
  92. funcs.clear();
  93. }
  94. operator bool() const
  95. {
  96. return funcs.size();
  97. }
  98. template <typename Arg>
  99. void operator()(const Arg & a) const
  100. {
  101. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  102. for(size_t i=0;i<funcs2.size(); ++i)
  103. {
  104. funcs2[i](a);
  105. }
  106. }
  107. };
  108. #endif //FUNCTIONLISt_H