RTC Toolkit  2.0.0
repositoryIf.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
14 
15 #include <chrono>
16 #include <cstdint>
17 #include <functional>
18 #include <future>
19 #include <vector>
20 
21 #include <gsl/span>
22 
26 
27 namespace rtctk::componentFramework {
28 
34 class RepositoryIf {
35 public:
36  using PathList = std::vector<DataPointPath>;
37 
44  class ReadRequest {
45  public:
49  class Parameters {
50  public:
51  Parameters() = default;
52 
60  explicit Parameters(const DataPointPath& path,
61  void* buffer,
62  const std::type_info& type,
63  const std::function<void()>& callback)
64  : m_path(path), m_buffer(buffer), m_type(&type), m_callback(callback) {
65  }
66 
67  inline const DataPointPath& GetPath() const {
68  return m_path;
69  };
70  inline void* GetBuffer() const {
71  return m_buffer;
72  };
73  inline const std::type_info& GetType() const {
74  return *m_type;
75  };
76  inline const std::function<void()>& GetCallback() const {
77  return m_callback;
78  };
79 
80  private:
82  DataPointPath m_path;
83 
85  void* m_buffer;
86 
88  const std::type_info* m_type;
89 
91  std::function<void()> m_callback;
92  };
93 
94  template <typename T>
95  void Add(const DataPointPath& path, T& buffer);
96  template <typename T, typename F>
97  void Add(const DataPointPath& path, T& buffer, F handler);
98  inline const std::vector<Parameters>& GetParams() const {
99  return m_params;
100  };
101  inline std::vector<Parameters>& GetParams() {
102  return m_params;
103  };
104 
105  private:
106  std::vector<Parameters> m_params;
107  };
108 
115  class WriteRequest {
116  public:
120  class Parameters {
121  public:
122  Parameters() = default;
123 
131  explicit Parameters(const DataPointPath& path,
132  const void* buffer,
133  const std::type_info& type,
134  const std::function<void()>& callback)
135  : m_path(path), m_buffer(buffer), m_type(&type), m_callback(callback) {
136  }
137 
138  inline const DataPointPath& GetPath() const {
139  return m_path;
140  };
141  inline const void* GetBuffer() const {
142  return m_buffer;
143  };
144  inline const std::type_info& GetType() const {
145  return *m_type;
146  };
147  inline const std::function<void()>& GetCallback() const {
148  return m_callback;
149  };
150 
151  private:
153  DataPointPath m_path;
154 
156  const void* m_buffer;
157 
159  const std::type_info* m_type;
160 
162  std::function<void()> m_callback;
163  };
164 
165  template <typename T>
166  void Add(const DataPointPath& path, const T& buffer);
167  template <typename T, typename F>
168  void Add(const DataPointPath& path, const T& buffer, F handler);
169  inline const std::vector<Parameters>& GetParams() const {
170  return m_params;
171  };
172  inline std::vector<Parameters>& GetParams() {
173  return m_params;
174  };
175 
176  private:
177  std::vector<Parameters> m_params;
178  };
179 
188  class Response {
189  public:
190  explicit Response(std::future<void>&& future) noexcept;
191  Response(Response&& other) noexcept;
192  Response& operator=(Response&& other) noexcept;
193 
202  void Wait();
203 
219  bool Wait(const std::chrono::microseconds& timeout);
220 
221  private:
222  Response(const Response& other) = delete;
223  Response& operator=(const Response& other) = delete;
224 
225  std::future<void> m_future;
226  };
227 
228  virtual ~RepositoryIf();
229 
261  virtual void CreateDataPoint(const DataPointPath& path, const std::type_info& type) = 0;
262 
263  template <typename T>
264  void CreateDataPoint(const DataPointPath& path);
265  template <typename T>
266  void CreateDataPoint(const DataPointPath& path, const T default_value);
267 
276  virtual void DeleteDataPoint(const DataPointPath& path) = 0;
277 
289  virtual const std::type_info& GetDataPointType(const DataPointPath& path) const = 0;
290 
310  virtual size_t GetDataPointSize(const DataPointPath& path) const = 0;
311 
321  virtual bool DataPointExists(const DataPointPath& path) const = 0;
322 
344  virtual std::pair<PathList, PathList> GetChildren(const DataPointPath& path) const = 0;
345 
346  template <typename T>
347  T GetDataPoint(const DataPointPath& path) const;
348  template <typename T>
349  void SetDataPoint(const DataPointPath& path, const T value);
350  template <typename T>
351  void ReadDataPoint(const DataPointPath& path, T& buffer) const;
352  template <typename T>
353  void WriteDataPoint(const DataPointPath& path, const T& buffer);
354  void WriteDataPoint(const DataPointPath& path, const char* buffer);
355 
380  virtual Response SendReadRequest(const ReadRequest& request) const = 0;
381 
408  virtual Response SendWriteRequest(const WriteRequest& request) = 0;
409 };
410 
420 template <typename T>
421 void RepositoryIf::ReadRequest::Add(const DataPointPath& path, T& buffer) {
422  m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr));
423 }
424 
450 template <typename T, typename F>
451 void RepositoryIf::ReadRequest::Add(const DataPointPath& path, T& buffer, F handler) {
452  // Prepare a lambda with no arguments that also captures the buffer, i.e. it effectively
453  // captures the type and allows us to store this in the m_callback member and invoke the
454  // handler at a later time.
455  auto callback = [handler, &buffer]() -> void { handler(buffer); };
456  m_params.push_back(Parameters(path, &buffer, typeid(buffer), callback));
457 }
458 
468 template <typename T>
469 void RepositoryIf::WriteRequest::Add(const DataPointPath& path, const T& buffer) {
470  m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr));
471 }
472 
497 template <typename T, typename F>
498 void RepositoryIf::WriteRequest::Add(const DataPointPath& path, const T& buffer, F handler) {
499  auto callback = [handler, &buffer]() -> void { handler(buffer); };
500  m_params.push_back(Parameters(path, &buffer, typeid(buffer), callback));
501 }
502 
514 template <typename T>
516  CreateDataPoint(path, typeid(T));
517 }
518 
531 template <typename T>
532 void RepositoryIf::CreateDataPoint(const DataPointPath& path, const T default_value) {
533  CreateDataPoint<T>(path);
534  SetDataPoint(path, default_value);
535 }
536 
551 template <typename T>
553  T data = T();
554  ReadDataPoint(path, data);
555  return data;
556 }
557 
572 template <typename T>
573 void RepositoryIf::SetDataPoint(const DataPointPath& path, const T value) {
574  WriteDataPoint(path, value);
575 }
576 
591 template <typename T>
592 void RepositoryIf::ReadDataPoint(const DataPointPath& path, T& buffer) const {
593  ReadRequest request;
594  request.Add(path, buffer);
595  SendReadRequest(request).Wait();
596 }
597 
616 template <typename T>
617 void RepositoryIf::WriteDataPoint(const DataPointPath& path, const T& buffer) {
618  WriteRequest request;
619  request.Add(path, buffer);
620  SendWriteRequest(request).Wait();
621 }
622 
623 // The following specialisations are declared here so that they are used during compilation, but
624 // must actually be defined in the .cpp file to avoid linker failures.
625 template <>
626 void RepositoryIf::CreateDataPoint(const DataPointPath& path, const char* default_value);
627 
628 template <>
629 void RepositoryIf::SetDataPoint(const DataPointPath& path, const char* value);
630 
631 } // namespace rtctk::componentFramework
632 
633 #endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters::GetType
const std::type_info & GetType() const
Definition: repositoryIf.hpp:73
rtctk::componentFramework::RepositoryIf::SetDataPoint
void SetDataPoint(const DataPointPath &path, const T value)
Sets a datapoint in the repository.
Definition: repositoryIf.hpp:573
rtctk::componentFramework::RepositoryIf::GetDataPoint
T GetDataPoint(const DataPointPath &path) const
Fetches a datapoint from the repository.
Definition: repositoryIf.hpp:552
rtctk::componentFramework::RepositoryIf::WriteRequest::Add
void Add(const DataPointPath &path, const T &buffer)
Adds a datapoint to the request for writing.
Definition: repositoryIf.hpp:469
rtctk::componentFramework::RepositoryIf::WriteRequest::GetParams
std::vector< Parameters > & GetParams()
Definition: repositoryIf.hpp:172
rtctk::componentFramework::RepositoryIf::ReadRequest
A request object to pass information about datapoints that should be read from the repository.
Definition: repositoryIf.hpp:44
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters::GetType
const std::type_info & GetType() const
Definition: repositoryIf.hpp:144
rtctk::componentFramework::RepositoryIf::SendReadRequest
virtual Response SendReadRequest(const ReadRequest &request) const =0
Sends a request to read data from the repository.
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters::GetBuffer
void * GetBuffer() const
Definition: repositoryIf.hpp:70
rtctk::componentFramework
Definition: commandReplier.cpp:20
matrixSpan.hpp
Declaration of the MatrixSpan template class used in APIs.
rtctk::componentFramework::RepositoryIf::DataPointExists
virtual bool DataPointExists(const DataPointPath &path) const =0
Checks for the existence of a datapoint in the repository.
rtctk::componentFramework::RepositoryIf::GetDataPointType
virtual const std::type_info & GetDataPointType(const DataPointPath &path) const =0
Fetches the type of the datapoint.
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters::Parameters
Parameters(const DataPointPath &path, void *buffer, const std::type_info &type, const std::function< void()> &callback)
Allows to explicitly construct a complete parameters structure.
Definition: repositoryIf.hpp:60
matrixBuffer.hpp
Declaration of the MatrixBuffer template class used in APIs.
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters::Parameters
Parameters(const DataPointPath &path, const void *buffer, const std::type_info &type, const std::function< void()> &callback)
Allows to explicitly construct a complete parameters structure.
Definition: repositoryIf.hpp:131
rtctk::componentFramework::RepositoryIf::GetChildren
virtual std::pair< PathList, PathList > GetChildren(const DataPointPath &path) const =0
Queries the datapoints and child paths for a given path.
rtctk::componentFramework::RepositoryIf::GetDataPointSize
virtual size_t GetDataPointSize(const DataPointPath &path) const =0
Fetches the size of the datapoint's data.
rtctk::componentFramework::RepositoryIf::WriteRequest::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositoryIf.hpp:169
rtctk::componentFramework::RepositoryIf::WriteRequest
A request object to pass information about datapoints that should be written to the repository.
Definition: repositoryIf.hpp:115
rtctk::componentFramework::RepositoryIf::ReadRequest::Add
void Add(const DataPointPath &path, T &buffer)
Adds a datapoint to the request for reading.
Definition: repositoryIf.hpp:421
rtctk::componentFramework::RepositoryIf::DeleteDataPoint
virtual void DeleteDataPoint(const DataPointPath &path)=0
Deletes a datapoint.
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters::GetBuffer
const void * GetBuffer() const
Definition: repositoryIf.hpp:141
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::RepositoryIf
Abstract interface providing basic read and write facilities to a repository.
Definition: repositoryIf.hpp:34
rtctk::componentFramework::RepositoryIf::Response::operator=
Response & operator=(Response &&other) noexcept
Definition: repositoryIf.cpp:25
rtctk::componentFramework::RepositoryIf::Response::Response
Response(std::future< void > &&future) noexcept
Definition: repositoryIf.cpp:17
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters
A structure to hold the arguments passed with one of the Add methods.
Definition: repositoryIf.hpp:49
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters::GetCallback
const std::function< void()> & GetCallback() const
Definition: repositoryIf.hpp:147
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters::GetPath
const DataPointPath & GetPath() const
Definition: repositoryIf.hpp:67
rtctk::componentFramework::RepositoryIf::ReadDataPoint
void ReadDataPoint(const DataPointPath &path, T &buffer) const
Reads a datapoint from the repository.
Definition: repositoryIf.hpp:592
rtctk::componentFramework::RepositoryIf::WriteDataPoint
void WriteDataPoint(const DataPointPath &path, const T &buffer)
Writes a datapoint to the repository.
Definition: repositoryIf.hpp:617
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters::GetPath
const DataPointPath & GetPath() const
Definition: repositoryIf.hpp:138
rtctk::componentFramework::RepositoryIf::PathList
std::vector< DataPointPath > PathList
Definition: repositoryIf.hpp:36
rtctk::componentFramework::RepositoryIf::ReadRequest::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositoryIf.hpp:98
rtctk::componentFramework::RepositoryIf::SendWriteRequest
virtual Response SendWriteRequest(const WriteRequest &request)=0
Sends a request to write data to the repository.
rtctk::componentFramework::RepositoryIf::CreateDataPoint
virtual void CreateDataPoint(const DataPointPath &path, const std::type_info &type)=0
Creates a new datapoint in the repository with a specified type.
dataPointPath.hpp
Header file for DataPointPath.
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::RepositoryIf::ReadRequest::GetParams
std::vector< Parameters > & GetParams()
Definition: repositoryIf.hpp:101
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters
A structure to hold the arguments passed with one of the Add methods.
Definition: repositoryIf.hpp:120
rtctk_config_tool.type
type
List all available datapoint paths under a given path hierarchy.
Definition: rtctk_config_tool.py:44
rtctk::componentFramework::RepositoryIf::~RepositoryIf
virtual ~RepositoryIf()
Definition: repositoryIf.cpp:44
rtctk::componentFramework::RepositoryIf::WriteRequest::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositoryIf::ReadRequest::Parameters::GetCallback
const std::function< void()> & GetCallback() const
Definition: repositoryIf.hpp:76