RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
repositorySubscriberIf.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
14#define RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
15
17
19
28public:
29 // TODO: flesh out the details of this error type
30 // this likely will map to follwoing dds reader callbacks:
31 // - on sample_rejected
32 // - on_sample_lost
33 // - on_request_deadline_missed
34 // - on request_incompatible_qos
36 public:
37 using RtctkException::RtctkException;
38 };
39
41 public:
42 SequenceIdMissmatch(const DataPointPath& path, RtcUInt64 expected, RtcUInt64 actual);
43 };
44
45 template <typename T>
46 using ValueCallbackType = std::function<void(const DataPointPath&, T&, const MetaData&)>;
47
48 using NotifyCallbackType = std::function<void(const DataPointPath&, const MetaData&)>;
49 using ErrorCallbackType = std::function<void(std::exception_ptr)>;
50
51 using SubscriptionId = size_t;
52
64 class RTCTK_API Subscription final {
65 public:
66 using UnsubscribeMethod = std::function<void()>;
67
70
71 Subscription(const Subscription&) = delete;
73
74 Subscription(Subscription&&) noexcept;
75 Subscription& operator=(Subscription&&) noexcept;
76
77 void Unsubscribe();
78
79 private:
80 UnsubscribeMethod m_unsubscribe;
81 };
82
84 public:
86
88
89 void Unsubscribe(SubscriptionId) const;
90 };
91
93 virtual ~RepositorySubscriberIf() = default;
94
117 template <
118 typename T = void,
119 typename F,
120 std::enable_if_t<
121 std::is_convertible_v<F, NotifyCallbackType> or
122 (not std::is_same_v<T, void> and
123 std::is_convertible_v<
124 F,
125 ValueCallbackType<std::conditional_t<std::is_same_v<T, void>, std::byte, T>>>),
126 bool> = true>
127 Subscription Subscribe(const DataPointPath& path,
128 const F& cb_value,
129 const ErrorCallbackType& cb_error) const;
130
147 Subscription Subscribe(const CallbackType& cb_created,
148 const CallbackType& cb_deleted,
149 const ErrorCallbackType& cb_error) const;
150
152 // The following is part of the legacy API:
153
154public:
162 public:
168 public:
170 using CallbackType = std::function<void(const DataPointPath&)>;
171
172 Parameters() = default;
173
185 explicit Parameters(const DataPointPath& path,
186 bool value_requested,
187 void* buffer,
188 const std::type_info& type,
189 const CallbackType& removed_callback,
190 const CallbackType& newvalue_callback)
191 : m_path(path)
192 , m_value_requested(value_requested)
193 , m_buffer(buffer)
194 , m_type(&type)
195 , m_removed_callback(removed_callback)
196 , m_newvalue_callback(newvalue_callback) {
197 }
198
199 explicit Parameters(const DataPointPath& path,
200 void* buffer,
201 const std::type_info& type,
202 const CallbackType& removed_callback,
203 const CallbackType& newvalue_callback)
204 : Parameters(path, true, buffer, type, removed_callback, newvalue_callback) {
205 }
206
207 inline const DataPointPath& GetPath() const {
208 return m_path;
209 };
210 inline const bool GetValueRequested() const {
211 return m_value_requested;
212 }
213 inline void* GetBuffer() const {
214 return m_buffer;
215 };
216 inline const std::type_info& GetType() const {
217 return *m_type;
218 };
219 inline const CallbackType& GetRemovedCallback() const {
220 return m_removed_callback;
221 };
222 inline const CallbackType& GetNewValueCallback() const {
223 return m_newvalue_callback;
224 };
225
226 private:
229
231 bool m_value_requested;
232
234 void* m_buffer;
235
237 const std::type_info* m_type;
238
240 CallbackType m_removed_callback;
241 CallbackType m_newvalue_callback;
242 };
243
244 template <typename F>
245 void AddRemovedHandler(const DataPointPath& path, F handler);
246
247 template <typename T, typename F>
248 void AddNewValueHandler(const DataPointPath& path, T& buffer, F handler);
249
250 template <typename F>
251 void AddValueChangedHandler(const DataPointPath& path, F handler);
252
253 inline const std::vector<Parameters>& GetParams() const {
254 return m_params;
255 };
256
257 private:
258 std::vector<Parameters> m_params;
259 };
260
268 public:
274 public:
275 Parameters() = default;
276
285 explicit Parameters(const DataPointPath& path,
286 const bool unsubscribe_removed,
287 const bool unsubscribe_newvalue)
288 : m_path(path)
289 , m_unsubscribe_removed(unsubscribe_removed)
290 , m_unsubscribe_newvalue(unsubscribe_newvalue) {
291 }
292
293 inline const DataPointPath& GetPath() const {
294 return m_path;
295 };
296 inline bool GetUnsubscribeRemoved() const {
297 return m_unsubscribe_removed;
298 };
299 inline bool GetUnsubscribeNewValue() const {
300 return m_unsubscribe_newvalue;
301 };
302
303 private:
306
308 bool m_unsubscribe_removed;
309
311 bool m_unsubscribe_newvalue;
312 };
313
326 void AddRemovedHandler(const DataPointPath& path);
327
340 void AddNewValueHandler(const DataPointPath& path);
341
342 inline const std::vector<Parameters>& GetParams() const {
343 return m_params;
344 };
345
346 private:
347 std::vector<Parameters> m_params;
348 };
349
390 virtual RepositoryIf::Response SendSubscribeRequest(const SubscribeRequest& request) const;
391
423 virtual RepositoryIf::Response SendUnsubscribeRequest(const UnsubscribeRequest& request) const;
424
426 using NotifyHandler = std::function<void(const DataPointPath& path)>;
427
429 template <typename T>
430 using ValueHandler = std::function<void(const DataPointPath& path, T& value)>;
431
432 template <
433 typename T,
434 typename F,
435 std::enable_if_t<not std::is_convertible_v<T, RepositorySubscriberIf::NotifyCallbackType>,
436 bool> = true>
437 void Subscribe(const DataPointPath& path, T& buffer, F handler) const;
438
460 template <typename F>
461 void Subscribe(const DataPointPath& path, F handler) const {
462 SubscribeRequest subscribe;
463 if constexpr (std::is_convertible_v<F, NotifyHandler>) {
464 subscribe.AddValueChangedHandler(path, handler);
465 } else {
466 // later we intend to support more callback types
467 static_assert(std::is_convertible_v<F, NotifyHandler>, "Feature not supported");
468 }
469 SendSubscribeRequest(subscribe).Wait();
470 }
471
487 void Unsubscribe(const DataPointPath& path) const;
488
489private:
490 // This is a workaround to support Python bindings. We need to associate certain extra objects
491 // and buffers under the hood that have to live as long as an instance of this class does.
492 friend class PyRepositorySubscriberIf;
493 std::any m_extra_objects;
494
496 mutable std::mutex m_subscriptions_mutex;
497
498 // List of subscriptions made from the legacy API.
499 mutable std::map<DataPointPath, std::vector<SubscriptionId>> m_new_value_subscriptions;
500 mutable std::map<DataPointPath, std::vector<SubscriptionId>> m_removed_subscriptions;
501};
502
503} // namespace rtctk::componentFramework
504
506
507#endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:74
Class for passing/receiving metadata to/from the repository.
Definition repositoryIf.hpp:195
An object used to wait for a request to complete.
Definition repositoryIf.hpp:1354
Abstract interface providing basic read and write facilities to a repository.
Definition repositoryIf.hpp:104
std::function< void(const DataPointPath &)> CallbackType
Signature of the callback functions that can be registered with the repository requests.
Definition repositoryIf.hpp:114
A structure to hold the arguments passed to the Add method.
Definition repositorySubscriberIf.hpp:167
const std::type_info & GetType() const
Definition repositorySubscriberIf.hpp:216
void * GetBuffer() const
Definition repositorySubscriberIf.hpp:213
const CallbackType & GetRemovedCallback() const
Definition repositorySubscriberIf.hpp:219
const DataPointPath & GetPath() const
Definition repositorySubscriberIf.hpp:207
Parameters(const DataPointPath &path, bool value_requested, void *buffer, const std::type_info &type, const CallbackType &removed_callback, const CallbackType &newvalue_callback)
Allows to explicitly construct a complete parameters structure.
Definition repositorySubscriberIf.hpp:185
Parameters(const DataPointPath &path, void *buffer, const std::type_info &type, const CallbackType &removed_callback, const CallbackType &newvalue_callback)
Definition repositorySubscriberIf.hpp:199
std::function< void(const DataPointPath &)> CallbackType
callback type that is used internally
Definition repositorySubscriberIf.hpp:170
const bool GetValueRequested() const
Definition repositorySubscriberIf.hpp:210
const CallbackType & GetNewValueCallback() const
Definition repositorySubscriberIf.hpp:222
A request object to pass information about datapoints to subscribe to.
Definition repositorySubscriberIf.hpp:161
const std::vector< Parameters > & GetParams() const
Definition repositorySubscriberIf.hpp:253
void AddValueChangedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to value change notifications for a particular datapoint.
Definition repositorySubscriberIf.ipp:199
RAII wrapper class used to manage the life-time of individual subscriptions.
Definition repositorySubscriberIf.hpp:64
std::function< void()> UnsubscribeMethod
Definition repositorySubscriberIf.hpp:66
Subscription & operator=(const Subscription &)=delete
A structure to hold the arguments passed with one of the Add methods.
Definition repositorySubscriberIf.hpp:273
const DataPointPath & GetPath() const
Definition repositorySubscriberIf.hpp:293
bool GetUnsubscribeRemoved() const
Definition repositorySubscriberIf.hpp:296
bool GetUnsubscribeNewValue() const
Definition repositorySubscriberIf.hpp:299
Parameters(const DataPointPath &path, const bool unsubscribe_removed, const bool unsubscribe_newvalue)
Allows to explicitly construct a complete parameters structure.
Definition repositorySubscriberIf.hpp:285
A request object to pass information about datapoints to unsubscribe from.
Definition repositorySubscriberIf.hpp:267
const std::vector< Parameters > & GetParams() const
Definition repositorySubscriberIf.hpp:342
Abstract interface providing I/O and additional subscription facilities for a repository.
Definition repositorySubscriberIf.hpp:27
std::function< void(std::exception_ptr)> ErrorCallbackType
Definition repositorySubscriberIf.hpp:49
void Subscribe(const DataPointPath &path, F handler) const
A convenience template function that will register a callback to receive datapoint value change notif...
Definition repositorySubscriberIf.hpp:461
std::function< void(const DataPointPath &path, T &value)> ValueHandler
Signature of value callbacks.
Definition repositorySubscriberIf.hpp:430
std::function< void(const DataPointPath &path)> NotifyHandler
Signature of notification callbacks.
Definition repositorySubscriberIf.hpp:426
std::function< void(const DataPointPath &, T &, const MetaData &)> ValueCallbackType
Definition repositorySubscriberIf.hpp:46
std::function< void(const DataPointPath &, const MetaData &)> NotifyCallbackType
Definition repositorySubscriberIf.hpp:48
size_t SubscriptionId
Definition repositorySubscriberIf.hpp:51
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:211
#define RTCTK_API
Helper to indicate that a class or function must be exported in the public symbol table.
Definition config.hpp:33
Definition commandReplier.cpp:22
uint64_t RtcUInt64
Definition repositoryIf.hpp:61
DataPointPath m_path
Definition populateConfig.cpp:165
Header file for RepositoryIf and related base classes.
Implementation file for RepositorySubscriberIf template methods and related classes.
Definition repositorySubscriberIf.ipp:41