Lely core libraries  2.3.4
val.hpp
Go to the documentation of this file.
1 
23 #ifndef LELY_CO_VAL_HPP_
24 #define LELY_CO_VAL_HPP_
25 
26 #if !defined(__cplusplus) || LELY_NO_CXX
27 #error "include <lely/co/val.h> for the C interface"
28 #endif
29 
30 #include <lely/util/c_type.hpp>
31 #include <lely/co/type.hpp>
32 #include <lely/co/val.h>
33 
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 namespace lely {
39 
41 template <co_unsigned16_t N>
42 class COVal {
44 
45  public:
46  static const co_unsigned16_t index = traits::index;
47 
48  typedef typename traits::type type;
49 
50  operator const type&() const noexcept { return m_val; }
51  operator type&() noexcept { return m_val; }
52 
53  COVal() : m_val() {}
54 
55  COVal(const COVal&) = default;
56  COVal(COVal&&) = default;
57  COVal(const type& val) : m_val(val) {}
58  COVal(type&& val) : m_val(::std::move(val)) {}
59 
60  COVal(const void* ptr, ::std::size_t n) {
61  if (!co_val_make(index, this, ptr, n) && ptr && n)
63  }
64 
65  COVal& operator=(const COVal&) = default;
66  COVal& operator=(COVal&&) = default;
67 
68  COVal&
69  operator=(const type& val) {
70  m_val = val;
71  return *this;
72  }
73 
74  COVal&
75  operator=(type&& val) {
76  m_val = ::std::move(val);
77  return *this;
78  }
79 
80  const void*
81  address() const noexcept {
82  return static_cast<const void*>(&m_val);
83  }
84 
85  ::std::size_t
86  size() const noexcept {
87  return sizeof(type);
88  }
89 
90  private:
91  type m_val;
92 };
93 
95 template <>
98 
99  public:
100  static const co_unsigned16_t index = traits::index;
101 
102  typedef typename traits::type type;
103 
104  operator type() const noexcept { return m_val; }
105 
106  operator ::std::string() const {
107  return m_val ? ::std::string(m_val) : ::std::string();
108  }
109 
110  COVal() : m_val() {}
111  COVal(const COVal& val) : m_val() { *this = val; }
112  COVal(COVal&& val) : m_val() { *this = ::std::move(val); }
113 
114  COVal(const void* ptr, ::std::size_t n) {
115  if (!co_val_make(index, this, ptr, n) && ptr && n)
117  }
118 
119  COVal(const char* vs) { init(vs); }
120  COVal(const ::std::string& vs) { init(vs); }
121 
122  ~COVal() { co_val_fini(index, &m_val); }
123 
124  COVal&
125  operator=(const COVal& val) {
126  if (this != &val) {
127  this->~COVal();
128  if (!co_val_copy(index, &m_val, &val.m_val)) throw_or_abort(bad_copy());
129  }
130  return *this;
131  }
132 
133  COVal&
134  operator=(COVal&& val) {
135  this->~COVal();
136  if (!co_val_move(index, &m_val, &val.m_val)) throw_or_abort(bad_move());
137  return *this;
138  }
139 
140  COVal&
141  operator=(const char* vs) {
142  this->~COVal();
143  init(vs);
144  return *this;
145  }
146 
147  COVal&
148  operator=(const ::std::string& vs) {
149  this->~COVal();
150  init(vs);
151  return *this;
152  }
153 
154  const void*
155  address() const noexcept {
156  return co_val_addressof(index, this);
157  }
158 
159  ::std::size_t
160  size() const noexcept {
161  return co_val_sizeof(index, this);
162  }
163 
164  private:
165  void
166  init(const char* vs) {
167  if (co_val_init_vs(&m_val, vs) == -1) throw_or_abort(bad_init());
168  }
169 
170  void
171  init(const ::std::string& vs) {
172  init(vs.c_str());
173  }
174 
175  type m_val;
176 };
177 
179 template <>
182 
183  public:
184  static const co_unsigned16_t index = traits::index;
185 
186  typedef typename traits::type type;
187 
188  operator type() const noexcept { return m_val; }
189 
190  operator ::std::vector<uint_least8_t>() const {
191  return m_val ? ::std::vector<uint_least8_t>(m_val, m_val + size())
192  : ::std::vector<uint_least8_t>();
193  }
194 
195  COVal() : m_val() {}
196  COVal(const COVal& val) : m_val() { *this = val; }
197  COVal(COVal&& val) : m_val() { *this = ::std::move(val); }
198 
199  COVal(const void* ptr, ::std::size_t n) {
200  if (!co_val_make(index, this, ptr, n) && ptr && n)
202  }
203 
204  COVal(const uint_least8_t* os, ::std::size_t n) { init(os, n); }
205  COVal(const ::std::vector<uint_least8_t>& os) { init(os); }
206 
207  ~COVal() { co_val_fini(index, &m_val); }
208 
209  COVal&
210  operator=(const COVal& val) {
211  if (this != &val) {
212  this->~COVal();
213  if (!co_val_copy(index, &m_val, &val.m_val)) throw_or_abort(bad_copy());
214  }
215  return *this;
216  }
217 
218  COVal&
219  operator=(COVal&& val) {
220  this->~COVal();
221  if (!co_val_move(index, &m_val, &val.m_val)) throw_or_abort(bad_move());
222  return *this;
223  }
224 
225  COVal&
226  operator=(const ::std::vector<uint_least8_t>& os) {
227  this->~COVal();
228  init(os);
229  return *this;
230  }
231 
232  const void*
233  address() const noexcept {
234  return co_val_addressof(index, this);
235  }
236 
237  ::std::size_t
238  size() const noexcept {
239  return co_val_sizeof(index, this);
240  }
241 
242  private:
243  void
244  init(const uint_least8_t* os, ::std::size_t n) {
245  if (co_val_init_os(&m_val, os, n)) throw_or_abort(bad_init());
246  }
247 
248  void
249  init(const ::std::vector<uint_least8_t>& os) {
250  init(os.data(), os.size());
251  }
252 
253  type m_val;
254 };
255 
257 template <>
260 
261  public:
262  static const co_unsigned16_t index = traits::index;
263 
264  typedef typename traits::type type;
265 
266  operator type() const noexcept { return m_val; }
267 
268  operator ::std::basic_string<char16_t>() const {
269  return m_val ? ::std::basic_string<char16_t>(m_val)
270  : ::std::basic_string<char16_t>();
271  }
272 
273  COVal() : m_val() {}
274  COVal(const COVal& val) : m_val() { *this = val; }
275  COVal(COVal&& val) : m_val() { *this = ::std::move(val); }
276 
277  COVal(const void* ptr, ::std::size_t n) {
278  if (!co_val_make(index, this, ptr, n) && ptr && n)
280  }
281 
282  COVal(const char16_t* us) { init(us); }
283  COVal(const ::std::basic_string<char16_t>& us) { init(us); }
284 
285  ~COVal() { co_val_fini(index, &m_val); }
286 
287  COVal&
288  operator=(const COVal& val) {
289  if (this != &val) {
290  this->~COVal();
291  if (!co_val_copy(index, &m_val, &val.m_val)) throw_or_abort(bad_copy());
292  }
293  return *this;
294  }
295 
296  COVal&
297  operator=(COVal&& val) {
298  this->~COVal();
299  if (!co_val_move(index, &m_val, &val.m_val)) throw_or_abort(bad_move());
300  return *this;
301  }
302 
303  COVal&
304  operator=(const char16_t* us) {
305  this->~COVal();
306  init(us);
307  return *this;
308  }
309 
310  COVal&
311  operator=(const ::std::basic_string<char16_t>& us) {
312  this->~COVal();
313  init(us);
314  return *this;
315  }
316 
317  const void*
318  address() const noexcept {
319  return co_val_addressof(index, this);
320  }
321 
322  ::std::size_t
323  size() const noexcept {
324  return co_val_sizeof(index, this);
325  }
326 
327  private:
328  void
329  init(const char16_t* us) {
330  if (co_val_init_us(&m_val, us) == -1) throw_or_abort(bad_init());
331  }
332 
333  void
334  init(const ::std::basic_string<char16_t>& us) {
335  init(us.c_str());
336  }
337 
338  type m_val;
339 };
340 
342 template <>
345 
346  public:
347  static const co_unsigned16_t index = traits::index;
348 
349  typedef typename traits::type type;
350 
351  operator type() const noexcept { return m_val; }
352 
353  COVal() : m_val() {}
354  COVal(const COVal& val) : m_val() { *this = val; }
355  COVal(COVal&& val) : m_val() { *this = ::std::move(val); }
356 
357  COVal(const void* dom, ::std::size_t n) {
358  if (co_val_init_dom(&m_val, dom, n)) throw_or_abort(bad_init());
359  }
360 
361  ~COVal() { co_val_fini(index, &m_val); }
362 
363  COVal&
364  operator=(const COVal& val) {
365  if (this != &val) {
366  this->~COVal();
367  if (!co_val_copy(index, &m_val, &val.m_val)) throw_or_abort(bad_copy());
368  }
369  return *this;
370  }
371 
372  COVal&
373  operator=(COVal&& val) {
374  this->~COVal();
375  if (!co_val_move(index, &m_val, &val.m_val)) throw_or_abort(bad_move());
376  return *this;
377  }
378 
379  const void*
380  address() const noexcept {
381  return co_val_addressof(index, this);
382  }
383 
384  ::std::size_t
385  size() const noexcept {
386  return co_val_sizeof(index, this);
387  }
388 
389  private:
390  type m_val;
391 };
392 
393 } // namespace lely
394 
395 #endif // !LELY_CO_VAL_HPP_
lely::bad_init
The type of objects thrown as exceptions to report a failure to initialize an instantiation of a C ty...
Definition: c_type.hpp:38
co_val_copy
size_t co_val_copy(co_unsigned16_t type, void *dst, const void *src)
Copies one value to another.
Definition: val.c:318
val.h
co_val_fini
void co_val_fini(co_unsigned16_t type, void *val)
Finalizes a value of the specified data type.
Definition: val.c:249
co_val_addressof
const void * co_val_addressof(co_unsigned16_t type, const void *val)
Returns the address of the first byte in a value of the specified data type.
Definition: val.c:260
co_val_move
size_t co_val_move(co_unsigned16_t type, void *dst, void *src)
Moves one value to another.
Definition: val.c:354
CO_DEFTYPE_VISIBLE_STRING
#define CO_DEFTYPE_VISIBLE_STRING
The data type (and object index) of an array of visible characters.
Definition: type.h:56
CO_DEFTYPE_DOMAIN
#define CO_DEFTYPE_DOMAIN
The data type (and object index) of an arbitrary large block of data.
Definition: type.h:77
co_val_init_vs
int co_val_init_vs(char **val, const char *vs)
Initializes an array of visible characters (CO_DEFTYPE_VISIBLE_STRING).
Definition: val.c:146
c_type.hpp
CO_DEFTYPE_OCTET_STRING
#define CO_DEFTYPE_OCTET_STRING
The data type (and object index) of an array of octets.
Definition: type.h:59
throw_or_abort
#define throw_or_abort(e)
If exceptions are disabled, aborts the process instead of throwing an exception.
Definition: exception.hpp:38
lely::bad_copy
The type of objects thrown as exceptions to report a failure to copy an instantiation of a C type.
Definition: c_type.hpp:44
type.hpp
co_val_sizeof
size_t co_val_sizeof(co_unsigned16_t type, const void *val)
Returns the size (in bytes) of a value of the specified data type.
Definition: val.c:269
co_val_make
size_t co_val_make(co_unsigned16_t type, void *val, const void *ptr, size_t n)
Constructs a value of the specified data type.
Definition: val.c:282
co_val_init_us
int co_val_init_us(char16_t **val, const char16_t *us)
Initializes an array of (16-bit) Unicode characters (CO_DEFTYPE_UNICODE_STRING).
Definition: val.c:198
co_val_init_os
int co_val_init_os(uint_least8_t **val, const uint_least8_t *os, size_t n)
Initializes an array of octets (CO_DEFTYPE_OCTET_STRING).
Definition: val.c:179
lely::COVal
A CANopen value.
Definition: val.hpp:42
co_val_init_dom
int co_val_init_dom(void **val, const void *dom, size_t n)
Initializes an arbitrary large block of data (CO_DEFTYPE_DOMAIN).
Definition: val.c:230
CO_DEFTYPE_UNICODE_STRING
#define CO_DEFTYPE_UNICODE_STRING
The data type (and object index) of an array of (16-bit) Unicode characters.
Definition: type.h:62
lely::bad_move
The type of objects thrown as exceptions to report a failure to move an instantiation of a C type.
Definition: c_type.hpp:50
lely::co_type_traits_N
A class template mapping CANopen types to C++ types.
Definition: type.hpp:48