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