5#ifndef _MSG_SERIALIZE_H
6#define _MSG_SERIALIZE_H
34#define CHECK_UNSUPPORTED_CONTAINER
35#ifdef CHECK_UNSUPPORTED_CONTAINER
38#include <forward_list>
41#include <unordered_set>
42#include <unordered_map>
47 template <
typename U,
typename V>
50 template <
typename T,
typename Alloc>
53 template <
typename T,
typename Alloc>
56 template <
typename T,
typename Alloc>
59 template <
typename T,
typename Alloc>
62 template <
typename T,
typename Alloc>
65 template <
typename T,
typename Hash,
typename KeyEqual,
typename Alloc>
68 template <
typename Key,
typename T,
typename Hash,
typename KeyEqual,
typename Alloc>
71 template <
typename Key,
typename T,
typename Hash,
typename KeyEqual,
typename Alloc>
162 const static int n = 1;
163 const static bool le= (* (
char *)&n == 1);
170 std::istream&
readEndian(std::istream& is,
bool& littleEndian)
174 is.read((
char*) &littleEndian,
sizeof(littleEndian));
183 bool littleEndian =
LE();
185 os.write((
const char*) &littleEndian,
sizeof(littleEndian));
195 std::istream&
read (std::istream& is,
I* t_)
197 if (check_stop_parse(is))
200 if (check_pointer(is, t_))
205 std::streampos startPos = is.tellg();
207 read(is, size,
false);
210 push_stop_parse_pos(startPos + std::streampos(size));
214 pop_stop_parse_pos();
218 std::streampos endPos = is.tellg();
219 uint16_t rcvdSize =
static_cast<uint16_t
>(endPos - startPos);
225 uint16_t seekOffset = size - rcvdSize;
226 is.seekg(seekOffset, std::ios_base::cur);
239 std::istream&
read (std::istream& is, std::string& s)
241 if (check_stop_parse(is))
247 read(is, size,
false);
248 if (check_stream(is) && check_slength(is, size))
251 parseStatus(
typeid(s), s.size());
252 read_internal(is,
const_cast<char*
>(s.c_str()), size,
true);
262 std::istream&
read (std::istream& is, std::wstring& s)
264 if (check_stop_parse(is))
270 read(is, size,
false);
271 if (check_stream(is) && check_slength(is, size))
274 parseStatus(
typeid(s), s.size());
275 for (uint16_t ii = 0; ii < size; ii++)
278 int offset =
sizeof(wchar_t) - WCHAR_SIZE;
279 read_internal(is,
reinterpret_cast<char*
>(&c) + offset, WCHAR_SIZE);
291 std::istream&
read (std::istream& is,
char* str)
293 if (check_stop_parse(is))
299 read(is, size,
false);
300 if (check_stream(is) && check_slength(is, size))
302 if (check_pointer(is, str))
304 parseStatus(
typeid(str), size);
305 read_internal(is, str, size,
true);
320 std::istream&
read (std::istream& is, std::vector<bool>& container)
322 if (check_stop_parse(is))
329 read(is, size,
false);
330 if (check_stream(is) && check_container_size(is, size))
332 parseStatus(
typeid(container), size);
333 for (uint16_t i = 0; i < size; ++i)
337 container.push_back(t);
349 std::ostream&
write (std::ostream& os,
I* t_)
351 if (check_pointer(os, t_))
353 uint16_t elementSize = 0;
356 std::streampos elementSizePos = os.tellp();
357 write(os, elementSize,
false);
360 t_->
write(*
this, os);
365 std::streampos currentPos = os.tellp();
366 os.seekp(elementSizePos);
367 elementSize =
static_cast<uint16_t
>(currentPos - elementSizePos);
368 write(os, elementSize,
false);
369 os.seekp(currentPos);
380 std::ostream&
write(std::ostream& os,
const std::string& s)
382 uint16_t size =
static_cast<uint16_t
>(s.size());
384 write(os, size,
false);
385 if (check_stream(os) && check_slength(os, size))
387 write_internal(os, s.c_str(), size,
true);
396 std::ostream&
write(std::ostream& os, std::string& s)
398 return write(os,
static_cast<const std::string&
>(s));
405 std::ostream&
write (std::ostream& os,
const std::wstring& s)
407 uint16_t size =
static_cast<uint16_t
>(s.size());
409 write(os, size,
false);
410 if (check_stream(os) && check_slength(os, size))
412 for (uint16_t ii = 0; ii < size; ii++)
415 int offset =
sizeof(wchar_t) - WCHAR_SIZE;
416 write_internal(os,
reinterpret_cast<char*
>(&c) + offset, WCHAR_SIZE);
426 std::ostream&
write (std::ostream& os, std::wstring& s)
428 return write(os,
static_cast<const std::wstring&
>(s));
435 std::ostream&
write(std::ostream& os,
char* str)
437 return write(os,
static_cast<const char*
>(str));
444 std::ostream&
write (std::ostream& os,
const char* str)
446 if (check_pointer(os, str))
448 uint16_t size =
static_cast<uint16_t
>(strlen(str)) + 1;
450 write(os, size,
false);
451 if (check_stream(os) && check_slength(os, size))
453 write_internal (os, str, size,
true);
467 std::ostream&
write (std::ostream& os, std::vector<bool>& container)
469 uint16_t size =
static_cast<uint16_t
>(container.size());
471 write(os, size,
false);
472 if (check_stream(os) && check_container_size(os, size))
474 for (
const bool& c : container)
487 std::istream&
read(std::istream& is, T &t_,
bool readPrependedType =
true)
491 static_assert(!(std::is_pointer<T>::value &&
492 (std::is_arithmetic<typename std::remove_pointer<T>::type>::value ||
493 std::is_class<typename std::remove_pointer<T>::type>::value)),
494 "T cannot be a pointer to a built-in or custom data type");
496 if (check_stop_parse(is))
500 if (std::is_class<T>::value ==
false)
503 if (std::is_pointer<T>::value ==
false)
505 if (readPrependedType)
513 if (readPrependedType)
514 parseStatus(
typeid(t_));
515 read_internal(is, (
char*)&t_,
sizeof (t_));
522 is.setstate(std::ios::failbit);
529 parseStatus(
typeid(t_));
540 std::ostream&
write(std::ostream& os, T &t_,
bool prependType =
true)
544 static_assert(!(std::is_pointer<T>::value &&
545 (std::is_arithmetic<typename std::remove_pointer<T>::type>::value ||
546 std::is_class<typename std::remove_pointer<T>::type>::value)),
547 "T cannot be a pointer to a built-in or custom data type");
550#if defined(_MSVC_LANG) && _MSVC_LANG >= 201703L || __cplusplus >= 201703L
552 if constexpr (std::is_class<T>::value)
555 static_assert(std::is_base_of<serialize::I, T>::value,
"T must inherit from serialize::I");
563 if (std::is_pointer<T>::value ==
false)
569 return write_internal(os, (
const char*)&t_,
sizeof(t_));
575 os.setstate(std::ios::failbit);
581 if (std::is_class<T>::value ==
false)
584 if (std::is_pointer<T>::value ==
false)
590 return write_internal(os, (
const char*)&t_,
sizeof(t_));
596 os.setstate(std::ios::failbit);
614 std::ostream&
write(std::ostream& os, std::vector<T>& container)
618 uint16_t size =
static_cast<uint16_t
>(container.size());
620 write(os, size,
false);
621 if (check_stream(os) && check_container_size(os, size))
623 for (
const auto& item : container)
625 write(os, item,
false);
637 std::istream&
read(std::istream& is, std::vector<T>& container)
641 if (check_stop_parse(is))
648 read(is, size,
false);
649 if (check_stream(is) && check_container_size(is, size))
651 parseStatus(
typeid(container), size);
652 for (uint16_t i = 0; i < size; ++i)
656 container.push_back(t);
669 std::ostream&
write(std::ostream& os, std::vector<T*>& container)
671 static_assert(std::is_base_of<serialize::I, T>::value,
"Type T must be derived from serialize::I");
673 uint16_t size =
static_cast<uint16_t
>(container.size());
675 write(os, size,
false);
677 if (check_stream(os) && check_container_size(os, size))
679 for (
auto* ptr : container)
684 write(os, notNULL,
false);
686 auto* i =
static_cast<I*
>(ptr);
691 bool notNULL =
false;
692 write(os, notNULL,
false);
706 std::istream&
read(std::istream& is, std::vector<T*>& container)
708 static_assert(std::is_base_of<serialize::I, T>::value,
"Type T must be derived from serialize::I");
710 if (check_stop_parse(is))
717 read(is, size,
false);
718 if (check_stream(is) && check_container_size(is, size))
720 parseStatus(
typeid(container), size);
721 for (uint16_t i = 0; i < size; ++i)
723 bool notNULL =
false;
724 read(is, notNULL,
false);
729 auto *i =
static_cast<I*
>(object);
731 container.push_back(
object);
735 container.push_back(
nullptr);
748 template <
class K,
class V,
class P>
749 std::ostream&
write(std::ostream& os, std::map<K, V, P>& container)
753 uint16_t size =
static_cast<uint16_t
>(container.size());
755 write(os, size,
false);
756 if (check_stream(os) && check_container_size(os, size))
758 for (
const auto& entry : container)
760 write(os, entry.first,
false);
761 write(os, entry.second,
false);
772 template <
class K,
class V,
class P>
773 std::istream&
read(std::istream& is, std::map<K, V, P>& container)
777 if (check_stop_parse(is))
784 read(is, size,
false);
785 if (check_stream(is) && check_container_size(is, size))
787 parseStatus(
typeid(container), size);
788 for (uint16_t i = 0; i < size; ++i)
792 read(is, key,
false);
794 read(is, value,
false);
795 container[key] = value;
807 template <
class K,
class V,
class P>
808 std::ostream&
write(std::ostream& os, std::map<K, V*, P>& container)
810 static_assert(std::is_base_of<serialize::I, V>::value,
"Type V must be derived from serialize::I");
812 uint16_t size =
static_cast<uint16_t
>(container.size());
814 write(os, size,
false);
816 if (check_stream(os) && check_container_size(os, size))
818 for (
auto& entry : container)
820 write(os, entry.first,
false);
822 if (entry.second !=
nullptr)
825 write(os, notNULL,
false);
827 auto* i =
static_cast<I*
>(entry.second);
832 bool notNULL =
false;
833 write(os, notNULL,
false);
845 template <
class K,
class V,
class P>
846 std::istream&
read(std::istream& is, std::map<K, V*, P>& container)
848 static_assert(std::is_base_of<serialize::I, V>::value,
"Type V must be derived from serialize::I");
850 if (check_stop_parse(is))
857 read(is, size,
false);
858 if (check_stream(is) && check_container_size(is, size))
860 parseStatus(
typeid(container), size);
861 for (uint16_t i = 0; i < size; ++i)
864 read(is, key,
false);
866 read(is, notNULL,
false);
870 auto *i =
static_cast<I*
>(object);
872 container[key] = (V*)
object;
876 container[key] =
nullptr;
889 template <
class T,
class P>
890 std::ostream&
write(std::ostream& os, std::set<T, P>& container)
894 uint16_t size =
static_cast<uint16_t
>(container.size());
896 write(os, size,
false);
898 if (check_stream(os) && check_container_size(os, size))
900 for (
const auto& item : container)
902 write(os, item,
false);
913 template <
class T,
class P>
914 std::istream&
read(std::istream& is, std::set<T, P>& container)
918 if (check_stop_parse(is))
925 read(is, size,
false);
926 if (check_stream(is) && check_container_size(is, size))
928 parseStatus(
typeid(container), size);
929 for (uint16_t i = 0; i < size; ++i)
945 template <
class T,
class P>
946 std::ostream&
write(std::ostream& os, std::set<T*, P>& container)
948 static_assert(std::is_base_of<serialize::I, T>::value,
"Type T must be derived from serialize::I");
950 uint16_t size =
static_cast<uint16_t
>(container.size());
952 write(os, size,
false);
953 if (check_stream(os) && check_container_size(os, size))
955 for (
auto ptr : container)
960 write(os, notNULL,
false);
962 auto* i =
static_cast<I*
>(ptr);
967 bool notNULL =
false;
968 write(os, notNULL,
false);
980 template <
class T,
class P>
981 std::istream&
read(std::istream& is, std::set<T*, P>& container)
983 static_assert(std::is_base_of<serialize::I, T>::value,
"Type T must be derived from serialize::I");
985 if (check_stop_parse(is))
992 read(is, size,
false);
993 if (check_stream(is) && check_container_size(is, size))
995 parseStatus(
typeid(container), size);
996 for (uint16_t i = 0; i < size; ++i)
998 bool notNULL =
false;
999 read(is, notNULL,
false);
1003 auto *i =
static_cast<I*
>(object);
1005 container.insert(
object);
1009 container.insert(
nullptr);
1023 std::ostream&
write(std::ostream& os, std::list<T>& container)
1027 uint16_t size =
static_cast<uint16_t
>(container.size());
1029 write(os, size,
false);
1031 if (check_stream(os) && check_container_size(os, size))
1033 for (
const auto& item : container)
1035 write(os, item,
false);
1047 std::istream&
read(std::istream& is, std::list<T>& container)
1051 if (check_stop_parse(is))
1058 read(is, size,
false);
1059 if (check_stream(is) && check_container_size(is, size))
1061 parseStatus(
typeid(container), size);
1062 for (uint16_t i = 0; i < size; ++i)
1066 container.push_back(t);
1079 std::ostream&
write(std::ostream& os, std::list<T*>& container)
1081 static_assert(std::is_base_of<serialize::I, T>::value,
"Type T must be derived from serialize::I");
1083 uint16_t size =
static_cast<uint16_t
>(container.size());
1085 write(os, size,
false);
1087 if (check_stream(os) && check_container_size(os, size))
1089 for (
auto* ptr : container)
1093 bool notNULL =
true;
1094 write(os, notNULL,
false);
1096 auto* i =
static_cast<I*
>(ptr);
1101 bool notNULL =
false;
1102 write(os, notNULL,
false);
1115 std::istream&
read(std::istream& is, std::list<T*>& container)
1117 static_assert(std::is_base_of<serialize::I, T>::value,
"Type T must be derived from serialize::I");
1119 if (check_stop_parse(is))
1126 read(is, size,
false);
1127 if (check_stream(is) && check_container_size(is, size))
1129 parseStatus(
typeid(container), size);
1130 for (uint16_t i = 0; i < size; ++i)
1132 bool notNULL =
false;
1133 read(is, notNULL,
false);
1137 auto *i =
static_cast<I*
>(object);
1139 container.push_back(
object);
1143 container.push_back(
nullptr);
1154 error_handler = error_handler_;
1163 parse_handler = parse_handler_;
1174 std::istream& read_internal(std::istream& is,
char* p, uint32_t size,
bool no_swap =
false)
1176 if (check_stop_parse(is))
1181 if (!check_pointer(is, p))
1185 if (
LE() && !no_swap)
1188 for (
int i = size - 1; i >= 0; --i)
1209 std::ostream& write_internal(std::ostream& os,
const char* p, uint32_t size,
bool no_swap =
false)
1211 if (!check_pointer(os, p))
1215 if (
LE() && !no_swap)
1218 for (
int i = size - 1; i >= 0; --i)
1232 static const uint16_t MAX_STRING_SIZE = 256;
1233 static const uint16_t MAX_CONTAINER_SIZE = 200;
1236 static const size_t WCHAR_SIZE = 2;
1239 std::list<std::streampos> stopParsePosStack;
1243 void raiseError(
ParsingError error,
int line,
const char* file)
1247 error_handler(error, line, file);
1251 void parseStatus(
const std::type_info& typeId,
size_t size = 0)
1254 parse_handler(typeId, size);
1257 void write_type(std::ostream& os,
Type type_)
1259 uint8_t type =
static_cast<uint8_t
>(type_);
1260 write_internal(os, (
const char*) &type,
sizeof(type));
1263 bool read_type(std::istream& is,
Type type_)
1265 Type type =
static_cast<Type>(is.peek());
1268 uint8_t typeByte = 0;
1269 read_internal(is, (
char*) &typeByte,
sizeof(typeByte));
1275 is.setstate(std::ios::failbit);
1280 bool check_stream(std::ios& stream)
1285 stream.setstate(std::ios::failbit);
1287 return stream.good();
1290 bool check_slength(std::ios& stream,
int stringSize)
1292 bool sizeOk = stringSize <= MAX_STRING_SIZE;
1296 stream.setstate(std::ios::failbit);
1298 if (stringSize == 0)
1303 bool check_container_size(std::ios& stream,
int containerSize)
1305 bool sizeOk = containerSize <= MAX_CONTAINER_SIZE;
1309 stream.setstate(std::ios::failbit);
1314 bool check_pointer(std::ios& stream,
const void* ptr)
1319 stream.setstate(std::ios::failbit);
1324 void push_stop_parse_pos(std::streampos stopParsePos)
1326 stopParsePosStack.push_front(stopParsePos);
1329 std::streampos pop_stop_parse_pos()
1331 std::streampos stopParsePos = stopParsePosStack.front();
1332 stopParsePosStack.pop_front();
1333 return stopParsePos;
1336 bool check_stop_parse(std::istream& is)
1343 if (stopParsePosStack.size() > 0)
1345 std::streampos stopParsePos = stopParsePosStack.front();
1346 if (is.tellg() >= stopParsePos)
Abstract interface that all serialized user defined classes inherit.
Definition msg_serialize.h:109
virtual std::ostream & write(serialize &ms, std::ostream &os)=0
virtual std::istream & read(serialize &ms, std::istream &is)=0
The serialize class binary serializes and deserializes C++ objects.
Definition msg_serialize.h:105
std::ostream & write(std::ostream &os, char *str)
Definition msg_serialize.h:435
void writeEndian(std::ostream &os)
Definition msg_serialize.h:181
std::ostream & write(std::ostream &os, std::set< T, P > &container)
Definition msg_serialize.h:890
bool LE()
Definition msg_serialize.h:160
std::istream & read(std::istream &is, std::vector< bool > &container)
Definition msg_serialize.h:320
std::istream & read(std::istream &is, std::wstring &s)
Definition msg_serialize.h:262
std::istream & read(std::istream &is, std::map< K, V *, P > &container)
Definition msg_serialize.h:846
std::istream & read(std::istream &is, char *str)
Definition msg_serialize.h:291
std::ostream & write(std::ostream &os, const std::wstring &s)
Definition msg_serialize.h:405
std::ostream & write(std::ostream &os, std::map< K, V *, P > &container)
Definition msg_serialize.h:808
void setParseHandler(ParseHandler parse_handler_)
Definition msg_serialize.h:1161
std::ostream & write(std::ostream &os, const std::string &s)
Definition msg_serialize.h:380
void clearLastError()
Definition msg_serialize.h:1158
std::ostream & write(std::ostream &os, std::wstring &s)
Definition msg_serialize.h:426
std::ostream & write(std::ostream &os, std::vector< T * > &container)
Definition msg_serialize.h:669
std::istream & read(std::istream &is, std::set< T, P > &container)
Definition msg_serialize.h:914
std::ostream & write(std::ostream &os, T &t_, bool prependType=true)
Definition msg_serialize.h:540
std::istream & read(std::istream &is, std::list< T * > &container)
Definition msg_serialize.h:1115
std::ostream & write(std::ostream &os, std::vector< T > &container)
Definition msg_serialize.h:614
std::ostream & write(std::ostream &os, std::list< T > &container)
Definition msg_serialize.h:1023
std::ostream & write(std::ostream &os, std::set< T *, P > &container)
Definition msg_serialize.h:946
Type
Definition msg_serialize.h:131
std::ostream & write(std::ostream &os, std::map< K, V, P > &container)
Definition msg_serialize.h:749
void setErrorHandler(ErrorHandler error_handler_)
Definition msg_serialize.h:1152
std::ostream & write(std::ostream &os, std::vector< bool > &container)
Definition msg_serialize.h:467
std::istream & read(std::istream &is, T &t_, bool readPrependedType=true)
Definition msg_serialize.h:487
std::ostream & write(std::ostream &os, std::string &s)
Definition msg_serialize.h:396
std::istream & read(std::istream &is, std::set< T *, P > &container)
Definition msg_serialize.h:981
std::istream & read(std::istream &is, std::vector< T > &container)
Definition msg_serialize.h:637
std::ostream & write(std::ostream &os, std::list< T * > &container)
Definition msg_serialize.h:1079
void(*) ParseHandler(const std::type_info &typeId, size_t size)
Definition msg_serialize.h:1160
ParsingError getLastError() const
Definition msg_serialize.h:1157
std::istream & read(std::istream &is, std::string &s)
Definition msg_serialize.h:239
std::istream & read(std::istream &is, I *t_)
Definition msg_serialize.h:195
std::istream & readEndian(std::istream &is, bool &littleEndian)
Definition msg_serialize.h:170
std::ostream & write(std::ostream &os, I *t_)
Definition msg_serialize.h:349
void(*) ErrorHandler(ParsingError error, int line, const char *file)
Definition msg_serialize.h:1151
ParsingError
Definition msg_serialize.h:145
std::istream & read(std::istream &is, std::vector< T * > &container)
Definition msg_serialize.h:706
std::istream & read(std::istream &is, std::map< K, V, P > &container)
Definition msg_serialize.h:773
std::istream & read(std::istream &is, std::list< T > &container)
Definition msg_serialize.h:1047
std::ostream & write(std::ostream &os, const char *str)
Definition msg_serialize.h:444
Definition msg_serialize.h:21
Definition msg_serialize.h:23
Definition msg_serialize.h:29