RTC Toolkit 4.0.1
Loading...
Searching...
No Matches
repositoryIf.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
14#define RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
15
16#include <chrono>
17#include <cstdint>
18#include <functional>
19#include <future>
20#include <type_traits>
21#include <vector>
22
23#include <gsl/span>
24
28
30
39public:
40 using PathList = std::vector<DataPointPath>;
41
50 public:
55 class Parameters {
56 public:
57 Parameters() = default;
58
66 explicit Parameters(const DataPointPath& path,
67 void* buffer,
68 const std::type_info& type,
69 const std::function<void()>& callback)
70 : m_path(path), m_buffer(buffer), m_type(&type), m_callback(callback) {
71 }
72
73 inline const DataPointPath& GetPath() const {
74 return m_path;
75 };
76 inline void* GetBuffer() const {
77 return m_buffer;
78 };
79 inline const std::type_info& GetType() const {
80 return *m_type;
81 };
82 inline const std::function<void()>& GetCallback() const {
83 return m_callback;
84 };
85
86 private:
88 DataPointPath m_path;
89
91 void* m_buffer;
92
94 const std::type_info* m_type;
95
97 std::function<void()> m_callback;
98 };
99
100 template <typename T>
101 void Add(const DataPointPath& path, T& buffer);
102 template <typename T, typename F>
103 void Add(const DataPointPath& path, T& buffer, F handler);
104 inline const std::vector<Parameters>& GetParams() const {
105 return m_params;
106 };
107 inline std::vector<Parameters>& GetParams() {
108 return m_params;
109 };
110
111 private:
112 std::vector<Parameters> m_params;
113 };
114
123 public:
129 public:
130 Parameters() = default;
131
139 explicit Parameters(const DataPointPath& path,
140 const void* buffer,
141 const std::type_info& type,
142 const std::function<void()>& callback)
143 : m_path(path), m_buffer(buffer), m_type(&type), m_callback(callback) {
144 }
145
146 inline const DataPointPath& GetPath() const {
147 return m_path;
148 };
149 inline const void* GetBuffer() const {
150 return m_buffer;
151 };
152 inline const std::type_info& GetType() const {
153 return *m_type;
154 };
155 inline const std::function<void()>& GetCallback() const {
156 return m_callback;
157 };
158
159 private:
161 DataPointPath m_path;
162
164 const void* m_buffer;
165
167 const std::type_info* m_type;
168
170 std::function<void()> m_callback;
171 };
172
173 template <typename T>
174 void Add(const DataPointPath& path, const T& buffer);
175 template <typename T, typename F>
176 void Add(const DataPointPath& path, const T& buffer, F handler);
177 inline const std::vector<Parameters>& GetParams() const {
178 return m_params;
179 };
180 inline std::vector<Parameters>& GetParams() {
181 return m_params;
182 };
183
184 private:
185 std::vector<Parameters> m_params;
186 };
187
197 class Response {
198 public:
199 explicit Response(std::future<void>&& future) noexcept;
200 Response(Response&& other) noexcept;
201 Response& operator=(Response&& other) noexcept;
202
211 void Wait();
212
228 bool Wait(const std::chrono::microseconds& timeout);
229
230 private:
231 Response(const Response& other) = delete;
232 Response& operator=(const Response& other) = delete;
233
234 std::future<void> m_future;
235 };
236
237 virtual ~RepositoryIf();
238
270 virtual void CreateDataPoint(const DataPointPath& path, const std::type_info& type) = 0;
271
272 template <typename T>
274 template <typename T>
275 void CreateDataPoint(const DataPointPath& path, const T default_value);
276
285 virtual void DeleteDataPoint(const DataPointPath& path) = 0;
286
298 virtual const std::type_info& GetDataPointType(const DataPointPath& path) const = 0;
299
319 virtual size_t GetDataPointSize(const DataPointPath& path) const = 0;
320
330 virtual bool DataPointExists(const DataPointPath& path) const = 0;
331
353 virtual std::pair<PathList, PathList> GetChildren(const DataPointPath& path) const = 0;
354
355 template <typename T>
356 T GetDataPoint(const DataPointPath& path) const;
357 template <typename T>
358 void SetDataPoint(const DataPointPath& path, const T value);
359 template <typename T>
360 void ReadDataPoint(const DataPointPath& path, T& buffer) const;
361 template <typename T>
362 void WriteDataPoint(const DataPointPath& path, const T& buffer);
363 void WriteDataPoint(const DataPointPath& path, const char* buffer);
364
389 virtual Response SendReadRequest(const ReadRequest& request) const = 0;
390
417 virtual Response SendWriteRequest(const WriteRequest& request) = 0;
418};
419
430template <typename T>
431void RepositoryIf::ReadRequest::Add(const DataPointPath& path, T& buffer) {
432 m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr));
433}
434
463template <typename T, typename F>
464void RepositoryIf::ReadRequest::Add(const DataPointPath& path, T& buffer, F handler) {
465 static_assert(std::is_convertible_v<F, std::function<void(T&)>>,
466 "The handler callback must have a signature compatible with void(T&).");
467 // Prepare a lambda with no arguments that also captures the buffer, i.e. it effectively
468 // captures the type and allows us to store this in the m_callback member and invoke the
469 // handler at a later time.
470 auto callback = [handler, &buffer]() -> void { handler(buffer); };
471 m_params.push_back(Parameters(path, &buffer, typeid(buffer), callback));
472}
473
484template <typename T>
485void RepositoryIf::WriteRequest::Add(const DataPointPath& path, const T& buffer) {
486 m_params.push_back(Parameters(path, &buffer, typeid(buffer), nullptr));
487}
488
515template <typename T, typename F>
516void RepositoryIf::WriteRequest::Add(const DataPointPath& path, const T& buffer, F handler) {
517 static_assert(std::is_convertible_v<F, std::function<void(const T&)>>,
518 "The handler callback must have a signature compatible with void(const T&).");
519 auto callback = [handler, &buffer]() -> void { handler(buffer); };
520 m_params.push_back(Parameters(path, &buffer, typeid(buffer), callback));
521}
522
535template <typename T>
537 CreateDataPoint(path, typeid(T));
538}
539
553template <typename T>
554void RepositoryIf::CreateDataPoint(const DataPointPath& path, const T default_value) {
555 CreateDataPoint<T>(path);
556 SetDataPoint(path, default_value);
557}
558
573template <typename T>
575 T data = T();
576 ReadDataPoint(path, data);
577 return data;
578}
579
594template <typename T>
595void RepositoryIf::SetDataPoint(const DataPointPath& path, const T value) {
596 WriteDataPoint(path, value);
597}
598
613template <typename T>
614void RepositoryIf::ReadDataPoint(const DataPointPath& path, T& buffer) const {
615 ReadRequest request;
616 request.Add(path, buffer);
617 SendReadRequest(request).Wait();
618}
619
638template <typename T>
639void RepositoryIf::WriteDataPoint(const DataPointPath& path, const T& buffer) {
640 WriteRequest request;
641 request.Add(path, buffer);
642 SendWriteRequest(request).Wait();
643}
644
645// The following specialisations are declared here so that they are used during compilation, but
646// must actually be defined in the .cpp file to avoid linker failures.
647template <>
648void RepositoryIf::CreateDataPoint(const DataPointPath& path, const char* default_value);
649
650template <>
651void RepositoryIf::SetDataPoint(const DataPointPath& path, const char* value);
652
653} // namespace rtctk::componentFramework
654
655#endif // RTCTK_COMPONENTFRAMEWORK_REPOSITORYIF_HPP
This class provides a wrapper for a data point path.
Definition: dataPointPath.hpp:73
A structure to hold the arguments passed with one of the Add methods.
Definition: repositoryIf.hpp:55
const DataPointPath & GetPath() const
Definition: repositoryIf.hpp:73
const std::function< void()> & GetCallback() const
Definition: repositoryIf.hpp:82
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:66
const std::type_info & GetType() const
Definition: repositoryIf.hpp:79
void * GetBuffer() const
Definition: repositoryIf.hpp:76
A request object to pass information about datapoints that should be read from the repository.
Definition: repositoryIf.hpp:49
void Add(const DataPointPath &path, T &buffer)
Adds a datapoint to the request for reading.
Definition: repositoryIf.hpp:431
std::vector< Parameters > & GetParams()
Definition: repositoryIf.hpp:107
const std::vector< Parameters > & GetParams() const
Definition: repositoryIf.hpp:104
An object used to wait for a request to complete.
Definition: repositoryIf.hpp:197
Response & operator=(Response &&other) noexcept
Definition: repositoryIf.cpp:26
void Wait()
Waits for the request that was sent to the repository to complete.
Definition: repositoryIf.cpp:32
A structure to hold the arguments passed with one of the Add methods.
Definition: repositoryIf.hpp:128
const void * GetBuffer() const
Definition: repositoryIf.hpp:149
const std::type_info & GetType() const
Definition: repositoryIf.hpp:152
const DataPointPath & GetPath() const
Definition: repositoryIf.hpp:146
const std::function< void()> & GetCallback() const
Definition: repositoryIf.hpp:155
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:139
A request object to pass information about datapoints that should be written to the repository.
Definition: repositoryIf.hpp:122
void Add(const DataPointPath &path, const T &buffer)
Adds a datapoint to the request for writing.
Definition: repositoryIf.hpp:485
std::vector< Parameters > & GetParams()
Definition: repositoryIf.hpp:180
const std::vector< Parameters > & GetParams() const
Definition: repositoryIf.hpp:177
Abstract interface providing basic read and write facilities to a repository.
Definition: repositoryIf.hpp:38
virtual const std::type_info & GetDataPointType(const DataPointPath &path) const =0
Fetches the type of the datapoint.
virtual size_t GetDataPointSize(const DataPointPath &path) const =0
Fetches the size of the datapoint's data.
T GetDataPoint(const DataPointPath &path) const
Fetches a datapoint from the repository.
Definition: repositoryIf.hpp:574
virtual std::pair< PathList, PathList > GetChildren(const DataPointPath &path) const =0
Queries the datapoints and child paths for a given path.
virtual void CreateDataPoint(const DataPointPath &path, const std::type_info &type)=0
Creates a new datapoint in the repository with a specified type.
void SetDataPoint(const DataPointPath &path, const T value)
Sets a datapoint in the repository.
Definition: repositoryIf.hpp:595
virtual void DeleteDataPoint(const DataPointPath &path)=0
Deletes a datapoint.
void WriteDataPoint(const DataPointPath &path, const T &buffer)
Writes a datapoint to the repository.
Definition: repositoryIf.hpp:639
virtual ~RepositoryIf()
Definition: repositoryIf.cpp:45
virtual bool DataPointExists(const DataPointPath &path) const =0
Checks for the existence of a datapoint in the repository.
std::vector< DataPointPath > PathList
Definition: repositoryIf.hpp:40
void ReadDataPoint(const DataPointPath &path, T &buffer) const
Reads a datapoint from the repository.
Definition: repositoryIf.hpp:614
virtual Response SendWriteRequest(const WriteRequest &request)=0
Sends a request to write data to the repository.
virtual Response SendReadRequest(const ReadRequest &request) const =0
Sends a request to read data from the repository.
Header file for DataPointPath.
Declaration of the MatrixBuffer template class used in APIs.
Declaration of the MatrixSpan template class used in APIs.
Definition: commandReplier.cpp:22