RTC Toolkit  1.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 
16 
17 namespace rtctk::componentFramework {
18 
25 public:
32  public:
36  struct Parameters {
37  Parameters() = default;
38 
49  explicit Parameters(
50  const DataPointPath& path,
51  void* buffer,
52  const std::type_info& type,
53  const std::function<void(const DataPointPath& path)>& removed_callback,
54  const std::function<void(const DataPointPath& path)>& newvalue_callback)
55  : m_path(path)
56  , m_buffer(buffer)
57  , m_type(type)
58  , m_removed_callback(removed_callback)
59  , m_newvalue_callback(newvalue_callback) {
60  }
61 
64 
66  void* m_buffer;
67 
69  const std::type_info& m_type;
70 
72  std::function<void(const DataPointPath& path)> m_removed_callback;
73  std::function<void(const DataPointPath& path)> m_newvalue_callback;
74  };
75 
76  template <typename F>
77  void AddRemovedHandler(const DataPointPath& path, F handler);
78 
79  template <typename T, typename F>
80  void AddNewValueHandler(const DataPointPath& path, T& buffer, F handler);
81 
82  inline const std::vector<Parameters>& GetParams() const {
83  return m_params;
84  };
85 
86  private:
87  std::vector<Parameters> m_params;
88  };
89 
96  public:
100  struct Parameters {
101  Parameters() = default;
102 
112  const bool unsubscribe_removed,
113  const bool unsubscribe_newvalue)
114  : m_path(path)
115  , m_unsubscribe_removed(unsubscribe_removed)
116  , m_unsubscribe_newvalue(unsubscribe_newvalue) {
117  }
118 
121 
124 
127  };
128 
141  void AddRemovedHandler(const DataPointPath& path);
142 
155  void AddNewValueHandler(const DataPointPath& path);
156 
157  inline const std::vector<Parameters>& GetParams() const {
158  return m_params;
159  };
160 
161  private:
162  std::vector<Parameters> m_params;
163  };
164 
165  virtual ~RepositorySubscriberIf();
166 
204 
236  virtual RepositoryIf::Response
237  SendUnsubscribeRequest(const UnsubscribeRequest& request) const = 0;
238 
239  template <typename T, typename F>
240  void Subscribe(const DataPointPath& path, T& buffer, F handler) const;
241 
257  void Unsubscribe(const DataPointPath& path) const;
258 };
259 
278 template <typename F>
280  F handler) {
281  m_params.push_back(Parameters(path, nullptr, typeid(void), handler, nullptr));
282 }
283 
308 template <typename T, typename F>
310  T& buffer,
311  F handler) {
312  // We use a lambda here to have type erasure and allow storing callbacks for different types in
313  // the m_newvalue_callback.
314  auto callback = [handler, &buffer](const DataPointPath& path) -> void {
315  handler(path, buffer);
316  };
317  m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr, callback));
318 }
319 
338 template <typename T, typename F>
339 void RepositorySubscriberIf::Subscribe(const DataPointPath& path, T& buffer, F handler) const {
340  SubscribeRequest subscribe;
341  subscribe.AddNewValueHandler(path, buffer, handler);
342  SendSubscribeRequest(subscribe).Wait();
343 }
344 
345 } // namespace rtctk::componentFramework
346 
347 #endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYSUBSCRIBERIF_HPP
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::m_newvalue_callback
std::function< void(const DataPointPath &path)> m_newvalue_callback
Definition: repositorySubscriberIf.hpp:73
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::m_unsubscribe_newvalue
bool m_unsubscribe_newvalue
Indicates that we want to unsubscribe for new value notifications.
Definition: repositorySubscriberIf.hpp:126
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters
A structure to hold the arguments passed with one of the Add methods.
Definition: repositorySubscriberIf.hpp:100
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:111
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::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:309
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositorySubscriberIf.hpp:157
rtctk::componentFramework::RepositorySubscriberIf::~RepositorySubscriberIf
virtual ~RepositorySubscriberIf()
Definition: repositorySubscriberIf.cpp:16
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::m_removed_callback
std::function< void(const DataPointPath &path)> m_removed_callback
The callback functions to invoke when a datapoint is removed or updated.
Definition: repositorySubscriberIf.hpp:72
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::m_type
const std::type_info & m_type
This stores the type information for the buffer.
Definition: repositorySubscriberIf.hpp:69
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest
A request object to pass information about datapoints to subscribe to.
Definition: repositorySubscriberIf.hpp:31
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::Parameters
Parameters(const DataPointPath &path, void *buffer, const std::type_info &type, const std::function< void(const DataPointPath &path)> &removed_callback, const std::function< void(const DataPointPath &path)> &newvalue_callback)
Allows to explicitly construct a complete parameters structure.
Definition: repositorySubscriberIf.hpp:49
repositoryIf.hpp
Header file for RepositoryIf and related base classes.
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters
A structure to hold the arguments passed to the Add method.
Definition: repositorySubscriberIf.hpp:36
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest
A request object to pass information about datapoints to unsubscribe from.
Definition: repositorySubscriberIf.hpp:95
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::m_path
DataPointPath m_path
The datapoint path to subscribe to.
Definition: repositorySubscriberIf.hpp:63
rtctk::componentFramework::RepositoryIf::Response::Wait
void Wait()
Waits for the request sent to the repository to complete.
Definition: repositoryIf.cpp:30
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::m_unsubscribe_removed
bool m_unsubscribe_removed
Indicates that we want to unsubscribe for datapoint removal notifications.
Definition: repositorySubscriberIf.hpp:123
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:279
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::Parameters::m_buffer
void * m_buffer
The buffer of data that will be filled with new values.
Definition: repositorySubscriberIf.hpp:66
rtctk::componentFramework::RepositorySubscriberIf::SubscribeRequest::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositorySubscriberIf.hpp:82
rtctk::componentFramework::DataPointPath
This class provides a wraper for DataPoint paths which ensures that they only contain valid character...
Definition: dataPointPath.hpp:34
rtctk::componentFramework::RepositorySubscriberIf::UnsubscribeRequest::Parameters::m_path
DataPointPath m_path
The datapoint path to unsubscribe from.
Definition: repositorySubscriberIf.hpp:120
rtctk::componentFramework::RepositoryIf::Response
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:106
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
Abstract interface providing subscription facilities for a repository.
Definition: repositorySubscriberIf.hpp:24
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:339