Motorcortex Core  version: 2.7.6
ps_handlers.h
1 /*
2  * Developer : Alexey Zakharov (alexey.zakharov@vectioneer.com)
3  * All rights reserved. Copyright (c) 2015 VECTIONEER.
4  */
5 #ifndef PS_HANDLERS_H
6 #define PS_HANDLERS_H
7 
8 #include "ps_info.h"
9 #include "ps_parambase.h"
10 #include "utl_log.h"
11 #include <initializer_list>
12 #include <map>
13 
14 namespace mcx {
15 
16 namespace parameter_server {
17 
18 // Base class for all types parameter handlers
19 
20 class Handle {
21 public:
22  explicit Handle(ParameterBase* param) : param_(param){};
23 
24  inline bool isFound() const { return param_ != nullptr; }
25 
26  inline const char* getPath() const { return param_->getInfo().getPath(); }
27 
28  inline motorcortex_ParameterInfo getInfo() const {
30  if (param_) {
31  param_->getInfo().getInfo(&info);
32  }
33  return info;
34  }
35 
36 protected:
37  ParameterBase* param_;
38 };
39 
40 // ParamHandle is created by addParam method
41 // Instance of ParamHandle shows if parameter was updated from the input buffer
42 
43 class ParamHandle : public Handle {
44 public:
45  ParamHandle();
46 
47  ParamHandle(ParameterBase* param, void* origin_ptr);
48 
49  virtual ~ParamHandle();
50 
51  ParamHandle(const ParamHandle& orig);
52 
53  ParamHandle& operator=(const ParamHandle& orig);
54 
55  void setUserGroup(UserGroup user_group);
56 
57  // check if new value has been written to the parameter input
58  bool isUpdated();
59 
60  size_t size() const;
61 
62  // enable/disable update of the output buffer
63  void updateOutput(bool newval);
64 
65  void updateOutputOnce();
66 
67  void updateOutputOnce(size_t length);
68 
69  void updateOutputOnInput(bool newval);
70 
71  template <typename T>
72  void write(const T& in_data) {
73  *(T*)origin_ptr_ = in_data;
74  updateOutputOnce();
75  }
76 
77  template <typename T>
78  void write(const T in_data, int length) {
79  for (int i = 0; i < length; i++) {
80  ((T)origin_ptr_)[i] = in_data[i];
81  }
82  updateOutputOnce();
83  }
84 
85 private:
86  uint32_t update_counter_{};
87  void* origin_ptr_;
88 };
89 
90 class GroupHandle : public Handle {
91 public:
92  GroupHandle();
93 
94  GroupHandle(ParameterBase* group, std::map<std::string, ParamHandle> params);
95 
96  virtual ~GroupHandle();
97 
98  GroupHandle(const GroupHandle& orig);
99 
100  GroupHandle& operator=(const GroupHandle& orig);
101 
102  ParamHandle& getHandle(const std::string& name);
103 
104  void setUserGroup(UserGroup user_group);
105 
106  // check if new value has been written to the parameter input
107  bool isUpdated();
108 
109  // enable/disable update of the output buffer
110  void updateOutput(bool newval);
111 
112  void updateOutputOnce();
113 
114  void updateOutputOnInput(bool newval);
115 
116  template <typename T>
117  void write(const std::string& name, const T& in_data) {
118  params_handle_[name].write(in_data);
119  }
120 
121  template <typename T>
122  void write(const std::string& name, const T in_data, int length) {
123  params_handle_[name].write(in_data, length);
124  }
125 
126 private:
127  std::map<std::string, ParamHandle> params_handle_;
128 };
129 
130 // ReqHandle is created by request method
131 // Instance of ReqHandle is used to perform asynchronous read of the output buffer of the parameter
132 // ReqHandle does not start continuous
133 
134 class ReqHandle : public Handle {
135 public:
136  ReqHandle();
137 
138  ReqHandle(ParameterBase* param, long id);
139 
140  virtual ~ReqHandle();
141 
142  ReqHandle(const ReqHandle& orig);
143 
144  ReqHandle& operator=(const ReqHandle& orig);
145 
146  template <typename T>
147  bool read(T* out_data, int number_of_param);
148 
149  template <typename T>
150  bool read(T& out_data);
151 
152  const struct timespec& getTime() const;
153 
154 private:
155  long update_counter_{};
156  struct timespec update_time_ {};
157 };
158 
159 template <typename T>
160 bool ReqHandle::read(T* out_data, int number_of_param) {
161  if (param_) {
162  if (update_counter_ >= 0 && update_counter_ != param_->getReplyId()) {
163  param_->read(out_data, sizeof(T) * number_of_param, &update_time_);
164  update_counter_ = -1;
165  return true;
166  } else {
167  update_counter_ = param_->addRequest();
168  }
169  }
170 
171  return false;
172 }
173 
174 template <typename T>
175 bool ReqHandle::read(T& out_data) {
176  return read(&out_data, 1);
177 }
178 
179 // SubHandle is created by subscribe method
180 // Instance of SubHandle is used to read value from an output buffer of the parameter
181 
182 class SubHandle : public Handle {
183 public:
184  SubHandle();
185 
186  explicit SubHandle(ParameterBase* param);
187 
188  virtual ~SubHandle();
189 
190  SubHandle(const SubHandle& orig);
191 
192  SubHandle& operator=(const SubHandle& orig);
193 
194  template <typename T>
195  struct timespec read(T& out_data);
196 };
197 
198 template <typename T>
199 struct timespec SubHandle::read(T& out_data) {
200  log_assert(param_, "SubHandle can not have NULL pointer to the parameter") struct timespec timestamp {};
201  param_->read(&out_data, sizeof(out_data), &timestamp);
202  return timestamp;
203 }
204 
205 // PubHandle is created by publish method
206 // Instance of PubHandle is used to write values to an input buffer of the parameter
207 
208 class PubHandle : public Handle {
209 public:
210  PubHandle();
211 
212  explicit PubHandle(ParameterBase* param);
213 
214  virtual ~PubHandle();
215 
216  PubHandle(const PubHandle& orig);
217 
218  PubHandle& operator=(const PubHandle& orig);
219 
220  template <typename T>
221  void write(std::initializer_list<T> in_data);
222 
223  template <typename T, int number_of_param = 1>
224  void write(const T& in_data);
225 };
226 
227 template <typename T>
228 void PubHandle::write(std::initializer_list<T> in_data) {
229  log_assert(param_, "PubHandle can not have NULL pointer to the parameter")
230  param_->write(in_data.begin(), static_cast<uint32_t>(in_data.size() * sizeof(T)));
231 }
232 
233 template <typename T, int number_of_param>
234 void PubHandle::write(const T& in_data) {
235  log_assert(param_, "PubHandle can not have NULL pointer to the parameter")
236  param_->write(&in_data, number_of_param * sizeof(in_data));
237 }
238 
239 // LinkHandle is created by link method
240 // Instance of LinkHandle is used to remove link if necessary
241 
242 class LinkHandle : public Handle {
243 public:
244  LinkHandle();
245 
246  LinkHandle(ParameterBase* param, uint32_t element_offset);
247 
248  virtual ~LinkHandle();
249 
250  LinkHandle(const LinkHandle& orig);
251 
252  LinkHandle& operator=(const LinkHandle& orig);
253 
254  void activate(bool flag);
255 
256 private:
257  uint32_t element_offset_{};
258 };
259 
260 class OverwriteHandle : public Handle {
261 public:
262  OverwriteHandle();
263 
264  explicit OverwriteHandle(ParameterBase* param);
265 
266  virtual ~OverwriteHandle();
267 
268  OverwriteHandle(const OverwriteHandle& orig);
269 
270  OverwriteHandle& operator=(const OverwriteHandle& orig);
271 
272  void activate(bool flag);
273 
274  template <typename T>
275  void write(std::initializer_list<T> in_data);
276 
277  template <typename T, int number_of_param = 1>
278  void write(const T& in_data);
279 };
280 
281 template <typename T>
282 void OverwriteHandle::write(std::initializer_list<T> in_data) {
283  log_assert(param_, "OverwriteHandle can not have NULL pointer to the parameter")
284  param_->force(in_data.begin(), static_cast<uint32_t>(in_data.size() * sizeof(T)));
285 }
286 
287 template <typename T, int number_of_param>
288 void OverwriteHandle::write(const T& in_data) {
289  log_assert(param_, "OverwriteHandle can not have NULL pointer to the parameter")
290  param_->force(&in_data, number_of_param * sizeof(in_data));
291 }
292 
293 } // namespace parameter_server
294 
295 } // namespace mcx
296 
297 #endif /* PS_HANDLERS_H */
mcx::parameter_server::ParameterBase
Definition: ps_parambase.h:19
_motorcortex_ParameterInfo
Definition: motorcortex.pb.h:143
mcx::parameter_server::ReqHandle
Definition: ps_handlers.h:134
mcx::parameter_server::LinkHandle
Definition: ps_handlers.h:242
mcx::parameter_server::Handle
Definition: ps_handlers.h:20
mcx::parameter_server::GroupHandle
Definition: ps_handlers.h:90
mcx::parameter_server::SubHandle
Definition: ps_handlers.h:182
mcx::parameter_server::PubHandle
Definition: ps_handlers.h:208
mcx::parameter_server::OverwriteHandle
Definition: ps_handlers.h:260
mcx::parameter_server::ParamHandle
Definition: ps_handlers.h:43