FunctionList.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 clear()
  33. {
  34. funcs.clear();
  35. }
  36. operator bool() const
  37. {
  38. return funcs.size();
  39. }
  40. void operator()() const
  41. {
  42. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  43. for(size_t i=0;i<funcs2.size(); ++i) {
  44. funcs2[i]();
  45. }
  46. }
  47. template <typename Arg>
  48. void operator()(const Arg & a) const
  49. {
  50. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  51. for(int i=0;i<funcs2.size(); i++) {
  52. funcs2[i](a);
  53. }
  54. }
  55. };
  56. template<typename Signature>
  57. class CFunctionList2
  58. {
  59. public:
  60. std::vector<boost::function<Signature> > funcs;
  61. CFunctionList2(int){};
  62. CFunctionList2(){};
  63. template <typename Functor>
  64. CFunctionList2(const Functor &f)
  65. {
  66. funcs.push_back(boost::function<Signature>(f));
  67. }
  68. CFunctionList2(const boost::function<Signature> &first)
  69. {
  70. funcs.push_back(first);
  71. }
  72. CFunctionList2(boost::function<Signature> &first)
  73. {
  74. funcs.push_back(first);
  75. }
  76. CFunctionList2 & operator+=(const boost::function<Signature> &first)
  77. {
  78. funcs.push_back(first);
  79. return *this;
  80. }
  81. void clear()
  82. {
  83. funcs.clear();
  84. }
  85. operator bool() const
  86. {
  87. return funcs.size();
  88. }
  89. template <typename Arg>
  90. void operator()(const Arg & a) const
  91. {
  92. std::vector<boost::function<Signature> > funcs2 = funcs; //backup
  93. for(size_t i=0;i<funcs2.size(); ++i) {
  94. funcs2[i](a);
  95. }
  96. }
  97. };
  98. #endif //FUNCTIONLISt_H