RTC Toolkit 4.0.1
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
16#include <any>
17
19
21
30public:
38 public:
43 class Parameters {
44 public:
46 using CallbackType = std::function<void(const DataPointPath&)>;
47
48 Parameters() = default;
49
61 explicit Parameters(const DataPointPath& path,
62 bool value_requested,
63 void* buffer,
64 const std::type_info& type,
65 const CallbackType& removed_callback,
66 const CallbackType& newvalue_callback)
67 : m_path(path)
68 , m_value_requested(value_requested)
69 , m_buffer(buffer)
70 , m_type(&type)
71 , m_removed_callback(removed_callback)
72 , m_newvalue_callback(newvalue_callback) {
73 }
74
75 explicit Parameters(const DataPointPath& path,
76 void* buffer,
77 const std::type_info& type,
78 const CallbackType& removed_callback,
79 const CallbackType& newvalue_callback)
80 : Parameters(path, true, buffer, type, removed_callback, newvalue_callback) {
81 }
82
83 inline const DataPointPath& GetPath() const {
84 return m_path;
85 };
86 inline const bool GetValueRequested() const {
87 return m_value_requested;
88 }
89 inline void* GetBuffer() const {
90 return m_buffer;
91 };
92 inline const std::type_info& GetType() const {
93 return *m_type;
94 };
95 inline const CallbackType& GetRemovedCallback() const {
96 return m_removed_callback;
97 };
98 inline const CallbackType& GetNewValueCallback() const {
99 return m_newvalue_callback;
100 };
101
102 private:
104 DataPointPath m_path;
105
107 bool m_value_requested;
108
110 void* m_buffer;
111
113 const std::type_info* m_type;
114
116 CallbackType m_removed_callback;
117 CallbackType m_newvalue_callback;
118 };
119
120 template <typename F>
121 void AddRemovedHandler(const DataPointPath& path, F handler);
122
123 template <typename T, typename F>
124 void AddNewValueHandler(const DataPointPath& path, T& buffer, F handler);
125
126 template <typename F>
127 void AddValueChangedHandler(const DataPointPath& path, F handler);
128
129 inline const std::vector<Parameters>& GetParams() const {
130 return m_params;
131 };
132
133 private:
134 std::vector<Parameters> m_params;
135 };
136
144 public:
150 public:
151 Parameters() = default;
152
161 explicit Parameters(const DataPointPath& path,
162 const bool unsubscribe_removed,
163 const bool unsubscribe_newvalue)
164 : m_path(path)
165 , m_unsubscribe_removed(unsubscribe_removed)
166 , m_unsubscribe_newvalue(unsubscribe_newvalue) {
167 }
168
169 inline const DataPointPath& GetPath() const {
170 return m_path;
171 };
172 inline bool GetUnsubscribeRemoved() const {
173 return m_unsubscribe_removed;
174 };
175 inline bool GetUnsubscribeNewValue() const {
176 return m_unsubscribe_newvalue;
177 };
178
179 private:
181 DataPointPath m_path;
182
184 bool m_unsubscribe_removed;
185
187 bool m_unsubscribe_newvalue;
188 };
189
202 void AddRemovedHandler(const DataPointPath& path);
203
216 void AddNewValueHandler(const DataPointPath& path);
217
218 inline const std::vector<Parameters>& GetParams() const {
219 return m_params;
220 };
221
222 private:
223 std::vector<Parameters> m_params;
224 };
225
226 virtual ~RepositorySubscriberIf();
227
269
303
305 using NotifyHandler = std::function<void(DataPointPath const& path)>;
306
308 template <typename T>
309 using ValueHandler = std::function<void(DataPointPath const& path, T& value)>;
310
311 template <typename T, typename F>
312 void Subscribe(const DataPointPath& path, T& buffer, F handler) const;
313
335 template <typename F>
336 void Subscribe(const DataPointPath& path, F handler) const {
337 SubscribeRequest subscribe;
338 if constexpr (std::is_convertible_v<F, NotifyHandler>) {
339 subscribe.AddValueChangedHandler(path, handler);
340 } else {
341 // later we intend to support more callback types
342 static_assert(std::is_convertible_v<F, NotifyHandler>, "Feature not supported");
343 }
344 SendSubscribeRequest(subscribe).Wait();
345 }
346
362 void Unsubscribe(const DataPointPath& path) const;
363
364private:
365 // This is a workaround to support Python bindings. We need to associate certain extra objects
366 // and buffers under the hood that have to live as long as an instance of this class does.
368 std::any m_extra_objects;
369};
370
391template <typename F>
393 F handler) {
394 static_assert(
395 std::is_convertible_v<F, std::function<void(const DataPointPath&)>>,
396 "The handler callback must have a signature compatible with void(const DataPointPath&).");
397 m_params.push_back(Parameters(path, nullptr, typeid(void), handler, nullptr));
398}
399
428template <typename T, typename F>
430 T& buffer,
431 F handler) {
432 static_assert(std::is_convertible_v<F, ValueHandler<T>>,
433 "The handler callback must have a signature compatible with void(const"
434 " DataPointPath&, T&).");
435 // We use a lambda here to have type erasure and allow storing callbacks for different types in
436 // the m_newvalue_callback.
437 auto callback = [handler, &buffer](const DataPointPath& path) -> void {
438 handler(path, buffer);
439 };
440 m_params.push_back(Parameters(path, true, &buffer, typeid(buffer), nullptr, callback));
441}
442
462template <typename F>
464 F handler) {
465 static_assert(std::is_convertible_v<F, NotifyHandler>,
466 "The handler callback must have a signature compatible with void(const"
467 " DataPointPath&).");
468 // We use a lambda here to have type erasure and allow storing callbacks for different types in
469 // the m_newvalue_callback.
470 auto callback = [handler](const DataPointPath& path) -> void { handler(path); };
471 m_params.push_back(Parameters(path, false, nullptr, typeid(void), nullptr, callback));
472}
473
497template <typename T, typename F>
498void RepositorySubscriberIf::Subscribe(const DataPointPath& path, T& buffer, F handler) const {
499 SubscribeRequest subscribe;
500 subscribe.AddNewValueHandler(path, buffer, handler);
501 SendSubscribeRequest(subscribe).Wait();
502}
503
504} // namespace rtctk::componentFramework
505
506#endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
This class provides a wrapper for a data point path.
Definition: dataPointPath.hpp:73
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:197
void Wait()
Waits for the request that was sent to the repository to complete.
Definition: repositoryIf.cpp:32
A structure to hold the arguments passed to the Add method.
Definition: repositorySubscriberIf.hpp:43
const std::type_info & GetType() const
Definition: repositorySubscriberIf.hpp:92
void * GetBuffer() const
Definition: repositorySubscriberIf.hpp:89
const CallbackType & GetRemovedCallback() const
Definition: repositorySubscriberIf.hpp:95
const DataPointPath & GetPath() const
Definition: repositorySubscriberIf.hpp:83
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:61
Parameters(const DataPointPath &path, void *buffer, const std::type_info &type, const CallbackType &removed_callback, const CallbackType &newvalue_callback)
Definition: repositorySubscriberIf.hpp:75
std::function< void(const DataPointPath &)> CallbackType
callback type that is used internally
Definition: repositorySubscriberIf.hpp:46
const bool GetValueRequested() const
Definition: repositorySubscriberIf.hpp:86
const CallbackType & GetNewValueCallback() const
Definition: repositorySubscriberIf.hpp:98
A request object to pass information about datapoints to subscribe to.
Definition: repositorySubscriberIf.hpp:37
const std::vector< Parameters > & GetParams() const
Definition: repositorySubscriberIf.hpp:129
void AddNewValueHandler(const DataPointPath &path, T &buffer, F handler)
Adds a request to subscribe to new data values for a particular datapoint.
Definition: repositorySubscriberIf.hpp:429
void AddRemovedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to removal notifications for a particular datapoint.
Definition: repositorySubscriberIf.hpp:392
void AddValueChangedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to value change notifications for a particular datapoint.
Definition: repositorySubscriberIf.hpp:463
A structure to hold the arguments passed with one of the Add methods.
Definition: repositorySubscriberIf.hpp:149
const DataPointPath & GetPath() const
Definition: repositorySubscriberIf.hpp:169
bool GetUnsubscribeRemoved() const
Definition: repositorySubscriberIf.hpp:172
bool GetUnsubscribeNewValue() const
Definition: repositorySubscriberIf.hpp:175
Parameters(const DataPointPath &path, const bool unsubscribe_removed, const bool unsubscribe_newvalue)
Allows to explicitly construct a complete parameters structure.
Definition: repositorySubscriberIf.hpp:161
A request object to pass information about datapoints to unsubscribe from.
Definition: repositorySubscriberIf.hpp:143
void AddRemovedHandler(const DataPointPath &path)
Adds a request to unsubscribe from removal notifications.
Definition: repositorySubscriberIf.cpp:20
void AddNewValueHandler(const DataPointPath &path)
Adds a request to unsubscribe from new value notifications for a datapoint.
Definition: repositorySubscriberIf.cpp:24
const std::vector< Parameters > & GetParams() const
Definition: repositorySubscriberIf.hpp:218
Abstract interface providing subscription facilities for a repository.
Definition: repositorySubscriberIf.hpp:29
virtual RepositoryIf::Response SendUnsubscribeRequest(const UnsubscribeRequest &request) const =0
This is called to asynchronously send a request to unsubscribe from various notifications.
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:336
friend class PyRepositorySubscriberIf
Definition: repositorySubscriberIf.hpp:367
virtual RepositoryIf::Response SendSubscribeRequest(const SubscribeRequest &request) const =0
This is called to asynchronously send a subscription request for datapoints.
std::function< void(DataPointPath const &path)> NotifyHandler
Signature of notification callbacks.
Definition: repositorySubscriberIf.hpp:305
virtual ~RepositorySubscriberIf()
Definition: repositorySubscriberIf.cpp:17
void Subscribe(const DataPointPath &path, T &buffer, F handler) const
A convenience template function that will register a callback to receive new datapoint values in a sy...
Definition: repositorySubscriberIf.hpp:498
void Unsubscribe(const DataPointPath &path) const
A simple convenience function that will deregister all callbacks for receiving new datapoint values.
Definition: repositorySubscriberIf.cpp:28
std::function< void(DataPointPath const &path, T &value)> ValueHandler
Signature of value callbacks.
Definition: repositorySubscriberIf.hpp:309
Definition: commandReplier.cpp:22
Header file for RepositoryIf and related base classes.