ADTF
Loading...
Searching...
No Matches
timer_decl.h
Go to the documentation of this file.
1
14
15#ifndef A_UTIL_UTIL_SYSTEM_TIMER_DECL_INCLUDED
16#define A_UTIL_UTIL_SYSTEM_TIMER_DECL_INCLUDED
17
18#include <cstdint>
19
20namespace a_util {
21namespace result {
22namespace detail {
23
24// forward declare private reference counter
25template <typename T, typename U>
27
28} // namespace detail
29} // namespace result
30
31namespace experimental {
32// forward declaration
33template <typename T>
34class NullaryDelegate;
35
36} // namespace experimental
37namespace system {
45class Timer {
46public:
49
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
124private:
125 Timer(const Timer&); // = delete;
126 Timer& operator=(const Timer&); // = delete;
128
129private: // temporary IntrusivePtr implementation until it can be released publically
130 template <typename T>
131 class IntrusivePtr {
132 using reference_counted_object_pointer =
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
232private:
233 struct Implementation;
234 IntrusivePtr<Implementation> _impl;
235};
236
237} // namespace system
238} // namespace a_util
239
243
244#endif // A_UTIL_UTIL_SYSTEM_TIMER_DECL_INCLUDED
Definition delegate_decl.h:129
Default implementation of a reference counter.
Definition reference_counted_object.h:33
Timer()
Default CTOR.
void setCallback(void(*function)())
~Timer()
DTOR - Calls stop()
std::uint64_t getPeriod() const
Timer(std::uint64_t period_us, void(*function)())
bool isRunning() const
void setPeriod(std::uint64_t period_us)
Serves as component for features which are considered experimental.
Definition base.h:26
Serves as namespace for result implementation details.
Definition result.h:22
Serves as component for functionality handling error and return types.
Definition result.h:20
Serves as component for portable OS (Windows, Linux, ...) functionality.
Definition system.h:20
Serves as the root component, with common functionality documented in core functionality.
Definition base.h:24