2
0

FunctionList.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * FunctionList.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  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. std::vector<std::function<Signature> > funcs;
  16. public:
  17. CFunctionList( std::nullptr_t ) { }
  18. CFunctionList( int ) { }
  19. CFunctionList( ){ }
  20. template <typename Functor>
  21. CFunctionList(const Functor &f)
  22. {
  23. funcs.push_back(std::function<Signature>(f));
  24. }
  25. CFunctionList(const std::function<Signature> &first)
  26. {
  27. if (first)
  28. funcs.push_back(first);
  29. }
  30. CFunctionList & operator+=(const CFunctionList<Signature> &first)
  31. {
  32. for( auto & fun : first.funcs)
  33. {
  34. funcs.push_back(fun);
  35. }
  36. return *this;
  37. }
  38. void clear()
  39. {
  40. funcs.clear();
  41. }
  42. operator bool() const
  43. {
  44. return !funcs.empty();
  45. }
  46. template <typename... Args>
  47. void operator()(Args ... args) const
  48. {
  49. std::vector<std::function<Signature> > funcs_copy = funcs;
  50. for( auto & fun : funcs_copy)
  51. {
  52. if (fun)
  53. fun(args...);
  54. }
  55. }
  56. };