| Fast CDR Version 2.3.5 Fast CDR

Loading...
Searching...
No Matches
FastCdr.h
1// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef _FASTCDR_FASTCDR_H_
16#define _FASTCDR_FASTCDR_H_
17
18#include <array>
19#include <cstdint>
20#include <cstring>
21#include <string>
22#include <vector>
23
24#if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
25#include <malloc.h>
26#else
27#include <stdlib.h>
28#endif // if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
29
30#include "fastcdr_dll.h"
31#include "FastBuffer.h"
32#include "exceptions/NotEnoughMemoryException.h"
33#include "exceptions/BadParamException.h"
34
35 namespace eprosima {
36 namespace fastcdr {
42 class Cdr_DllAPI FastCdr
43{
44 public:
45
49 class Cdr_DllAPI state
50 {
51 friend class FastCdr;
52
53 public:
54
58 state(
59 const FastCdr& fastcdr);
60
64 state(
65 const state&);
66
67 private:
68
69 state& operator =(
70 const state&) = delete;
71
73 const FastBuffer::iterator current_position_;
74 };
75
81 FastCdr(
82 FastBuffer& cdr_buffer);
83
89 bool jump(
90 size_t num_bytes);
91
95 void reset();
96
101 char* get_current_position();
102
107 inline size_t get_serialized_data_length() const
108 {
109 return current_position_ - cdr_buffer_.begin();
110 }
111
116 FastCdr::state get_state();
117
122 void set_state(
123 FastCdr::state& state);
124
131 inline FastCdr& operator <<(
132 const uint8_t octet_t)
133 {
134 return serialize(octet_t);
135 }
136
143 inline FastCdr& operator <<(
144 const char char_t)
145 {
146 return serialize(char_t);
147 }
148
155 inline FastCdr& operator <<(
156 const int8_t int8)
157 {
158 return serialize(int8);
159 }
160
167 inline FastCdr& operator <<(
168 const uint16_t ushort_t)
169 {
170 return serialize(ushort_t);
171 }
172
179 inline FastCdr& operator <<(
180 const int16_t short_t)
181 {
182 return serialize(short_t);
183 }
184
191 inline FastCdr& operator <<(
192 const uint32_t ulong_t)
193 {
194 return serialize(ulong_t);
195 }
196
203 inline FastCdr& operator <<(
204 const int32_t long_t)
205 {
206 return serialize(long_t);
207 }
208
215 inline FastCdr& operator <<(
216 const wchar_t wchar)
217 {
218 return serialize(wchar);
219 }
220
227 inline FastCdr& operator <<(
228 const uint64_t ulonglong_t)
229 {
230 return serialize(ulonglong_t);
231 }
232
239 inline FastCdr& operator <<(
240 const int64_t longlong_t)
241 {
242 return serialize(longlong_t);
243 }
244
251 inline FastCdr& operator <<(
252 const float float_t)
253 {
254 return serialize(float_t);
255 }
256
263 inline FastCdr& operator <<(
264 const double double_t)
265 {
266 return serialize(double_t);
267 }
268
275 inline FastCdr& operator <<(
276 const long double ldouble_t)
277 {
278 return serialize(ldouble_t);
279 }
280
287 inline FastCdr& operator <<(
288 const bool bool_t)
289 {
290 return serialize(bool_t);
291 }
292
299 inline FastCdr& operator <<(
300 const char* string_t)
301 {
302 return serialize(string_t);
303 }
304
311 inline FastCdr& operator <<(
312 const wchar_t* string_t)
313 {
314 return serialize(string_t);
315 }
316
323 inline FastCdr& operator <<(
324 const std::string& string_t)
325 {
326 return serialize(string_t);
327 }
328
335 inline FastCdr& operator <<(
336 const std::wstring& string_t)
337 {
338 return serialize(string_t);
339 }
340
347 template<class _T, size_t _Size>
348 inline FastCdr& operator <<(
349 const std::array<_T, _Size>& array_t)
350 {
351 return serialize<_T, _Size>(array_t);
352 }
353
360 template<class _T>
361 inline FastCdr& operator <<(
362 const std::vector<_T>& vector_t)
363 {
364 return serialize<_T>(vector_t);
365 }
366
373 template<class _T>
374 inline FastCdr& operator <<(
375 const _T& type_t)
376 {
377 type_t.serialize(*this);
378 return *this;
379 }
380
387 inline FastCdr& operator >>(
388 uint8_t& octet_t)
389 {
390 return deserialize(octet_t);
391 }
392
399 inline FastCdr& operator >>(
400 char& char_t)
401 {
402 return deserialize(char_t);
403 }
404
411 inline FastCdr& operator >>(
412 int8_t& int8)
413 {
414 return deserialize(int8);
415 }
416
423 inline FastCdr& operator >>(
424 uint16_t& ushort_t)
425 {
426 return deserialize(ushort_t);
427 }
428
435 inline FastCdr& operator >>(
436 int16_t& short_t)
437 {
438 return deserialize(short_t);
439 }
440
447 inline FastCdr& operator >>(
448 uint32_t& ulong_t)
449 {
450 return deserialize(ulong_t);
451 }
452
459 inline FastCdr& operator >>(
460 int32_t& long_t)
461 {
462 return deserialize(long_t);
463 }
464
471 inline FastCdr& operator >>(
472 wchar_t& wchar)
473 {
474 return deserialize(wchar);
475 }
476
483 inline FastCdr& operator >>(
484 uint64_t& ulonglong_t)
485 {
486 return deserialize(ulonglong_t);
487 }
488
495 inline FastCdr& operator >>(
496 int64_t& longlong_t)
497 {
498 return deserialize(longlong_t);
499 }
500
507 inline FastCdr& operator >>(
508 float& float_t)
509 {
510 return deserialize(float_t);
511 }
512
519 inline FastCdr& operator >>(
520 double& double_t)
521 {
522 return deserialize(double_t);
523 }
524
531 inline FastCdr& operator >>(
532 long double& ldouble_t)
533 {
534 return deserialize(ldouble_t);
535 }
536
544 inline FastCdr& operator >>(
545 bool& bool_t)
546 {
547 return deserialize(bool_t);
548 }
549
559 inline FastCdr& operator >>(
560 char*& string_t)
561 {
562 return deserialize(string_t);
563 }
564
571 inline FastCdr& operator >>(
572 std::string& string_t)
573 {
574 return deserialize(string_t);
575 }
576
583 inline FastCdr& operator >>(
584 std::wstring& string_t)
585 {
586 return deserialize(string_t);
587 }
588
595 template<class _T, size_t _Size>
596 inline FastCdr& operator >>(
597 std::array<_T, _Size>& array_t)
598 {
599 return deserialize<_T, _Size>(array_t);
600 }
601
608 template<class _T>
609 inline FastCdr& operator >>(
610 std::vector<_T>& vector_t)
611 {
612 return deserialize<_T>(vector_t);
613 }
614
621 template<class _T>
622 inline FastCdr& operator >>(
623 _T& type_t)
624 {
625 type_t.deserialize(*this);
626 return *this;
627 }
628
635 inline
636 FastCdr& serialize(
637 const uint8_t octet_t)
638 {
639 return serialize(static_cast<char>(octet_t));
640 }
641
648 inline
649 FastCdr& serialize(
650 const char char_t)
651 {
652 if (((last_position_ - current_position_) >= sizeof(char_t)) || resize(sizeof(char_t)))
653 {
654 current_position_++ << char_t;
655 return *this;
656 }
657
658 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
659 }
660
667 inline
668 FastCdr& serialize(
669 const int8_t int8)
670 {
671 return serialize(static_cast<char>(int8));
672 }
673
680 inline
681 FastCdr& serialize(
682 const uint16_t ushort_t)
683 {
684 return serialize(static_cast<int16_t>(ushort_t));
685 }
686
693 inline
694 FastCdr& serialize(
695 const int16_t short_t)
696 {
697 if (((last_position_ - current_position_) >= sizeof(short_t)) || resize(sizeof(short_t)))
698 {
699 current_position_ << short_t;
700 current_position_ += sizeof(short_t);
701
702 return *this;
703 }
704
705 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
706 }
707
714 inline
715 FastCdr& serialize(
716 const uint32_t ulong_t)
717 {
718 return serialize(static_cast<int32_t>(ulong_t));
719 }
720
727 inline
728 FastCdr& serialize(
729 const int32_t long_t)
730 {
731 if (((last_position_ - current_position_) >= sizeof(long_t)) || resize(sizeof(long_t)))
732 {
733 current_position_ << long_t;
734 current_position_ += sizeof(long_t);
735
736 return *this;
737 }
738
739 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
740 }
741
748 inline
749 FastCdr& serialize(
750 const wchar_t wchar)
751 {
752 return serialize(static_cast<uint32_t>(wchar));
753 }
754
761 inline
762 FastCdr& serialize(
763 const uint64_t ulonglong_t)
764 {
765 return serialize(static_cast<int64_t>(ulonglong_t));
766 }
767
774 inline
775 FastCdr& serialize(
776 const int64_t longlong_t)
777 {
778 if (((last_position_ - current_position_) >= sizeof(longlong_t)) || resize(sizeof(longlong_t)))
779 {
780 current_position_ << longlong_t;
781 current_position_ += sizeof(longlong_t);
782
783 return *this;
784 }
785
786 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
787 }
788
795 inline
796 FastCdr& serialize(
797 const float float_t)
798 {
799 if (((last_position_ - current_position_) >= sizeof(float_t)) || resize(sizeof(float_t)))
800 {
801 current_position_ << float_t;
802 current_position_ += sizeof(float_t);
803
804 return *this;
805 }
806
807 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
808 }
809
816 inline
817 FastCdr& serialize(
818 const double double_t)
819 {
820 if (((last_position_ - current_position_) >= sizeof(double_t)) || resize(sizeof(double_t)))
821 {
822 current_position_ << double_t;
823 current_position_ += sizeof(double_t);
824
825 return *this;
826 }
827
828 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
829 }
830
837 inline
838 FastCdr& serialize(
839 const long double ldouble_t)
840 {
841 if (((last_position_ - current_position_) >= sizeof(ldouble_t)) || resize(sizeof(ldouble_t)))
842 {
843 current_position_ << ldouble_t;
844#if defined(_WIN32)
845 current_position_ += sizeof(ldouble_t);
846 current_position_ << static_cast<long double>(0);
847#endif // if defined(_WIN32)
848 current_position_ += sizeof(ldouble_t);
849
850 return *this;
851 }
852
853 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
854 }
855
862 FastCdr& serialize(
863 const bool bool_t);
864
871 FastCdr& serialize(
872 const char* string_t);
873
880 FastCdr& serialize(
881 const wchar_t* string_t);
882
890 inline
891 FastCdr& serialize(
892 const std::string& string_t)
893 {
894 // Check there are no null characters in the string.
895 const char* c_str = string_t.c_str();
896 const auto str_len = strlen(c_str);
897 if (string_t.size() > str_len)
898 {
899 throw exception::BadParamException("The string contains null characters");
900 }
901
902 return serialize(c_str);
903 }
904
911 inline
912 FastCdr& serialize(
913 const std::wstring& string_t)
914 {
915 return serialize(string_t.c_str());
916 }
917
924 template<class _T, size_t _Size>
925 inline FastCdr& serialize(
926 const std::array<_T, _Size>& array_t)
927 {
928 return serialize_array(array_t.data(), array_t.size());
929 }
930
937 template<class _T = bool>
938 FastCdr& serialize(
939 const std::vector<bool>& vector_t)
940 {
941 return serialize_bool_sequence(vector_t);
942 }
943
950 template<class _T>
951 FastCdr& serialize(
952 const std::vector<_T>& vector_t)
953 {
954 state state_before_error(*this);
955
956 *this << static_cast<int32_t>(vector_t.size());
957
958 try
959 {
960 return serialize_array(vector_t.data(), vector_t.size());
961 }
962 catch (eprosima::fastcdr::exception::Exception& ex)
963 {
964 set_state(state_before_error);
965 ex.raise();
966 }
967
968 return *this;
969 }
970
971#ifdef _MSC_VER
978 template<>
979 FastCdr& serialize<bool>(
980 const std::vector<bool>& vector_t)
981 {
982 return serialize_bool_sequence(vector_t);
983 }
984
985#endif // ifdef _MSC_VER
986
993 template<class _T>
994 inline FastCdr& serialize(
995 const _T& type_t)
996 {
997 type_t.serialize(*this);
998 return *this;
999 }
1000
1008 inline
1009 FastCdr& serialize_array(
1010 const uint8_t* octet_t,
1011 size_t num_elements)
1012 {
1013 return serialize_array(reinterpret_cast<const char*>(octet_t), num_elements);
1014 }
1015
1023 FastCdr& serialize_array(
1024 const char* char_t,
1025 size_t num_elements);
1026
1034 inline
1035 FastCdr& serialize_array(
1036 const int8_t* int8,
1037 size_t num_elements)
1038 {
1039 return serialize_array(reinterpret_cast<const char*>(int8), num_elements);
1040 }
1041
1049 inline
1050 FastCdr& serialize_array(
1051 const uint16_t* ushort_t,
1052 size_t num_elements)
1053 {
1054 return serialize_array(reinterpret_cast<const int16_t*>(ushort_t), num_elements);
1055 }
1056
1064 FastCdr& serialize_array(
1065 const int16_t* short_t,
1066 size_t num_elements);
1067
1075 inline
1076 FastCdr& serialize_array(
1077 const uint32_t* ulong_t,
1078 size_t num_elements)
1079 {
1080 return serialize_array(reinterpret_cast<const int32_t*>(ulong_t), num_elements);
1081 }
1082
1090 FastCdr& serialize_array(
1091 const int32_t* long_t,
1092 size_t num_elements);
1093
1101 FastCdr& serialize_array(
1102 const wchar_t* wchar,
1103 size_t num_elements);
1104
1112 inline
1113 FastCdr& serialize_array(
1114 const uint64_t* ulonglong_t,
1115 size_t num_elements)
1116 {
1117 return serialize_array(reinterpret_cast<const int64_t*>(ulonglong_t), num_elements);
1118 }
1119
1127 FastCdr& serialize_array(
1128 const int64_t* longlong_t,
1129 size_t num_elements);
1130
1138 FastCdr& serialize_array(
1139 const float* float_t,
1140 size_t num_elements);
1141
1149 FastCdr& serialize_array(
1150 const double* double_t,
1151 size_t num_elements);
1152
1160 FastCdr& serialize_array(
1161 const long double* ldouble_t,
1162 size_t num_elements);
1163
1171 FastCdr& serialize_array(
1172 const bool* bool_t,
1173 size_t num_elements);
1174
1182 inline
1183 FastCdr& serialize_array(
1184 const std::string* string_t,
1185 size_t num_elements)
1186 {
1187 for (size_t count = 0; count < num_elements; ++count)
1188 {
1189 serialize(string_t[count].c_str());
1190 }
1191 return *this;
1192 }
1193
1201 inline
1202 FastCdr& serialize_array(
1203 const std::wstring* string_t,
1204 size_t num_elements)
1205 {
1206 for (size_t count = 0; count < num_elements; ++count)
1207 {
1208 serialize(string_t[count].c_str());
1209 }
1210 return *this;
1211 }
1212
1220 template<class _T>
1221 FastCdr& serialize_array(
1222 const std::vector<_T>* vector_t,
1223 size_t num_elements)
1224 {
1225 for (size_t count = 0; count < num_elements; ++count)
1226 {
1227 serialize(vector_t[count]);
1228 }
1229 return *this;
1230 }
1231
1239 template<class _T>
1240 FastCdr& serialize_array(
1241 const _T* type_t,
1242 size_t num_elements)
1243 {
1244 for (size_t count = 0; count < num_elements; ++count)
1245 {
1246 type_t[count].serialize(*this);
1247 }
1248 return *this;
1249 }
1250
1258 template<class _T>
1259 FastCdr& serialize_sequence(
1260 const _T* sequence_t,
1261 size_t num_elements)
1262 {
1263 state state_before_error(*this);
1264
1265 serialize(static_cast<int32_t>(num_elements));
1266
1267 try
1268 {
1269 return serialize_array(sequence_t, num_elements);
1270 }
1271 catch (eprosima::fastcdr::exception::Exception& ex)
1272 {
1273 set_state(state_before_error);
1274 ex.raise();
1275 }
1276
1277 return *this;
1278 }
1279
1286 inline
1287 FastCdr& deserialize(
1288 uint8_t& octet_t)
1289 {
1290 return deserialize(reinterpret_cast<char&>(octet_t));
1291 }
1292
1299 inline
1300 FastCdr& deserialize(
1301 char& char_t)
1302 {
1303 if ((last_position_ - current_position_) >= sizeof(char_t))
1304 {
1305 current_position_++ >> char_t;
1306 return *this;
1307 }
1308
1309 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1310 }
1311
1318 inline
1319 FastCdr& deserialize(
1320 int8_t& int8)
1321 {
1322 return deserialize(reinterpret_cast<char&>(int8));
1323 }
1324
1331 inline
1332 FastCdr& deserialize(
1333 uint16_t& ushort_t)
1334 {
1335 return deserialize(reinterpret_cast<int16_t&>(ushort_t));
1336 }
1337
1344 inline
1345 FastCdr& deserialize(
1346 int16_t& short_t)
1347 {
1348 if ((last_position_ - current_position_) >= sizeof(short_t))
1349 {
1350 current_position_ >> short_t;
1351 current_position_ += sizeof(short_t);
1352
1353 return *this;
1354 }
1355
1356 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1357 }
1358
1365 inline
1366 FastCdr& deserialize(
1367 uint32_t& ulong_t)
1368 {
1369 return deserialize(reinterpret_cast<int32_t&>(ulong_t));
1370 }
1371
1378 inline
1379 FastCdr& deserialize(
1380 int32_t& long_t)
1381 {
1382 if ((last_position_ - current_position_) >= sizeof(long_t))
1383 {
1384 current_position_ >> long_t;
1385 current_position_ += sizeof(long_t);
1386
1387 return *this;
1388 }
1389
1390 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1391 }
1392
1399 inline
1400 FastCdr& deserialize(
1401 wchar_t& wchar)
1402 {
1403 uint32_t ret;
1404 deserialize(ret);
1405 wchar = static_cast<wchar_t>(ret);
1406 return *this;
1407 }
1408
1415 inline
1416 FastCdr& deserialize(
1417 uint64_t& ulonglong_t)
1418 {
1419 return deserialize(reinterpret_cast<int64_t&>(ulonglong_t));
1420 }
1421
1428 inline
1429 FastCdr& deserialize(
1430 int64_t& longlong_t)
1431 {
1432 if ((last_position_ - current_position_) >= sizeof(longlong_t))
1433 {
1434 current_position_ >> longlong_t;
1435 current_position_ += sizeof(longlong_t);
1436
1437 return *this;
1438 }
1439
1440 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1441 }
1442
1449 inline
1450 FastCdr& deserialize(
1451 float& float_t)
1452 {
1453 if ((last_position_ - current_position_) >= sizeof(float_t))
1454 {
1455 current_position_ >> float_t;
1456 current_position_ += sizeof(float_t);
1457
1458 return *this;
1459 }
1460
1461 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1462 }
1463
1470 inline
1471 FastCdr& deserialize(
1472 double& double_t)
1473 {
1474 if ((last_position_ - current_position_) >= sizeof(double_t))
1475 {
1476 current_position_ >> double_t;
1477 current_position_ += sizeof(double_t);
1478
1479 return *this;
1480 }
1481
1482 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1483 }
1484
1491 inline
1492 FastCdr& deserialize(
1493 long double& ldouble_t)
1494 {
1495 if ((last_position_ - current_position_) >= sizeof(ldouble_t))
1496 {
1497 current_position_ >> ldouble_t;
1498 current_position_ += sizeof(ldouble_t);
1499#if defined(_WIN32)
1500 current_position_ += sizeof(ldouble_t);
1501#endif // if defined(_WIN32)
1502
1503 return *this;
1504 }
1505
1506 throw exception::NotEnoughMemoryException(exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
1507 }
1508
1516 FastCdr& deserialize(
1517 bool& bool_t);
1518
1528 FastCdr& deserialize(
1529 char*& string_t);
1530
1540 FastCdr& deserialize(
1541 wchar_t*& string_t);
1542
1549 inline
1550 FastCdr& deserialize(
1551 std::string& string_t)
1552 {
1553 uint32_t length = 0;
1554 const char* str = read_string(length);
1555 string_t.assign(str, length);
1556 return *this;
1557 }
1558
1565 inline
1566 FastCdr& deserialize(
1567 std::wstring& string_t)
1568 {
1569 uint32_t length = 0;
1570 string_t = read_wstring(length);
1571 return *this;
1572 }
1573
1580 template<class _T, size_t _Size>
1581 inline FastCdr& deserialize(
1582 std::array<_T, _Size>& array_t)
1583 {
1584 return deserialize_array(array_t.data(), array_t.size());
1585 }
1586
1593 template<class _T = bool>
1594 FastCdr& deserialize(
1595 std::vector<bool>& vector_t)
1596 {
1597 return deserialize_bool_sequence(vector_t);
1598 }
1599
1606 template<class _T>
1607 FastCdr& deserialize(
1608 std::vector<_T>& vector_t)
1609 {
1610 uint32_t sequence_length = 0;
1611 state state_before_error(*this);
1612
1613 *this >> sequence_length;
1614
1615 try
1616 {
1617 vector_t.resize(sequence_length);
1618 return deserialize_array(vector_t.data(), vector_t.size());
1619 }
1620 catch (eprosima::fastcdr::exception::Exception& ex)
1621 {
1622 set_state(state_before_error);
1623 ex.raise();
1624 }
1625
1626 return *this;
1627 }
1628
1629#ifdef _MSC_VER
1636 template<>
1637 FastCdr& deserialize<bool>(
1638 std::vector<bool>& vector_t)
1639 {
1640 return deserialize_bool_sequence(vector_t);
1641 }
1642
1643#endif // ifdef _MSC_VER
1644
1651 template<class _T>
1652 inline FastCdr& deserialize(
1653 _T& type_t)
1654 {
1655 type_t.deserialize(*this);
1656 return *this;
1657 }
1658
1666 inline
1667 FastCdr& deserialize_array(
1668 uint8_t* octet_t,
1669 size_t num_elements)
1670 {
1671 return deserialize_array(reinterpret_cast<char*>(octet_t), num_elements);
1672 }
1673
1681 FastCdr& deserialize_array(
1682 char* char_t,
1683 size_t num_elements);
1684
1692 inline
1693 FastCdr& deserialize_array(
1694 int8_t* int8,
1695 size_t num_elements)
1696 {
1697 return deserialize_array(reinterpret_cast<char*>(int8), num_elements);
1698 }
1699
1707 inline
1708 FastCdr& deserialize_array(
1709 uint16_t* ushort_t,
1710 size_t num_elements)
1711 {
1712 return deserialize_array(reinterpret_cast<int16_t*>(ushort_t), num_elements);
1713 }
1714
1722 FastCdr& deserialize_array(
1723 int16_t* short_t,
1724 size_t num_elements);
1725
1733 inline
1734 FastCdr& deserialize_array(
1735 uint32_t* ulong_t,
1736 size_t num_elements)
1737 {
1738 return deserialize_array(reinterpret_cast<int32_t*>(ulong_t), num_elements);
1739 }
1740
1748 FastCdr& deserialize_array(
1749 int32_t* long_t,
1750 size_t num_elements);
1751
1759 FastCdr& deserialize_array(
1760 wchar_t* wchar,
1761 size_t num_elements);
1762
1770 inline
1771 FastCdr& deserialize_array(
1772 uint64_t* ulonglong_t,
1773 size_t num_elements)
1774 {
1775 return deserialize_array(reinterpret_cast<int64_t*>(ulonglong_t), num_elements);
1776 }
1777
1785 FastCdr& deserialize_array(
1786 int64_t* longlong_t,
1787 size_t num_elements);
1788
1796 FastCdr& deserialize_array(
1797 float* float_t,
1798 size_t num_elements);
1799
1807 FastCdr& deserialize_array(
1808 double* double_t,
1809 size_t num_elements);
1810
1818 FastCdr& deserialize_array(
1819 long double* ldouble_t,
1820 size_t num_elements);
1821
1829 FastCdr& deserialize_array(
1830 bool* bool_t,
1831 size_t num_elements);
1832
1840 inline
1841 FastCdr& deserialize_array(
1842 std::string* string_t,
1843 size_t num_elements)
1844 {
1845 for (size_t count = 0; count < num_elements; ++count)
1846 {
1847 deserialize(string_t[count]);
1848 }
1849 return *this;
1850 }
1851
1859 inline
1860 FastCdr& deserialize_array(
1861 std::wstring* string_t,
1862 size_t num_elements)
1863 {
1864 for (size_t count = 0; count < num_elements; ++count)
1865 {
1866 deserialize(string_t[count]);
1867 }
1868 return *this;
1869 }
1870
1878 template<class _T>
1879 FastCdr& deserialize_array(
1880 std::vector<_T>* vector_t,
1881 size_t num_elements)
1882 {
1883 for (size_t count = 0; count < num_elements; ++count)
1884 {
1885 deserialize(vector_t[count]);
1886 }
1887 return *this;
1888 }
1889
1897 template<class _T>
1898 FastCdr& deserialize_array(
1899 _T* type_t,
1900 size_t num_elements)
1901 {
1902 for (size_t count = 0; count < num_elements; ++count)
1903 {
1904 type_t[count].deserialize(*this);
1905 }
1906 return *this;
1907 }
1908
1918 template<class _T = std::string>
1919 FastCdr& deserialize_sequence(
1920 std::string*& sequence_t,
1921 size_t& num_elements)
1922 {
1923 return deserialize_string_sequence(sequence_t, num_elements);
1924 }
1925
1935 template<class _T = std::wstring>
1936 FastCdr& deserialize_sequence(
1937 std::wstring*& sequence_t,
1938 size_t& num_elements)
1939 {
1940 return deserialize_wstring_sequence(sequence_t, num_elements);
1941 }
1942
1952 template<class _T>
1953 FastCdr& deserialize_sequence(
1954 _T*& sequence_t,
1955 size_t& num_elements)
1956 {
1957 uint32_t sequence_length = 0;
1958 state state_before_error(*this);
1959
1960 deserialize(sequence_length);
1961
1962 try
1963 {
1964 sequence_t = reinterpret_cast<_T*>(calloc(sequence_length, sizeof(_T)));
1965 deserialize_array(sequence_t, sequence_length);
1966 }
1967 catch (eprosima::fastcdr::exception::Exception& ex)
1968 {
1969 free(sequence_t);
1970 sequence_t = NULL;
1971 set_state(state_before_error);
1972 ex.raise();
1973 }
1974
1975 num_elements = sequence_length;
1976 return *this;
1977 }
1978
1979#ifdef _MSC_VER
1989 template<>
1990 FastCdr& deserialize_sequence<std::string>(
1991 std::string*& sequence_t,
1992 size_t& num_elements)
1993 {
1994 return deserialize_string_sequence(sequence_t, num_elements);
1995 }
1996
2006 template<>
2007 FastCdr& deserialize_sequence<std::wstring>(
2008 std::wstring*& sequence_t,
2009 size_t& num_elements)
2010 {
2011 return deserialize_wstring_sequence(sequence_t, num_elements);
2012 }
2013
2014#endif // ifdef _MSC_VER
2015
2016 private:
2017
2018 FastCdr(
2019 const FastCdr&) = delete;
2020
2021 FastCdr& operator =(
2022 const FastCdr&) = delete;
2023
2024 FastCdr& serialize_bool_sequence(
2025 const std::vector<bool>& vector_t);
2026
2027 FastCdr& deserialize_bool_sequence(
2028 std::vector<bool>& vector_t);
2029
2030 FastCdr& deserialize_string_sequence(
2031 std::string*& sequence_t,
2032 size_t& num_elements);
2033
2034 FastCdr& deserialize_wstring_sequence(
2035 std::wstring*& sequence_t,
2036 size_t& num_elements);
2037
2045 template<class _T, size_t _Size>
2046 FastCdr& serialize_array(
2047 const std::array<_T, _Size>* array_t,
2048 size_t num_elements)
2049 {
2050 return serialize_array(array_t->data(), num_elements * array_t->size());
2051 }
2052
2060 template<class _T, size_t _Size>
2061 FastCdr& deserialize_array(
2062 std::array<_T, _Size>* array_t,
2063 size_t num_elements)
2064 {
2065 return deserialize_array(array_t->data(), num_elements * array_t->size());
2066 }
2067
2068 bool resize(
2069 size_t min_size_inc);
2070
2071 const char* read_string(
2072 uint32_t& length);
2073
2074 std::wstring read_wstring(
2075 uint32_t& length);
2076
2078 FastBuffer& cdr_buffer_;
2079
2081 FastBuffer::iterator current_position_;
2082
2084 FastBuffer::iterator last_position_;
2085};
2086} //namespace fastcdr
2087} //namespace eprosima
2088
2089#endif //_FASTCDR_FASTCDR_H_
eprosima::fastcdr::FastBuffer
This class represents a stream of bytes that contains (or will contain) serialized data.
Definition FastBuffer.h:244
eprosima::fastcdr::FastBuffer::iterator
_FastBuffer_iterator iterator
Definition FastBuffer.h:247
eprosima::fastcdr::FastCdr::state
This class stores the current state of a CDR serialization.
Definition FastCdr.h:50
eprosima::fastcdr::FastCdr::state::state
state(const FastCdr &fastcdr)
Default constructor.
eprosima::fastcdr::FastCdr::state::FastCdr
friend class FastCdr
Definition FastCdr.h:51
eprosima::fastcdr::FastCdr::state::state
state(const state &)
Copy constructor.
eprosima::fastcdr::FastCdr
This class offers an interface to serialize/deserialize some basic types using a modified CDR protoco...
Definition FastCdr.h:43
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(float *float_t, size_t num_elements)
This function deserializes an array of floats.
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const int8_t *int8, size_t num_elements)
This function serializes an array of int8_t.
Definition FastCdr.h:1035
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const wchar_t *string_t)
This function serializes a wstring.
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(std::vector< _T > *vector_t, size_t num_elements)
This function template deserializes an array of sequences.
Definition FastCdr.h:1879
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(char *&string_t)
This function deserializes a string.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(int8_t &int8)
This function deserializes an int8_t.
Definition FastCdr.h:1319
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const wchar_t wchar)
This function serializes a wide-char.
Definition FastCdr.h:749
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(uint8_t *octet_t, size_t num_elements)
This function deserializes an array of octets.
Definition FastCdr.h:1667
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const std::vector< _T > &vector_t)
This function template serializes a sequence.
Definition FastCdr.h:951
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const uint32_t *ulong_t, size_t num_elements)
This function serializes an array of unsigned longs.
Definition FastCdr.h:1076
eprosima::fastcdr::FastCdr::jump
bool jump(size_t num_bytes)
This function skips a number of bytes in the CDR stream buffer.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(wchar_t &wchar)
This function deserializes a wide-char.
Definition FastCdr.h:1400
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(std::wstring *string_t, size_t num_elements)
This function deserializes an array of wide-strings.
Definition FastCdr.h:1860
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const int32_t *long_t, size_t num_elements)
This function serializes an array of longs.
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const _T *type_t, size_t num_elements)
This function template serializes an array of non-basic type objects.
Definition FastCdr.h:1240
eprosima::fastcdr::FastCdr::deserialize_sequence
FastCdr & deserialize_sequence(_T *&sequence_t, size_t &num_elements)
This function template deserializes a raw sequence.
Definition FastCdr.h:1953
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const uint8_t octet_t)
This function serializes an octet.
Definition FastCdr.h:636
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(int16_t *short_t, size_t num_elements)
This function deserializes an array of shorts.
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(_T *type_t, size_t num_elements)
This function template deserializes an array of non-basic type objects.
Definition FastCdr.h:1898
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(double &double_t)
This function deserializes a double.
Definition FastCdr.h:1471
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const int16_t short_t)
This function serializes a short.
Definition FastCdr.h:694
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(float &float_t)
This function deserializes a float.
Definition FastCdr.h:1450
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const int16_t *short_t, size_t num_elements)
This function serializes an array of shorts.
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const wchar_t *wchar, size_t num_elements)
This function serializes an array of wide-chars.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(bool &bool_t)
This function deserializes a boolean.
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const uint16_t *ushort_t, size_t num_elements)
This function serializes an array of unsigned shorts.
Definition FastCdr.h:1050
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(uint64_t &ulonglong_t)
This function deserializes an unsigned long long.
Definition FastCdr.h:1416
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(std::array< _T, _Size > &array_t)
This function template deserializes an array.
Definition FastCdr.h:1581
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const std::string *string_t, size_t num_elements)
This function serializes an array of strings.
Definition FastCdr.h:1183
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(int64_t &longlong_t)
This function deserializes a long long.
Definition FastCdr.h:1429
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(_T &type_t)
This function template deserializes a non-basic type object.
Definition FastCdr.h:1652
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(int16_t &short_t)
This function deserializes a short.
Definition FastCdr.h:1345
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const uint8_t *octet_t, size_t num_elements)
This function serializes an array of octets.
Definition FastCdr.h:1009
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const float float_t)
This function serializes a float.
Definition FastCdr.h:796
eprosima::fastcdr::FastCdr::deserialize_sequence
FastCdr & deserialize_sequence(std::string *&sequence_t, size_t &num_elements)
This function template deserializes a string sequence.
Definition FastCdr.h:1919
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const int8_t int8)
This function serializes an int8_t.
Definition FastCdr.h:668
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(std::wstring &string_t)
This function deserializes a std::wstring.
Definition FastCdr.h:1566
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const char *char_t, size_t num_elements)
This function serializes an array of characters.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(char &char_t)
This function deserializes a character.
Definition FastCdr.h:1300
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const double double_t)
This function serializes a double.
Definition FastCdr.h:817
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const std::wstring *string_t, size_t num_elements)
This function serializes an array of wstrings.
Definition FastCdr.h:1202
eprosima::fastcdr::FastCdr::deserialize_sequence
FastCdr & deserialize_sequence(std::wstring *&sequence_t, size_t &num_elements)
This function template deserializes a wide-string sequence.
Definition FastCdr.h:1936
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const bool bool_t)
This function serializes a boolean.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(std::vector< _T > &vector_t)
This function template deserializes a sequence.
Definition FastCdr.h:1607
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const bool *bool_t, size_t num_elements)
This function serializes an array of booleans.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(long double &ldouble_t)
This function deserializes a long double.
Definition FastCdr.h:1492
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const uint64_t *ulonglong_t, size_t num_elements)
This function serializes an array of unsigned long longs.
Definition FastCdr.h:1113
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(uint32_t *ulong_t, size_t num_elements)
This function deserializes an array of unsigned longs.
Definition FastCdr.h:1734
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(wchar_t *&string_t)
This function deserializes a wide string.
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(long double *ldouble_t, size_t num_elements)
This function deserializes an array of long doubles.
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(int64_t *longlong_t, size_t num_elements)
This function deserializes an array of long longs.
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const uint32_t ulong_t)
This function serializes an unsigned long.
Definition FastCdr.h:715
eprosima::fastcdr::FastCdr::FastCdr
FastCdr(FastBuffer &cdr_buffer)
This constructor creates a eprosima::fastcdr::FastCdr object that can serialize/deserialize the assig...
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const std::wstring &string_t)
This function serializes a std::wstring.
Definition FastCdr.h:912
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const uint16_t ushort_t)
This function serializes an unsigned short.
Definition FastCdr.h:681
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const std::array< _T, _Size > &array_t)
This function template serializes an array.
Definition FastCdr.h:925
eprosima::fastcdr::FastCdr::serialize_sequence
FastCdr & serialize_sequence(const _T *sequence_t, size_t num_elements)
This function template serializes a raw sequence.
Definition FastCdr.h:1259
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const uint64_t ulonglong_t)
This function serializes an unsigned long long.
Definition FastCdr.h:762
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(int32_t &long_t)
This function deserializes a long.
Definition FastCdr.h:1379
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(uint64_t *ulonglong_t, size_t num_elements)
This function deserializes an array of unsigned long longs.
Definition FastCdr.h:1771
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const long double *ldouble_t, size_t num_elements)
This function serializes an array of long doubles.
eprosima::fastcdr::FastCdr::set_state
void set_state(FastCdr::state &state)
This function sets a previous state of the CDR stream;.
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const double *double_t, size_t num_elements)
This function serializes an array of doubles.
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const char *string_t)
This function serializes a string.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(uint16_t &ushort_t)
This function deserializes an unsigned short.
Definition FastCdr.h:1332
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(char *char_t, size_t num_elements)
This function deserializes an array of characters.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(uint32_t &ulong_t)
This function deserializes an unsigned long.
Definition FastCdr.h:1366
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const int32_t long_t)
This function serializes a long.
Definition FastCdr.h:728
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const std::vector< bool > &vector_t)
This function template serializes a sequence of booleans.
Definition FastCdr.h:938
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(int8_t *int8, size_t num_elements)
This function deserializes an array of int8_t.
Definition FastCdr.h:1693
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const int64_t *longlong_t, size_t num_elements)
This function serializes an array of long longs.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(std::vector< bool > &vector_t)
This function template deserializes a sequence of booleans.
Definition FastCdr.h:1594
eprosima::fastcdr::FastCdr::get_serialized_data_length
size_t get_serialized_data_length() const
This function returns the length of the serialized data inside the stream.
Definition FastCdr.h:107
eprosima::fastcdr::FastCdr::reset
void reset()
This function resets the current position in the buffer to the begining.
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const char char_t)
This function serializes a character.
Definition FastCdr.h:649
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(uint16_t *ushort_t, size_t num_elements)
This function deserializes an array of unsigned shorts.
Definition FastCdr.h:1708
eprosima::fastcdr::FastCdr::get_state
FastCdr::state get_state()
This function returns the current state of the CDR stream.
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const std::vector< _T > *vector_t, size_t num_elements)
This function template serializes an array of sequences.
Definition FastCdr.h:1221
eprosima::fastcdr::FastCdr::serialize_array
FastCdr & serialize_array(const float *float_t, size_t num_elements)
This function serializes an array of floats.
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(bool *bool_t, size_t num_elements)
This function deserializes an array of booleans.
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(std::string *string_t, size_t num_elements)
This function deserializes an array of strings.
Definition FastCdr.h:1841
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(double *double_t, size_t num_elements)
This function deserializes an array of doubles.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(uint8_t &octet_t)
This function deserializes an octet.
Definition FastCdr.h:1287
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(wchar_t *wchar, size_t num_elements)
This function deserializes an array of wide-chars.
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const std::string &string_t)
This function serializes a std::string.
Definition FastCdr.h:891
eprosima::fastcdr::FastCdr::get_current_position
char * get_current_position()
This function returns the current position in the CDR stream.
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const long double ldouble_t)
This function serializes a long double.
Definition FastCdr.h:838
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const int64_t longlong_t)
This function serializes a long long.
Definition FastCdr.h:775
eprosima::fastcdr::FastCdr::serialize
FastCdr & serialize(const _T &type_t)
This function template serializes a non-basic type.
Definition FastCdr.h:994
eprosima::fastcdr::FastCdr::deserialize_array
FastCdr & deserialize_array(int32_t *long_t, size_t num_elements)
This function deserializes an array of longs.
eprosima::fastcdr::FastCdr::deserialize
FastCdr & deserialize(std::string &string_t)
This function deserializes a std::string.
Definition FastCdr.h:1550
eprosima::fastcdr::exception::BadParamException
This class is thrown as an exception when an invalid parameter is being serialized.
Definition BadParamException.h:28
eprosima::fastcdr::exception::Exception
This abstract class is used to create exceptions.
Definition Exception.h:29
eprosima::fastcdr::exception::Exception::raise
virtual void raise() const =0
This function throws the object as exception.
eprosima::fastcdr::exception::NotEnoughMemoryException
This class is thrown as an exception when the buffer's internal memory reachs its size limit.
Definition NotEnoughMemoryException.h:28
eprosima::fastcdr::exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT
static const char *const NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT
Default message used in the library.
Definition NotEnoughMemoryException.h:78
eprosima::fastcdr
Definition Cdr.h:50
eprosima::fastcdr::deserialize
void deserialize(Cdr &, _T &)
eprosima::fastcdr::serialize
void serialize(Cdr &, const _T &)
eprosima
Definition Cdr.h:49