ADTF  3.18.3
timer_decl.h
Go to the documentation of this file.
1 
15 #ifndef A_UTIL_UTIL_SYSTEM_TIMER_DECL_INCLUDED
16 #define A_UTIL_UTIL_SYSTEM_TIMER_DECL_INCLUDED
17 
18 #include <cstdint>
19 
20 namespace a_util {
21 namespace result {
22 namespace detail {
23 
24 // forward declare private reference counter
25 template <typename T, typename U>
26 class ReferenceCountedObject;
27 
28 } // namespace detail
29 } // namespace result
30 
31 namespace experimental {
32 // forward declaration
33 template <typename T>
34 class NullaryDelegate;
35 
36 } // namespace experimental
37 namespace system {
45 class Timer {
46 public:
48  Timer();
49 
51  ~Timer();
52 
61  Timer(std::uint64_t period_us, void (*function)());
62 
74  template <typename M, typename T>
75  Timer(std::uint64_t period_us, M method, T& instance);
76 
81  void setCallback(void (*function)());
82 
90  template <typename M, typename T>
91  void setCallback(M method, T& instance);
92 
98  void setPeriod(std::uint64_t period_us);
99 
104  std::uint64_t getPeriod() const;
105 
110  bool start();
111 
116  bool stop();
117 
122  bool isRunning() const;
123 
124 private:
125  Timer(const Timer&); // = delete;
126  Timer& operator=(const Timer&); // = delete;
128 
129 private: // temporary IntrusivePtr implementation until it can be released publically
130  template <typename T>
131  class IntrusivePtr {
134 
135  public:
136  using value_type = T;
137  using pointer = value_type*;
138  using const_pointer = const value_type*;
139  using reference = value_type&;
140  using const_reference = const value_type&;
141 
142  // construction/destruction
143  explicit IntrusivePtr(reference_counted_object_pointer reference_counted_object)
144  : _reference_counted_object{reference_counted_object}
145  {
146  // nullptr check currently missing on purpose, has to be added if generalized
147  // maybe a nullptr check doesn't make sense in ctor due to move operators setting the
148  // member to nullptr anyway ...
149  addReference();
150  }
151 
152  ~IntrusivePtr() noexcept
153  {
154  removeReference();
155  }
156 
157  // copy constructor
158  IntrusivePtr(const IntrusivePtr& other) : IntrusivePtr{other._reference_counted_object}
159  {
160  }
161 
162  // copy assignment
163  IntrusivePtr& operator=(const IntrusivePtr& other)
164  {
165  removeReference();
166  _reference_counted_object = other._reference_counted_object;
167  addReference();
168  return *this;
169  }
170 
171  // move constructor
172  IntrusivePtr(IntrusivePtr&& other) noexcept
173  : _reference_counted_object{other._reference_counted_object}
174  {
175  other.release();
176  }
177 
178  // move assignment
179  IntrusivePtr& operator=(IntrusivePtr&& other) noexcept
180  {
181  _reference_counted_object = other._reference_counted_object;
182  other.release();
183  return *this;
184  }
185 
186  public: // access/modification
187  // maybe return number of references instead of void?
188  void addReference() const noexcept
189  {
190  _reference_counted_object->addReference();
191  }
192 
193  // maybe return number of references instead of void?
194  void removeReference() const noexcept
195  {
196  _reference_counted_object->removeReference();
197  }
198 
199  // maybe return pointer to member instead of void?
200  void release() noexcept
201  {
202  _reference_counted_object = nullptr;
203  }
204 
205  auto operator->() const noexcept -> const_pointer
206  {
207  return &(_reference_counted_object->getObject());
208  }
209 
210  auto operator->() noexcept -> pointer
211  {
212  return &(_reference_counted_object->getObject());
213  }
214 
215  auto operator*() noexcept -> reference
216  {
217  return *(this->operator->());
218  }
219 
220  auto operator*() const noexcept -> const_reference
221  {
222  return *(this->operator->());
223  }
224 
225  private:
226  reference_counted_object_pointer _reference_counted_object;
227  }; // IntrusivePtr
228 
229  template <typename T, typename... Args>
230  static auto makeIntrusive(Args&&... args) -> IntrusivePtr<T>;
231 
232 private:
233  struct Implementation;
235 };
236 
237 } // namespace system
238 } // namespace a_util
239 
244 #endif // A_UTIL_UTIL_SYSTEM_TIMER_DECL_INCLUDED
General delegate template: encapsulates a function or a method without a parameter.
virtual void removeReference() const
Decrease the reference count by 1, deleting *this if it was the last reference.
virtual void addReference() const
Increase the reference count by 1.
virtual const Interface & getObject() const
Get a reference to the handled object's interface - provided for const correctness.
Periodic timer running in a separate thread.
Definition: timer_decl.h:45
~Timer()
DTOR - Calls stop()
void setCallback(void(*function)())
Set the callback for the timer.
bool stop()
Stop the timer - blocks until the callback returns.
Timer()
Default CTOR.
std::uint64_t getPeriod() const
Get the currently configured period of the timer.
bool start()
Start the timer.
Timer(std::uint64_t period_us, void(*function)())
CTOR for functions as callbacks.
bool isRunning() const
Check whether the timer is currently running.
void setPeriod(std::uint64_t period_us)
Set the period used by this timer, restarting the timer if already running.
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24
Private implementation for Timer class.