Lely core libraries 2.3.4
type_traits.cpp
Go to the documentation of this file.
1
24#include "coapp.hpp"
25#include <lely/co/val.h>
28#include <lely/util/error.hpp>
29
30#include <string>
31#include <vector>
32
33namespace lely {
34
35namespace canopen {
36
37namespace detail {
38
39template <class T, uint16_t N>
40typename canopen_traits<T, N, true>::c_type
41canopen_traits<T, N, true>::construct(const void* p, ::std::size_t n,
42 ::std::error_code& ec) noexcept {
43 auto val = typename canopen_traits<T, N, true>::c_type();
44 uint32_t ac = co_val_read_sdo(N, &val, p, n);
45 if (ac)
46 ec = make_error_code(static_cast<lely::canopen::SdoErrc>(ac));
47 else
48 ec.clear();
49 return val;
50}
51
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
54// BOOLEAN
55template bool canopen_traits<bool, 0x0001, true>::construct(
56 const void*, ::std::size_t, ::std::error_code&) noexcept;
57
58// INTEGER8
59template int8_t canopen_traits<int8_t, 0x0002, true>::construct(
60 const void*, ::std::size_t, ::std::error_code&) noexcept;
61
62// INTEGER16
63template int16_t canopen_traits<int16_t, 0x0003, true>::construct(
64 const void*, ::std::size_t, ::std::error_code&) noexcept;
65
66// INTEGER32
67template int32_t canopen_traits<int32_t, 0x0004, true>::construct(
68 const void*, ::std::size_t, ::std::error_code&) noexcept;
69
70// UNSIGNED8
71template uint8_t canopen_traits<uint8_t, 0x0005, true>::construct(
72 const void*, ::std::size_t, ::std::error_code&) noexcept;
73
74// UNSIGNED16
75template uint16_t canopen_traits<uint16_t, 0x0006, true>::construct(
76 const void*, ::std::size_t, ::std::error_code&) noexcept;
77
78// UNSIGNED32
79template uint32_t canopen_traits<uint32_t, 0x0007, true>::construct(
80 const void*, ::std::size_t, ::std::error_code&) noexcept;
81
82// REAL32
83template float canopen_traits<float, 0x0008, true>::construct(
84 const void*, ::std::size_t, ::std::error_code&) noexcept;
85
86// VISIBLE_STRING
87// OCTET_STRING
88// UNICODE_STRING
89// TIME_OF_DAY
90// TIME_DIFFERENCE
91// DOMAIN
92// INTEGER24
93
94// REAL64
95template double canopen_traits<double, 0x0011, true>::construct(
96 const void*, ::std::size_t, ::std::error_code&) noexcept;
97
98// INTEGER40
99// INTEGER48
100// INTEGER56
101
102// INTEGER64
103template int64_t canopen_traits<int64_t, 0x0015, true>::construct(
104 const void*, ::std::size_t, ::std::error_code&) noexcept;
105
106// UNSIGNED24
107// UNSIGNED40
108// UNSIGNED48
109// UNSIGNED56
110
111// UNSIGNED64
112template uint64_t canopen_traits<uint64_t, 0x001b, true>::construct(
113 const void*, ::std::size_t, ::std::error_code&) noexcept;
114
115#endif // DOXYGEN_SHOULD_SKIP_THIS
116
117} // namespace detail
118
119char*
120canopen_traits<::std::string>::construct(const void* p, ::std::size_t n,
121 ::std::error_code& ec) noexcept {
122 char* val = nullptr;
123 uint32_t ac = co_val_read_sdo(index, &val, p, n);
124 if (ac)
125 ec = make_error_code(static_cast<lely::canopen::SdoErrc>(ac));
126 else
127 ec.clear();
128 return val;
129}
130
131void
132canopen_traits<::std::string>::destroy(char*& val) noexcept {
133 co_val_fini(index, &val);
134}
135
136char*
137canopen_traits<::std::string>::to_c_type(const char* vs,
138 ::std::error_code& ec) noexcept {
139 char* val = nullptr;
140 int errsv = get_errc();
141 set_errc(0);
142 if (!vs || co_val_init_vs(&val, vs))
143 ec.clear();
144 else
146 set_errc(errsv);
147 return val;
148}
149
150uint8_t*
151canopen_traits<::std::vector<uint8_t>>::construct(
152 const void* p, ::std::size_t n, ::std::error_code& ec) noexcept {
153 uint8_t* val = nullptr;
154 uint32_t ac = co_val_read_sdo(index, &val, p, n);
155 if (ac)
156 ec = make_error_code(static_cast<lely::canopen::SdoErrc>(ac));
157 else
158 ec.clear();
159 return val;
160}
161
162void
163canopen_traits<::std::vector<uint8_t>>::destroy(uint8_t*& val) noexcept {
164 co_val_fini(index, &val);
165}
166
167uint8_t*
168canopen_traits<::std::vector<uint8_t>>::to_c_type(
169 const uint8_t* os, ::std::size_t n, ::std::error_code& ec) noexcept {
170 uint8_t* val = nullptr;
171 int errsv = get_errc();
172 set_errc(0);
173 if (!os || co_val_init_os(&val, os, n))
174 ec.clear();
175 else
177 set_errc(errsv);
178 return val;
179}
180
181::std::size_t
182canopen_traits<::std::vector<uint8_t>>::size(const uint8_t* val) noexcept {
183 return val ? co_val_sizeof(index, &val) : 0;
184}
185
186char16_t*
187canopen_traits<::std::basic_string<char16_t>>::construct(
188 const void* p, ::std::size_t n, ::std::error_code& ec) noexcept {
189 char16_t* val = nullptr;
190 uint32_t ac = co_val_read_sdo(index, &val, p, n);
191 if (ac)
192 ec = make_error_code(static_cast<lely::canopen::SdoErrc>(ac));
193 else
194 ec.clear();
195 return val;
196}
197
198void
199canopen_traits<::std::basic_string<char16_t>>::destroy(
200 char16_t*& val) noexcept {
201 co_val_fini(index, &val);
202}
203
204char16_t*
205canopen_traits<::std::basic_string<char16_t>>::to_c_type(
206 const char16_t* us, ::std::error_code& ec) noexcept {
207 char16_t* val = nullptr;
208 int errsv = get_errc();
209 set_errc(0);
210 if (!us || co_val_init_us(&val, us))
211 ec.clear();
212 else
214 set_errc(errsv);
215 return val;
216}
217
218} // namespace canopen
219
220} // namespace lely
This header file is part of the C++ CANopen application library; it contains the CANopen type traits.
This is the internal header file of the C++ CANopen application library.
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:932
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:944
This header file is part of the utilities library; it contains C++ convenience functions for creating...
SdoErrc
The SDO abort codes.
Definition: sdo_error.hpp:42
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
This header file is part of the C++ CANopen application library; it contains the SDO error declaratio...
This header file is part of the CANopen library; it contains the CANopen value declarations.
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
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
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_unsigned32_t co_val_read_sdo(co_unsigned16_t type, void *val, const void *ptr, size_t n)
Reads a value of the specified data type from an SDO buffer.
Definition: val.c:767
void co_val_fini(co_unsigned16_t type, void *val)
Finalizes a value of the specified data type.
Definition: val.c:249
int co_val_init_vs(char **val, const char *vs)
Initializes an array of visible characters (CO_DEFTYPE_VISIBLE_STRING).
Definition: val.c:146