RTC Toolkit  2.0.0
repositorySubscriberIf.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
14 
15 #include <any>
16 
18 
19 namespace rtctk::componentFramework {
20 
27 public:
34  public:
38  class Parameters {
39  public:
40  Parameters() = default;
41 
52  explicit Parameters(
53  const DataPointPath& path,
54  void* buffer,
55  const std::type_info& type,
56  const std::function<void(const DataPointPath&)>& removed_callback,
57  const std::function<void(const DataPointPath&)>& newvalue_callback)
58  : m_path(path)
59  , m_buffer(buffer)
60  , m_type(&type)
61  , m_removed_callback(removed_callback)
62  , m_newvalue_callback(newvalue_callback) {
63  }
64 
65  inline const DataPointPath& GetPath() const {
66  return m_path;
67  };
68  inline void* GetBuffer() const {
69  return m_buffer;
70  };
71  inline const std::type_info& GetType() const {
72  return *m_type;
73  };
74  inline const std::function<void(const DataPointPath&)>& GetRemovedCallback() const {
75  return m_removed_callback;
76  };
77  inline const std::function<void(const DataPointPath&)>& GetNewValueCallback() const {
78  return m_newvalue_callback;
79  };
80 
81  private:
83  DataPointPath m_path;
84 
86  void* m_buffer;
87 
89  const std::type_info* m_type;
90 
92  std::function<void(const DataPointPath&)> m_removed_callback;
93  std::function<void(const DataPointPath&)> m_newvalue_callback;
94  };
95 
96  template <typename F>
97  void AddRemovedHandler(const DataPointPath& path, F handler);
98 
99  template <typename T, typename F>
100  void AddNewValueHandler(const DataPointPath& path, T& buffer, F handler);
101 
102  inline const std::vector<Parameters>& GetParams() const {
103  return m_params;
104  };
105 
106  private:
107  std::vector<Parameters> m_params;
108  };
109 
116  public:
120  class Parameters {
121  public:
122  Parameters() = default;
123 
132  explicit Parameters(const DataPointPath& path,
133  const bool unsubscribe_removed,
134  const bool unsubscribe_newvalue)
135  : m_path(path)
136  , m_unsubscribe_removed(unsubscribe_removed)
137  , m_unsubscribe_newvalue(unsubscribe_newvalue) {
138  }
139 
140  inline const DataPointPath& GetPath() const {
141  return m_path;
142  };
143  inline bool GetUnsubscribeRemoved() const {
144  return m_unsubscribe_removed;
145  };
146  inline bool GetUnsubscribeNewValue() const {
147  return m_unsubscribe_newvalue;
148  };
149 
150  private:
152  DataPointPath m_path;
153 
155  bool m_unsubscribe_removed;
156 
158  bool m_unsubscribe_newvalue;
159  };
160 
173  void AddRemovedHandler(const DataPointPath& path);
174 
187  void AddNewValueHandler(const DataPointPath& path);
188 
189  inline const std::vector<Parameters>& GetParams() const {
190  return m_params;
191  };
192 
193  private:
194  std::vector<Parameters> m_params;
195  };
196 
197  virtual ~RepositorySubscriberIf();
198 
236 
268  virtual RepositoryIf::Response
269  SendUnsubscribeRequest(const UnsubscribeRequest& request) const = 0;
270 
271  template <typename T, typename F>
272  void Subscribe(const DataPointPath& path, T& buffer, F handler) const;
273 
289  void Unsubscribe(const DataPointPath& path) const;
290 
291 private:
292  // This is a workaround to support Python bindings. We need to associate certain extra objects
293  // and buffers under the hood that have to live as long as an instance of this class does.
295  std::any m_extra_objects;
296 };
297 
316 template <typename F>
318  F handler) {
319  m_params.push_back(Parameters(path, nullptr, typeid(void), handler, nullptr));
320 }
321 
346 template <typename T, typename F>
348  T& buffer,
349  F handler) {
350  // We use a lambda here to have type erasure and allow storing callbacks for different types in
351  // the m_newvalue_callback.
352  auto callback = [handler, &buffer](const DataPointPath& path) -> void {
353  handler(path, buffer);
354  };
355  m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr, callback));
356 }
357 
376 template <typename T, typename F>
377 void RepositorySubscriberIf::Subscribe(const DataPointPath& path, T& buffer, F handler) const {
378  SubscribeRequest subscribe;
379  subscribe.AddNewValueHandler(path, buffer, handler);
380  SendSubscribeRequest(subscribe).Wait();
381 }
382 
383 } // namespace rtctk::componentFramework
384 
385 #endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::GetPath
const DataPointPath & GetPath() const
Definition: repositorySubscriberIf.hpp:140
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::GetRemovedCallback
const std::function< void(const DataPointPath &)> & GetRemovedCallback() const
Definition: repositorySubscriberIf.hpp:74
rtctk::componentFramework::RepositorySubscriberIf::SendSubscribeRequest
virtual RepositoryIf::Response SendSubscribeRequest(const SubscribeRequest &request) const =0
This is called to asynchronously send a subscription request for datapoints.
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::GetNewValueCallback
const std::function< void(const DataPointPath &)> & GetNewValueCallback() const
Definition: repositorySubscriberIf.hpp:77
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::AddRemovedHandler
void AddRemovedHandler(const DataPointPath &path)
Adds a request to unsubscribe from removal notifications.
Definition: repositorySubscriberIf.cpp:19
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::AddNewValueHandler
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:347
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositorySubscriberIf.hpp:189
rtctk::componentFramework::RepositorySubscriberIf::~RepositorySubscriberIf
virtual ~RepositorySubscriberIf()
Definition: repositorySubscriberIf.cpp:16
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::GetType
const std::type_info & GetType() const
Definition: repositorySubscriberIf.hpp:71
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters
A structure to hold the arguments passed with one of the Add methods.
Definition: repositorySubscriberIf.hpp:120
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest
A request object to pass information about datapoints to subscribe to.
Definition: repositorySubscriberIf.hpp:33
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::GetUnsubscribeRemoved
bool GetUnsubscribeRemoved() const
Definition: repositorySubscriberIf.hpp:143
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::GetBuffer
void * GetBuffer() const
Definition: repositorySubscriberIf.hpp:68
repositoryIf.hpp
Header file for RepositoryIf and related base classes.
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest
A request object to pass information about datapoints to unsubscribe from.
Definition: repositorySubscriberIf.hpp:115
rtctk::componentFramework::RepositoryIf::Response::Wait
void Wait()
Waits for the request that was sent to the repository to complete.
Definition: repositoryIf.cpp:31
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::AddRemovedHandler
void AddRemovedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to removal notifications for a particular datapoint.
Definition: repositorySubscriberIf.hpp:317
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::GetUnsubscribeNewValue
bool GetUnsubscribeNewValue() const
Definition: repositorySubscriberIf.hpp:146
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::GetPath
const DataPointPath & GetPath() const
Definition: repositorySubscriberIf.hpp:65
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::Parameters
Parameters(const DataPointPath &path, const bool unsubscribe_removed, const bool unsubscribe_newvalue)
Allows to explicitly construct a complete parameters structure.
Definition: repositorySubscriberIf.hpp:132
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositorySubscriberIf.hpp:102
rtctk::componentFramework::DataPointPath
This class provides a wrapper for a data point path.
Definition: dataPointPath.hpp:65
rtctk::componentFramework::RepositoryIf::Response
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:188
rtctk::componentFramework::RepositorySubscriberIf::SendUnsubscribeRequest
virtual RepositoryIf::Response SendUnsubscribeRequest(const UnsubscribeRequest &request) const =0
This is called to asynchronously send a request to unsubscribe from various notifications.
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters
A structure to hold the arguments passed to the Add method.
Definition: repositorySubscriberIf.hpp:38
rtctk::componentFramework::RepositorySubscriberIf
Abstract interface providing subscription facilities for a repository.
Definition: repositorySubscriberIf.hpp:26
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::Parameters
Parameters(const DataPointPath &path, void *buffer, const std::type_info &type, const std::function< void(const DataPointPath &)> &removed_callback, const std::function< void(const DataPointPath &)> &newvalue_callback)
Allows to explicitly construct a complete parameters structure.
Definition: repositorySubscriberIf.hpp:52
rtctk_config_tool.type
type
List all available datapoint paths under a given path hierarchy.
Definition: rtctk_config_tool.py:44
rtctk::componentFramework::RepositorySubscriberIf::PyRepositorySubscriberIf
friend class PyRepositorySubscriberIf
Definition: repositorySubscriberIf.hpp:294
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::AddNewValueHandler
void AddNewValueHandler(const DataPointPath &path)
Adds a request to unsubscribe from new value notifications for a datapoint.
Definition: repositorySubscriberIf.cpp:23
rtctk::componentFramework::RepositorySubscriberIf::Unsubscribe
void Unsubscribe(const DataPointPath &path) const
A simple convenience function that will deregister all callbacks for receiving new datapoint values.
Definition: repositorySubscriberIf.cpp:27
rtctk::componentFramework::RepositorySubscriberIf::Subscribe
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:377