RTC Toolkit  0.1.0-alpha
repositoryIf.hpp
Go to the documentation of this file.
1 
9 #ifndef RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
10 #define RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
11 
12 #include <cstdint>
13 #include <string>
14 #include <vector>
15 #include <future>
16 #include <map>
17 #include <typeindex>
18 #include <functional>
19 #include <chrono>
22 
23 namespace rtctk::componentFramework {
24 
28 class RepositoryIf {
29 public:
30  using StringList = std::vector<std::string>;
31 
37  class Request {
38  public:
42  struct Parameters
43  {
46 
48  void* m_buffer;
49 
51  std::type_index m_type;
52 
54  std::function<void()> m_callback;
55  };
56 
57  template <typename T> void Add(const DataPointPath& path, T& buffer);
58  template <typename T> void Add(const DataPointPath& path, const T& buffer);
59  template <typename T, typename F> void Add(const DataPointPath& path, T& buffer, F handler);
60  template <typename T, typename F> void Add(const DataPointPath& path, const T& buffer,
61  F handler);
62  inline const std::vector<Parameters>& GetParams() const { return m_params; };
63 
64  private:
65  std::vector<Parameters> m_params;
66  };
67 
74  class Response {
75  public:
76  Response(std::future<void>&& future) noexcept;
77  Response(Response&& other) noexcept;
78  Response& operator=(Response&& other) noexcept;
79  void Wait();
80  bool Wait(const std::chrono::seconds timeout);
81 
82  private:
83  Response(const Response& other) = delete;
84  Response& operator=(const Response& other) = delete;
85 
86  std::future<void> m_future;
87  };
88 
89  virtual ~RepositoryIf();
90 
119  virtual void CreateDataPoint(const DataPointPath& path, const std::string& type) = 0;
120 
121  template <typename T> void CreateDataPointV2(const DataPointPath& path);
122  template <typename T> void CreateDataPointV2(const DataPointPath& path, const T default_value);
123 
129  virtual void DeleteDataPoint(const DataPointPath& path) = 0;
130 
142  virtual std::string GetDataPointType(const DataPointPath& path) const = 0;
143 
160  virtual size_t GetDataPointSize(const DataPointPath& path) const = 0;
161 
168  virtual bool DataPointExists(const DataPointPath& path) const = 0;
169 
187  virtual std::pair<StringList, StringList> GetChildren(const DataPointPath& path) const = 0;
188 
189  template <typename T> T GetDataPoint(const DataPointPath& path) const;
190  template <typename T> void SetDataPoint(const DataPointPath& path, const T value);
191  template <typename T> void ReadDataPoint(const DataPointPath& path, T& buffer) const;
192  template <typename T> void WriteDataPoint(const DataPointPath& path, const T& buffer);
193  void WriteDataPoint(const DataPointPath& path, const char* buffer);
194 
219  virtual Response SendReadRequest(const Request& request) const = 0;
220 
247  virtual Response SendWriteRequest(const Request& request) = 0;
248 };
249 
256 template <typename T>
258  m_params.push_back({path, &buffer, std::type_index(typeid(buffer))});
259 }
260 
267 template <typename T>
268 void RepositoryIf::Request::Add(const DataPointPath& path, const T& buffer) {
269  m_params.push_back({path, const_cast<T*>(&buffer), std::type_index(typeid(buffer))});
270 }
271 
294 template <typename T, typename F>
295 void RepositoryIf::Request::Add(const DataPointPath& path, T& buffer, F handler) {
296  // Prepare a lambda with no arguments that also captures the buffer, i.e. it effectively
297  // captures the type and allows us to store this in the m_callback member and invoke the
298  // handler at a later time.
299  auto callback = [handler, &buffer] () -> void { handler(buffer); };
300  m_params.push_back({path, &buffer, std::type_index(typeid(buffer)), callback});
301 }
302 
323 template <typename T, typename F>
324 void RepositoryIf::Request::Add(const DataPointPath& path, const T& buffer, F handler) {
325  auto callback = [handler, &buffer] () -> void { handler(buffer); };
326  m_params.push_back({path, const_cast<T*>(&buffer), std::type_index(typeid(buffer)),
327  callback});
328 }
329 
337 template <typename T>
339  static const std::map<std::type_index, std::string> type_map = {
340  {std::type_index(typeid(bool)), "RtcBool"},
341  {std::type_index(typeid(int32_t)), "RtcInt32"},
342  {std::type_index(typeid(int64_t)), "RtcInt64"},
343  {std::type_index(typeid(float)), "RtcFloat"},
344  {std::type_index(typeid(double)), "RtcDouble"},
345  {std::type_index(typeid(std::string)), "RtcString"},
346  {std::type_index(typeid(std::vector<bool>)), "RtcVectorBool"},
347  {std::type_index(typeid(std::vector<int32_t>)), "RtcVectorInt32"},
348  {std::type_index(typeid(std::vector<int64_t>)), "RtcVectorInt64"},
349  {std::type_index(typeid(std::vector<float>)), "RtcVectorFloat"},
350  {std::type_index(typeid(std::vector<double>)), "RtcVectorDouble"},
351  {std::type_index(typeid(std::vector<std::string>)), "RtcVectorString"},
352  {std::type_index(typeid(MatrixBuffer<bool>)), "RtcMatrixBool"},
353  {std::type_index(typeid(MatrixBuffer<int32_t>)), "RtcMatrixInt32"},
354  {std::type_index(typeid(MatrixBuffer<int64_t>)), "RtcMatrixInt64"},
355  {std::type_index(typeid(MatrixBuffer<float>)), "RtcMatrixFloat"},
356  {std::type_index(typeid(MatrixBuffer<double>)), "RtcMatrixDouble"},
357  {std::type_index(typeid(MatrixBuffer<std::string>)), "RtcMatrixString"}
358  };
359  std::string type = type_map.at(std::type_index(typeid(T)));
360  CreateDataPoint(path, type);
361 }
362 
371 template <typename T>
372 void RepositoryIf::CreateDataPointV2(const DataPointPath& path, const T default_value) {
373  CreateDataPointV2<T>(path);
374  SetDataPoint(path, default_value);
375 }
376 
388 template <typename T>
390  T data = T();
391  ReadDataPoint(path, data);
392  return data;
393 }
394 
406 template <typename T>
407 void RepositoryIf::SetDataPoint(const DataPointPath& path, const T value) {
408  WriteDataPoint(path, value);
409 }
410 
422 template <typename T>
423 void RepositoryIf::ReadDataPoint(const DataPointPath& path, T& buffer) const {
424  Request request;
425  request.Add(path, buffer);
426  SendReadRequest(request).Wait();
427 }
428 
444 template <typename T>
445 void RepositoryIf::WriteDataPoint(const DataPointPath& path, const T& buffer) {
446  Request request;
447  request.Add(path, buffer);
448  SendWriteRequest(request).Wait();
449 }
450 
451 // The following specialisations are declared here so that they are used during compilation, but
452 // must actually be defined in the .cpp file to avoid linker failures.
453 template <>
454 void RepositoryIf::CreateDataPointV2(const DataPointPath& path, const char* default_value);
455 
456 template <>
457 void RepositoryIf::SetDataPoint(const DataPointPath& path, const char* value);
458 
459 } // namespace rtctk::componentFramework
460 
461 #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:407
rtctk::componentFramework::RepositoryIf::GetDataPoint
T GetDataPoint(const DataPointPath &path) const
Fetches a datapoint from the repository.
Definition: repositoryIf.hpp:389
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:37
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:54
rtctk::componentFramework
Definition: rtcComponent.hpp:17
rtctk::componentFramework::RepositoryIf::Request::Parameters::m_type
std::type_index m_type
This stores the type information for the buffer.
Definition: repositoryIf.hpp:51
rtctk::componentFramework::RepositoryIf::DataPointExists
virtual bool DataPointExists(const DataPointPath &path) const =0
Checks for the existence of a datapoint in the repository.
matrixBuffer.hpp
Declaration of the MatrixBuffer template class used in APIs.
rtctkExampleDataTaskRobotTest.path
string path
Definition: rtctkExampleDataTaskRobotTest.py:228
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:62
rtctk::componentFramework::RepositoryIf::CreateDataPoint
virtual void CreateDataPoint(const DataPointPath &path, const std::string &type)=0
Creates a new datapoint in the repository with a specified type.
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:48
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:45
rtctk::componentFramework::RepositoryIf::StringList
std::vector< std::string > StringList
Definition: repositoryIf.hpp:30
rtctk::componentFramework::RepositoryIf::Request::Add
void Add(const DataPointPath &path, T &buffer)
Definition: repositoryIf.hpp:257
rtctk::componentFramework::RepositoryIf::Response::Wait
void Wait()
Waits for the request sent to the repository to complete.
Definition: repositoryIf.cpp:33
rtctk::componentFramework::RepositoryIf
Abstract interface providing basic read and write facilities to a repository.
Definition: repositoryIf.hpp:28
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:22
rtctk::componentFramework::RepositoryIf::Response::Response
Response(std::future< void > &&future) noexcept
Definition: repositoryIf.cpp:14
rtctk::componentFramework::RepositoryIf::GetDataPointType
virtual std::string GetDataPointType(const DataPointPath &path) const =0
Fetches the type of the datapoint.
rtctk::componentFramework::RepositoryIf::ReadDataPoint
void ReadDataPoint(const DataPointPath &path, T &buffer) const
Reads a datapoint from the repository.
Definition: repositoryIf.hpp:423
rtctk::componentFramework::RepositoryIf::WriteDataPoint
void WriteDataPoint(const DataPointPath &path, const T &buffer)
Writes a datapoint to the repository.
Definition: repositoryIf.hpp:445
rtctk::componentFramework::RepositoryIf::Request::Parameters
A structure to hold the arguments passed with one of the Add methods.
Definition: repositoryIf.hpp:43
mudpi::int32_t
int int32_t
Definition: mudpi.h:17
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
Definition: dataPointPath.hpp:30
rtctk::componentFramework::RepositoryIf::Response
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:74
rtctk::componentFramework::RepositoryIf::CreateDataPointV2
void CreateDataPointV2(const DataPointPath &path)
Creates a new datapoint in the repository.
Definition: repositoryIf.hpp:338
rtctk::componentFramework::MatrixBuffer
Definition: matrixBuffer.hpp:19
rtctk::componentFramework::RepositoryIf::~RepositoryIf
virtual ~RepositoryIf()
Definition: repositoryIf.cpp:58