RTC Toolkit 5.1.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
151#pragma GCC diagnostic push
152#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
153
155 // The following is part of the legacy API:
156
157public:
166 class [[deprecated("Use BatchRequest instead.")]] SubscribeRequest {
167 public:
172 class [[deprecated]] Parameters {
173 public:
175 using CallbackType = std::function<void(const DataPointPath&)>;
176
177 Parameters() = default;
178
190 explicit Parameters(const DataPointPath& path,
191 bool value_requested,
192 void* buffer,
193 const std::type_info& type,
194 const CallbackType& removed_callback,
195 const CallbackType& newvalue_callback)
196 : m_path(path)
197 , m_value_requested(value_requested)
198 , m_buffer(buffer)
199 , m_type(&type)
200 , m_removed_callback(removed_callback)
201 , m_newvalue_callback(newvalue_callback) {
202 }
203
204 explicit Parameters(const DataPointPath& path,
205 void* buffer,
206 const std::type_info& type,
207 const CallbackType& removed_callback,
208 const CallbackType& newvalue_callback)
209 : Parameters(path, true, buffer, type, removed_callback, newvalue_callback) {
210 }
211
212 [[deprecated]] inline const DataPointPath& GetPath() const {
213 return m_path;
214 };
215 [[deprecated]] inline const bool GetValueRequested() const {
216 return m_value_requested;
217 }
218 [[deprecated]] inline void* GetBuffer() const {
219 return m_buffer;
220 };
221 [[deprecated]] inline const std::type_info& GetType() const {
222 return *m_type;
223 };
224 [[deprecated]] inline const CallbackType& GetRemovedCallback() const {
225 return m_removed_callback;
226 };
227 [[deprecated]] inline const CallbackType& GetNewValueCallback() const {
228 return m_newvalue_callback;
229 };
230
231 private:
234
236 bool m_value_requested;
237
239 void* m_buffer;
240
242 const std::type_info* m_type;
243
245 CallbackType m_removed_callback;
246 CallbackType m_newvalue_callback;
247 };
248
249 template <typename F>
250 [[deprecated("Use BatchRequest instead.")]] void
251 AddRemovedHandler(const DataPointPath& path, F handler);
252
253 template <typename T, typename F>
254 [[deprecated("Use BatchRequest instead.")]] void
255 AddNewValueHandler(const DataPointPath& path, T& buffer, F handler);
256
257 template <typename F>
258 [[deprecated("Use BatchRequest instead.")]] void
259 AddValueChangedHandler(const DataPointPath& path, F handler);
260
261 [[deprecated("Use BatchRequest instead.")]] inline const std::vector<Parameters>&
262 GetParams() const {
263 return m_params;
264 };
265
266 private:
267 std::vector<Parameters> m_params;
268 };
269
278 class [[deprecated("Use BatchRequest instead.")]] UnsubscribeRequest {
279 public:
284 class [[deprecated]] Parameters {
285 public:
286 Parameters() = default;
287
296 explicit Parameters(const DataPointPath& path,
297 const bool unsubscribe_removed,
298 const bool unsubscribe_newvalue)
299 : m_path(path)
300 , m_unsubscribe_removed(unsubscribe_removed)
301 , m_unsubscribe_newvalue(unsubscribe_newvalue) {
302 }
303
304 [[deprecated]] inline const DataPointPath& GetPath() const {
305 return m_path;
306 };
307 [[deprecated]] inline bool GetUnsubscribeRemoved() const {
308 return m_unsubscribe_removed;
309 };
310 [[deprecated]] inline bool GetUnsubscribeNewValue() const {
311 return m_unsubscribe_newvalue;
312 };
313
314 private:
317
319 bool m_unsubscribe_removed;
320
322 bool m_unsubscribe_newvalue;
323 };
324
337 [[deprecated("Use BatchRequest instead.")]] void
338 AddRemovedHandler(const DataPointPath& path);
339
352 [[deprecated("Use BatchRequest instead.")]] void
353 AddNewValueHandler(const DataPointPath& path);
354
355 [[deprecated("Use BatchRequest instead.")]] inline const std::vector<Parameters>&
356 GetParams() const {
357 return m_params;
358 };
359
360 private:
361 std::vector<Parameters> m_params;
362 };
363
406 [[deprecated("Use SendRequest instead.")]] virtual RepositoryIf::Response
407 SendSubscribeRequest(const SubscribeRequest& request) const;
408
442 [[deprecated("Use SendRequest instead.")]] virtual RepositoryIf::Response
443 SendUnsubscribeRequest(const UnsubscribeRequest& request) const;
444
446 using NotifyHandler [[deprecated]] = std::function<void(const DataPointPath& path)>;
447
449 template <typename T>
450 using ValueHandler [[deprecated]] = std::function<void(const DataPointPath& path, T& value)>;
451
452 template <
453 typename T,
454 typename F,
455 std::enable_if_t<not std::is_convertible_v<T, RepositorySubscriberIf::NotifyCallbackType>,
456 bool> = true>
457 [[deprecated]] void Subscribe(const DataPointPath& path, T& buffer, F handler) const;
458
482 template <typename F>
483 [[deprecated]] void Subscribe(const DataPointPath& path, F handler) const {
484 SubscribeRequest subscribe;
485 if constexpr (std::is_convertible_v<F, NotifyHandler>) {
486 subscribe.AddValueChangedHandler(path, handler);
487 } else {
488 // later we intend to support more callback types
489 static_assert(std::is_convertible_v<F, NotifyHandler>, "Feature not supported");
490 }
491 SendSubscribeRequest(subscribe).Wait();
492 }
493
511 [[deprecated]] void Unsubscribe(const DataPointPath& path) const;
512
513private:
514 // This is a workaround to support Python bindings. We need to associate certain extra objects
515 // and buffers under the hood that have to live as long as an instance of this class does.
516 friend class PyRepositorySubscriberIf;
517 std::any m_extra_objects;
518
520 mutable std::mutex m_subscriptions_mutex;
521
522 // List of subscriptions made from the legacy API.
523 mutable std::map<DataPointPath, std::vector<SubscriptionId>> m_new_value_subscriptions;
524 mutable std::map<DataPointPath, std::vector<SubscriptionId>> m_removed_subscriptions;
525
526#pragma GCC diagnostic pop
527};
528
529} // namespace rtctk::componentFramework
530
532
533#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:1370
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:172
const std::type_info & GetType() const
Definition repositorySubscriberIf.hpp:221
void * GetBuffer() const
Definition repositorySubscriberIf.hpp:218
const CallbackType & GetRemovedCallback() const
Definition repositorySubscriberIf.hpp:224
const DataPointPath & GetPath() const
Definition repositorySubscriberIf.hpp:212
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:190
Parameters(const DataPointPath &path, void *buffer, const std::type_info &type, const CallbackType &removed_callback, const CallbackType &newvalue_callback)
Definition repositorySubscriberIf.hpp:204
std::function< void(const DataPointPath &)> CallbackType
callback type that is used internally
Definition repositorySubscriberIf.hpp:175
const bool GetValueRequested() const
Definition repositorySubscriberIf.hpp:215
const CallbackType & GetNewValueCallback() const
Definition repositorySubscriberIf.hpp:227
A request object to pass information about datapoints to subscribe to.
Definition repositorySubscriberIf.hpp:166
const std::vector< Parameters > & GetParams() const
Definition repositorySubscriberIf.hpp:262
void AddValueChangedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to value change notifications for a particular datapoint.
Definition repositorySubscriberIf.ipp:202
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:284
const DataPointPath & GetPath() const
Definition repositorySubscriberIf.hpp:304
bool GetUnsubscribeRemoved() const
Definition repositorySubscriberIf.hpp:307
bool GetUnsubscribeNewValue() const
Definition repositorySubscriberIf.hpp:310
Parameters(const DataPointPath &path, const bool unsubscribe_removed, const bool unsubscribe_newvalue)
Allows to explicitly construct a complete parameters structure.
Definition repositorySubscriberIf.hpp:296
A request object to pass information about datapoints to unsubscribe from.
Definition repositorySubscriberIf.hpp:278
const std::vector< Parameters > & GetParams() const
Definition repositorySubscriberIf.hpp:356
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:483
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:168
Header file for RepositoryIf and related base classes.
Implementation file for RepositorySubscriberIf template methods and related classes.
Definition repositorySubscriberIf.ipp:41