2
0

FunctionList.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. 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. };