5 #ifndef __DDS__BasicCmd__ 6 #define __DDS__BasicCmd__ 15 #include <boost/uuid/uuid.hpp> 16 #include <boost/uuid/uuid_io.hpp> 22 namespace protocol_api
33 size_t dsize(
const T& _value);
39 return sizeof(uint8_t);
46 return sizeof(uint16_t);
53 return sizeof(uint32_t);
60 return sizeof(uint64_t);
65 inline size_t dsize<std::string>(
const std::string& _value)
67 return _value.size() +
sizeof(uint16_t);
72 inline size_t dsize<std::vector<uint8_t>>(
const std::vector<uint8_t>& _value)
74 return _value.size() *
sizeof(uint8_t) +
sizeof(uint32_t);
79 inline size_t dsize<std::vector<uint16_t>>(
const std::vector<uint16_t>& _value)
81 return _value.size() *
sizeof(uint16_t) +
sizeof(uint16_t);
86 inline size_t dsize<std::vector<uint32_t>>(
const std::vector<uint32_t>& _value)
88 return _value.size() *
sizeof(uint32_t) +
sizeof(uint16_t);
93 inline size_t dsize<std::vector<uint64_t>>(
const std::vector<uint64_t>& _value)
95 return _value.size() *
sizeof(uint64_t) +
sizeof(uint16_t);
100 inline size_t dsize<std::vector<std::string>>(
const std::vector<std::string>& _value)
102 size_t sum(
sizeof(uint16_t));
103 for (
const auto& v : _value)
104 sum += (v.size() +
sizeof(uint16_t));
110 inline size_t dsize<boost::uuids::uuid>(
const boost::uuids::uuid& )
112 return boost::uuids::uuid::static_size();
118 template <
typename T>
124 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
125 throw std::invalid_argument(
"readDataFromContainer");
127 *_value = (*_data)[*_nPos];
135 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
136 throw std::invalid_argument(
"readDataFromContainer");
138 *_value = (*_data)[*_nPos];
139 *_value += ((*_data)[++(*_nPos)] << 8);
149 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
150 throw std::invalid_argument(
"readDataFromContainer");
152 *_value = (*_data)[*_nPos];
153 *_value += ((*_data)[++(*_nPos)] << 8);
154 *_value += ((*_data)[++(*_nPos)] << 16);
155 *_value += ((*_data)[++(*_nPos)] << 24);
165 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
166 throw std::invalid_argument(
"readDataFromContainer");
168 *_value = (*_data)[*_nPos];
169 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 8);
170 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 16);
171 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 24);
172 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 32);
173 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 40);
174 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 48);
175 *_value += ((uint64_t)(*_data)[++(*_nPos)] << 56);
185 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
186 throw std::invalid_argument(
"readDataFromContainer");
188 if (_value->size() > std::numeric_limits<uint16_t>::max())
189 throw std::invalid_argument(
"String size can't exceed 2^16 symbols. String size: " +
190 std::to_string(_value->size()));
196 dds::misc::BYTEVector_t::const_iterator iter = _data->begin();
197 std::advance(iter, *_nPos);
198 dds::misc::BYTEVector_t::const_iterator iter_end = _data->begin();
199 std::advance(iter_end, (*_nPos + n));
200 std::copy(iter, iter_end, back_inserter(*_value));
206 inline void readData<boost::uuids::uuid>(boost::uuids::uuid* _value,
210 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
211 throw std::invalid_argument(
"readDataFromContainer");
213 dds::misc::BYTEVector_t::const_iterator iter = _data->begin();
214 std::advance(iter, *_nPos);
215 dds::misc::BYTEVector_t::const_iterator iter_end = _data->begin();
216 std::advance(iter_end, (*_nPos + boost::uuids::uuid::static_size()));
217 copy(iter, iter_end, _value->begin());
218 (*_nPos) += boost::uuids::uuid::static_size();
221 template <
typename T>
224 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
225 throw std::invalid_argument(
"readDataFromContainer");
227 if (_value->size() > std::numeric_limits<uint16_t>::max())
228 throw std::invalid_argument(
"Vector size can't exceed 2^16 symbols. Vector size: " +
229 std::to_string(_value->size()));
236 for (
size_t i = 0; i < n; ++i)
240 _value->push_back(v);
245 inline void readData<std::vector<uint8_t>>(std::vector<uint8_t>* _value,
249 if (_data ==
nullptr || _nPos ==
nullptr || _value ==
nullptr)
250 throw std::invalid_argument(
"readDataFromContainer");
252 if (_value->size() > std::numeric_limits<uint32_t>::max())
253 throw std::invalid_argument(
"Vector<uint8_t> size can't exceed 2^32 symbols. Vector size: " +
254 std::to_string(_value->size()));
259 dds::misc::BYTEVector_t::const_iterator iter = _data->begin();
260 std::advance(iter, *_nPos);
261 dds::misc::BYTEVector_t::const_iterator iter_end = _data->begin();
262 std::advance(iter_end, (*_nPos + n));
263 std::copy(iter, iter_end, back_inserter(*_value));
269 inline void readData<std::vector<uint16_t>>(std::vector<uint16_t>* _value,
273 readDataVector<uint16_t>(_value, _data, _nPos);
277 inline void readData<std::vector<uint32_t>>(std::vector<uint32_t>* _value,
281 readDataVector<uint32_t>(_value, _data, _nPos);
285 inline void readData<std::vector<uint64_t>>(std::vector<uint64_t>* _value,
289 readDataVector<uint64_t>(_value, _data, _nPos);
293 inline void readData<std::vector<std::string>>(std::vector<std::string>* _value,
297 readDataVector<std::string>(_value, _data, _nPos);
304 template <
typename T>
310 if (_data ==
nullptr)
311 throw std::invalid_argument(
"pushDataFromContainer");
313 _data->push_back(_value);
319 if (_data ==
nullptr)
320 throw std::invalid_argument(
"pushDataFromContainer");
323 _data->push_back(value & 0xFF);
324 _data->push_back(value >> 8);
330 if (_data ==
nullptr)
331 throw std::invalid_argument(
"pushDataFromContainer");
334 _data->push_back(value & 0xFF);
335 _data->push_back((value >> 8) & 0xFF);
336 _data->push_back((value >> 16) & 0xFF);
337 _data->push_back((value >> 24) & 0xFF);
343 if (_data ==
nullptr)
344 throw std::invalid_argument(
"pushDataFromContainer");
347 _data->push_back(value & 0xFF);
348 _data->push_back((value >> 8) & 0xFF);
349 _data->push_back((value >> 16) & 0xFF);
350 _data->push_back((value >> 24) & 0xFF);
351 _data->push_back((value >> 32) & 0xFF);
352 _data->push_back((value >> 40) & 0xFF);
353 _data->push_back((value >> 48) & 0xFF);
354 _data->push_back((value >> 56) & 0xFF);
360 if (_data ==
nullptr)
361 throw std::invalid_argument(
"pushDataFromContainer");
363 if (_value.size() > std::numeric_limits<uint16_t>::max())
364 throw std::invalid_argument(
"String size can't exceed 2^16 symbols. String size: " +
365 std::to_string(_value.size()));
367 uint16_t n = _value.size();
369 copy(_value.begin(), _value.end(), back_inserter(*_data));
375 if (_data ==
nullptr)
376 throw std::invalid_argument(
"pushDataFromContainer");
378 copy(_value.begin(), _value.end(), back_inserter(*_data));
381 template <
typename T>
384 if (_data ==
nullptr)
385 throw std::invalid_argument(
"pushDataFromContainer");
387 if (_value.size() > std::numeric_limits<uint16_t>::max())
388 throw std::invalid_argument(
"Vector size can't exceed 2^16 symbols. Vector size: " +
389 std::to_string(_value.size()));
392 uint16_t n = _value.size();
395 for (
const T& v : _value)
404 if (_data ==
nullptr)
405 throw std::invalid_argument(
"pushDataFromContainer");
407 if (_value.size() > std::numeric_limits<uint32_t>::max())
408 throw std::invalid_argument(
"Vector<uint32_t> size can't exceed 2^32 symbols. Vector size: " +
409 std::to_string(_value.size()));
411 uint32_t n = _value.size();
413 copy(_value.begin(), _value.end(), back_inserter(*_data));
419 pushDataVector<uint16_t>(_value, _data);
425 pushDataVector<uint32_t>(_value, _data);
431 pushDataVector<uint64_t>(_value, _data);
435 inline void pushData<std::vector<std::string>>(
const std::vector<std::string>& _value,
438 pushDataVector<std::string>(_value, _data);
455 template <
typename T>
462 template <
typename T>
474 template <
class _Owner>
479 _Owner* p = reinterpret_cast<_Owner*>(
this);
480 if (_data.size() < p->size())
482 std::stringstream ss;
483 ss <<
"Protocol message data is too short, expected " << p->size() <<
" received " << _data.size();
484 throw std::runtime_error(ss.str());
486 p->_convertFromData(_data);
490 const _Owner* p = reinterpret_cast<const _Owner*>(
this);
491 p->_convertToData(_data);
void readData< uint8_t >(uint8_t *_value, const dds::misc::BYTEVector_t *_data, size_t *_nPos)
Definition: BasicCmd.h:122
Definition: BasicCmd.h:475
void convertToData(dds::misc::BYTEVector_t *_data) const
Definition: BasicCmd.h:488
T normalizeWrite(T _value)
size_t dsize< uint8_t >(const uint8_t &)
Helper function calculating size of uint8_t.
Definition: BasicCmd.h:37
SAttachmentDataProvider & get(T &_value)
Definition: BasicCmd.h:456
void pushDataVector(const std::vector< T > &_value, dds::misc::BYTEVector_t *_data)
Definition: BasicCmd.h:382
SAttachmentDataProvider(const dds::misc::BYTEVector_t &_data)
Definition: BasicCmd.h:449
T normalizeRead(T _value)
size_t dsize(const T &_value)
All vectors (except uint8_t) have a maximum size of uint16_t i.e. 2^16. All vector<uint8_t>'s have a ...
size_t dsize< uint64_t >(const uint64_t &)
Helper function calculating size of uint64_t.
Definition: BasicCmd.h:58
void readData< uint32_t >(uint32_t *_value, const dds::misc::BYTEVector_t *_data, size_t *_nPos)
Definition: BasicCmd.h:147
void pushData< uint16_t >(const uint16_t &_value, dds::misc::BYTEVector_t *_data)
Definition: BasicCmd.h:317
void readData< uint16_t >(uint16_t *_value, const dds::misc::BYTEVector_t *_data, size_t *_nPos)
Definition: BasicCmd.h:133
std::vector< unsigned char > BYTEVector_t
An STL vector of bytes.
Definition: def.h:124
void readData< uint64_t >(uint64_t *_value, const dds::misc::BYTEVector_t *_data, size_t *_nPos)
Definition: BasicCmd.h:163
void pushData< uint32_t >(const uint32_t &_value, dds::misc::BYTEVector_t *_data)
Definition: BasicCmd.h:328
SAttachmentDataProvider(dds::misc::BYTEVector_t *_data)
Definition: BasicCmd.h:443
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
Definition: BasicCmd.h:441
size_t dsize< uint32_t >(const uint32_t &)
Helper function calculating size of uint32_t.
Definition: BasicCmd.h:51
void pushData(const T &_value, dds::misc::BYTEVector_t *_data)
void convertFromData(const dds::misc::BYTEVector_t &_data)
Definition: BasicCmd.h:477
void readData(T *_value, const dds::misc::BYTEVector_t *_data, size_t *_nPos)
Helper template function reading data from byte array.
const SAttachmentDataProvider & put(const T &_value) const
Definition: BasicCmd.h:463
void pushData< uint64_t >(const uint64_t &_value, dds::misc::BYTEVector_t *_data)
Definition: BasicCmd.h:341
size_t dsize< uint16_t >(const uint16_t &)
Helper function calculating size of uint16_t.
Definition: BasicCmd.h:44
void readDataVector(std::vector< T > *_value, const dds::misc::BYTEVector_t *_data, size_t *_nPos)
Definition: BasicCmd.h:222
INet declares helpers for Socket and Network operations.
Definition: INet.h:37
void pushData< uint8_t >(const uint8_t &_value, dds::misc::BYTEVector_t *_data)
Definition: BasicCmd.h:308