Action.h
1 #pragma once
2 
3 #include "stormancer/BuildConfig.h"
4 
5 #include <functional>
6 #include <list>
7 #include <memory>
8 
9 namespace Stormancer
10 {
12  template<typename... TParams>
13  class Action
14  {
15  public:
16 
17  using TFunction = std::function<void(TParams...)>;
18  using TContainer = std::list<TFunction>;
19  using TIterator = typename TContainer::iterator;
20  using TAction = Action<TParams...>;
21 
22 #pragma region public_methods
23 
24  Action()
25  {
26  }
27 
28  Action(TFunction f)
29  {
30  _functions->push_back(f);
31  }
32 
33  virtual ~Action()
34  {
35  }
36 
37  Action(const TAction& other)
38  : _functions(other._functions)
39  {
40  }
41 
42  Action(TAction&& other)
43  : _functions(std::move(other._functions))
44  {
45  }
46 
47  TAction& operator=(const TAction& other)
48  {
49  _functions = other._functions;
50  return *this;
51  }
52 
53  TAction& operator=(TFunction f)
54  {
55  _functions->clear();
56  _functions->push_back(f);
57  return *this;
58  }
59 
60  TAction& operator+=(TFunction f)
61  {
62  _functions->push_back(f);
63  return *this;
64  }
65 
66  const TAction& operator()(TParams... data) const
67  {
68  TContainer functionsCopy = *_functions; // copy _functions because f can erase itself from the _functions
69  for (auto f : functionsCopy)
70  {
71  f(data...);
72  }
73  return *this;
74  }
75 
76  TIterator push_front(TFunction f)
77  {
78  return _functions->insert(_functions->begin(), f);
79  }
80 
81  TIterator push_back(TFunction f)
82  {
83  return _functions->insert(_functions->end(), f);
84  }
85 
86  void erase(TIterator it)
87  {
88  _functions->erase(it);
89  }
90 
91  void clear()
92  {
93  _functions->clear();
94  }
95 
96  std::size_t size() const
97  {
98  return _functions->size();
99  }
100 
101  operator bool() const
102  {
103  return (_functions->size() > 0);
104  }
105 
106 #pragma endregion
107 
108  private:
109 
110 #pragma region private_members
111 
112  std::shared_ptr<TContainer> _functions = std::make_shared<TContainer>();
113 
114 #pragma endregion
115  };
116 
118  template<>
119  class Action<void>
120  {
121  public:
122 
123  using TFunction = std::function<void()>;
124  using TContainer = std::list<TFunction>;
125  using TIterator = TContainer::iterator;
126  using TAction = Action<void>;
127 
128 #pragma region public_methods
129 
130  Action()
131  {
132  }
133 
134  Action(TFunction f)
135  {
136  _functions->push_back(f);
137  }
138 
139  virtual ~Action()
140  {
141  }
142 
143  Action(const TAction& other)
144  : _functions(other._functions)
145  {
146  }
147 
148  Action(TAction&& other)
149  : _functions(std::move(other._functions))
150  {
151  }
152 
153  TAction& operator=(const TAction& other)
154  {
155  _functions = other._functions;
156  return *this;
157  }
158 
159  TAction& operator=(TFunction f)
160  {
161  _functions->clear();
162  _functions->push_back(f);
163  return *this;
164  }
165 
166  TAction& operator+=(TFunction f)
167  {
168  _functions->push_back(f);
169  return *this;
170  }
171 
172  const TAction& operator()() const
173  {
174  TContainer functionsCopy = *_functions; // copy _functions because f can erase itself from the _functions
175  for (auto f : functionsCopy)
176  {
177  f();
178  }
179  return *this;
180  }
181 
182  TIterator push_front(TFunction f)
183  {
184  return _functions->insert(_functions->begin(), f);
185  }
186 
187  TIterator push_back(TFunction f)
188  {
189  return _functions->insert(_functions->end(), f);
190  }
191 
192  void erase(TIterator it)
193  {
194  _functions->erase(it);
195  }
196 
197  void clear()
198  {
199  _functions->clear();
200  }
201 
202  std::size_t size() const
203  {
204  return _functions->size();
205  }
206 
207  operator bool() const
208  {
209  return (_functions->size() > 0);
210  }
211 
212 #pragma endregion
213 
214  private:
215 
216 #pragma region private_members
217 
218  std::shared_ptr<TContainer> _functions = std::make_shared<TContainer>();
219 
220 #pragma endregion
221  };
222 }
Aggregates procedures to be run simultaneously.
Definition: Action.h:13
Aggregates procedure pointers to be run simultaneously.
Definition: Action.h:119