RTC Toolkit  1.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 <string>
20 #include <vector>
21 
22 #include <gsl/span>
23 
27 
28 namespace rtctk::componentFramework {
29 
35 class RepositoryIf {
36 public:
37  using StringList = std::vector<std::string>;
38 
45  class Request {
46  public:
50  struct Parameters {
51  Parameters() = default;
52 
62  explicit Parameters(const DataPointPath& path,
63  void* buffer,
64  const std::type_info& type,
65  const std::function<void()>& callback)
66  : m_path(path), m_buffer(buffer), m_type(type), m_callback(callback) {
67  }
68 
71 
73  void* m_buffer;
74 
76  const std::type_info& m_type;
77 
79  std::function<void()> m_callback;
80  };
81 
82  template <typename T>
83  void Add(const DataPointPath& path, T& buffer);
84  template <typename T>
85  void Add(const DataPointPath& path, const T& buffer);
86  template <typename T, typename F>
87  void Add(const DataPointPath& path, T& buffer, F handler);
88  template <typename T, typename F>
89  void Add(const DataPointPath& path, const T& buffer, F handler);
90  inline const std::vector<Parameters>& GetParams() const {
91  return m_params;
92  };
93 
94  private:
95  std::vector<Parameters> m_params;
96  };
97 
106  class Response {
107  public:
108  explicit Response(std::future<void>&& future) noexcept;
109  Response(Response&& other) noexcept;
110  Response& operator=(Response&& other) noexcept;
111 
121  void Wait();
122 
138  bool Wait(const std::chrono::seconds timeout);
139 
140  private:
141  Response(const Response& other) = delete;
142  Response& operator=(const Response& other) = delete;
143 
144  std::future<void> m_future;
145  };
146 
147  virtual ~RepositoryIf();
148 
180  virtual void CreateDataPoint(const DataPointPath& path, const std::type_info& type) = 0;
181 
182  template <typename T>
183  void CreateDataPoint(const DataPointPath& path);
184  template <typename T>
185  void CreateDataPoint(const DataPointPath& path, const T default_value);
186 
195  virtual void DeleteDataPoint(const DataPointPath& path) = 0;
196 
208  virtual const std::type_info& GetDataPointType(const DataPointPath& path) const = 0;
209 
229  virtual size_t GetDataPointSize(const DataPointPath& path) const = 0;
230 
240  virtual bool DataPointExists(const DataPointPath& path) const = 0;
241 
263  virtual std::pair<StringList, StringList> GetChildren(const DataPointPath& path) const = 0;
264 
265  template <typename T>
266  T GetDataPoint(const DataPointPath& path) const;
267  template <typename T>
268  void SetDataPoint(const DataPointPath& path, const T value);
269  template <typename T>
270  void ReadDataPoint(const DataPointPath& path, T& buffer) const;
271  template <typename T>
272  void WriteDataPoint(const DataPointPath& path, const T& buffer);
273  void WriteDataPoint(const DataPointPath& path, const char* buffer);
274 
299  virtual Response SendReadRequest(const Request& request) const = 0;
300 
327  virtual Response SendWriteRequest(const Request& request) = 0;
328 };
329 
339 template <typename T>
340 void RepositoryIf::Request::Add(const DataPointPath& path, T& buffer) {
341  m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr));
342 }
343 
353 template <typename T>
354 void RepositoryIf::Request::Add(const DataPointPath& path, const T& buffer) {
355  m_params.push_back(Parameters(path, const_cast<T*>(&buffer), typeid(buffer), nullptr));
356 }
357 
383 template <typename T, typename F>
384 void RepositoryIf::Request::Add(const DataPointPath& path, T& buffer, F handler) {
385  // Prepare a lambda with no arguments that also captures the buffer, i.e. it effectively
386  // captures the type and allows us to store this in the m_callback member and invoke the
387  // handler at a later time.
388  auto callback = [handler, &buffer]() -> void { handler(buffer); };
389  m_params.push_back(Parameters(path, &buffer, typeid(buffer), callback));
390 }
391 
415 template <typename T, typename F>
416 void RepositoryIf::Request::Add(const DataPointPath& path, const T& buffer, F handler) {
417  auto callback = [handler, &buffer]() -> void { handler(buffer); };
418  m_params.push_back(Parameters(path, const_cast<T*>(&buffer), typeid(buffer), callback));
419 }
420 
431 template <typename T>
433  CreateDataPoint(path, typeid(T));
434 }
435 
447 template <typename T>
448 void RepositoryIf::CreateDataPoint(const DataPointPath& path, const T default_value) {
449  CreateDataPoint<T>(path);
450  SetDataPoint(path, default_value);
451 }
452 
467 template <typename T>
469  T data = T();
470  ReadDataPoint(path, data);
471  return data;
472 }
473 
488 template <typename T>
489 void RepositoryIf::SetDataPoint(const DataPointPath& path, const T value) {
490  WriteDataPoint(path, value);
491 }
492 
507 template <typename T>
508 void RepositoryIf::ReadDataPoint(const DataPointPath& path, T& buffer) const {
509  Request request;
510  request.Add(path, buffer);
511  SendReadRequest(request).Wait();
512 }
513 
532 template <typename T>
533 void RepositoryIf::WriteDataPoint(const DataPointPath& path, const T& buffer) {
534  Request request;
535  request.Add(path, buffer);
536  SendWriteRequest(request).Wait();
537 }
538 
539 // The following specialisations are declared here so that they are used during compilation, but
540 // must actually be defined in the .cpp file to avoid linker failures.
541 template <>
542 void RepositoryIf::CreateDataPoint(const DataPointPath& path, const char* default_value);
543 
544 template <>
545 void RepositoryIf::SetDataPoint(const DataPointPath& path, const char* value);
546 
547 } // namespace rtctk::componentFramework
548 
549 #endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
rtctk::componentFramework::RepositoryIf::SetDataPoint
void SetDataPoint(const DataPointPath &path, const T value)
Sets a datapoint in the repository.
Definition: repositoryIf.hpp:489
rtctk::componentFramework::RepositoryIf::GetDataPoint
T GetDataPoint(const DataPointPath &path) const
Fetches a datapoint from the repository.
Definition: repositoryIf.hpp:468
rtctk::componentFramework::RepositoryIf::SendWriteRequest
virtual Response SendWriteRequest(const Request &request)=0
Sends a request to write data to the repository.
rtctk::componentFramework::RepositoryIf::Request
A request object to pass information about datapoints that should be read (written) from (to) the rep...
Definition: repositoryIf.hpp:45
rtctk::componentFramework::RepositoryIf::Request::Parameters::m_callback
std::function< void()> m_callback
The callback function to invoke when a datapoint is received.
Definition: repositoryIf.hpp:79
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::RepositoryIf::Request::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:62
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.
matrixBuffer.hpp
Declaration of the MatrixBuffer template class used in APIs.
rtctk::componentFramework::RepositoryIf::Request::Parameters::Parameters
Parameters()=default
rtctk::componentFramework::RepositoryIf::GetDataPointSize
virtual size_t GetDataPointSize(const DataPointPath &path) const =0
Fetches the size of the datapoint's data.
rtctk::componentFramework::RepositoryIf::Request::GetParams
const std::vector< Parameters > & GetParams() const
Definition: repositoryIf.hpp:90
rtctk::componentFramework::RepositoryIf::Request::Parameters::m_buffer
void * m_buffer
The buffer of data to read (write) for a write (read) request.
Definition: repositoryIf.hpp:73
rtctk::componentFramework::RepositoryIf::DeleteDataPoint
virtual void DeleteDataPoint(const DataPointPath &path)=0
Deletes a datapoint.
rtctk::componentFramework::RepositoryIf::Request::Parameters::m_path
DataPointPath m_path
The datapoint path to read or write.
Definition: repositoryIf.hpp:70
rtctk::componentFramework::RepositoryIf::StringList
std::vector< std::string > StringList
Definition: repositoryIf.hpp:37
rtctk::componentFramework::RepositoryIf::Request::Add
void Add(const DataPointPath &path, T &buffer)
Adds a datapoint to the request for reading.
Definition: repositoryIf.hpp:340
rtctk::componentFramework::RepositoryIf::Response::Wait
void Wait()
Waits for the request sent to the repository to complete.
Definition: repositoryIf.cpp:30
rtctk::componentFramework::RepositoryIf
Abstract interface providing basic read and write facilities to a repository.
Definition: repositoryIf.hpp:35
rtctk::componentFramework::RepositoryIf::GetChildren
virtual std::pair< StringList, StringList > GetChildren(const DataPointPath &path) const =0
Queries the datapoints and child paths for a given path.
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::ReadDataPoint
void ReadDataPoint(const DataPointPath &path, T &buffer) const
Reads a datapoint from the repository.
Definition: repositoryIf.hpp:508
rtctk::componentFramework::RepositoryIf::WriteDataPoint
void WriteDataPoint(const DataPointPath &path, const T &buffer)
Writes a datapoint to the repository.
Definition: repositoryIf.hpp:533
rtctk::componentFramework::RepositoryIf::Request::Parameters
A structure to hold the arguments passed with one of the Add methods.
Definition: repositoryIf.hpp:50
rtctk::componentFramework::RepositoryIf::Request::Parameters::m_type
const std::type_info & m_type
This stores the type information for the buffer.
Definition: repositoryIf.hpp:76
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 RepositoryIf and related base classes.
rtctk::componentFramework::RepositoryIf::SendReadRequest
virtual Response SendReadRequest(const Request &request) const =0
Sends a request to read data from the repository.
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::RepositoryIf::Response
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:106
rtctk::componentFramework::RepositoryIf::~RepositoryIf
virtual ~RepositoryIf()
Definition: repositoryIf.cpp:43