1#ifndef __SERIALIZABLE_OBJECT_IMP_LIB_H__
2#define __SERIALIZABLE_OBJECT_IMP_LIB_H__
8#if __cplusplus <= 201402L
9template<
bool B,
class T =
void >
10using enable_if_t =
typename enable_if<B, T>::type;
14namespace MedSerialize {
17 static void get_list_names(
char *list_of_args, vector<string> &names) {
19 string s(list_of_args);
20 boost::replace_all(s,
" ",
"");
21 boost::replace_all(s,
"\t",
"");
22 boost::replace_all(s,
"\n",
"");
23 boost::split(names, s, boost::is_any_of(
","));
33 template <
class T>
size_t get_size(T *&v);
34 template <
class T>
size_t serialize(
unsigned char *blob, T *&v);
35 template <
class T>
size_t deserialize(
unsigned char *blob, T *&v);
36 template <
class T>
string object_json(T *&v);
37 template <
class T>
string object_json(
const T *&v);
38 template <
class T>
string object_json(T *
const &v);
39 template <
class T>
size_t get_size(vector<T> &v);
40 template <
class T>
size_t serialize(
unsigned char *blob, vector<T> &v);
41 template <
class T>
size_t deserialize(
unsigned char *blob, vector<T> &v);
42 template <
class T>
string object_json(
const vector<const T> &v);
43 template <
class T>
string object_json(
const vector<T> &v);
44 template <
class T>
string object_json(vector<const T> &v);
45 template <
class T,
class S>
size_t get_size(pair<T, S> &v);
46 template <
class T,
class S>
size_t serialize(
unsigned char *blob, pair<T, S> &v);
47 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, pair<T, S> &v);
48 template <
class T,
class S>
string object_json(pair<const T, const S> &v);
49 template <
class T,
class S>
string object_json(
const pair<T, S> &v);
50 template <
class T,
class S>
string object_json(
const pair<const T, const S> &v);
51 template <
class T,
class S>
size_t get_size(map<T, S> &v);
52 template <
class T,
class S>
size_t serialize(
unsigned char *blob, map<T, S> &v);
53 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, map<T, S> &v);
54 template <
class T,
class S>
string object_json(map<const T, const S> &v);
55 template <
class T,
class S>
string object_json(
const map<const T, const S> &v);
56 template <
class T,
class S>
string object_json(
const map<T, S> &v);
57 template <
class T,
class S>
size_t get_size(unordered_map<T, S> &v);
58 template <
class T,
class S>
size_t serialize(
unsigned char *blob, unordered_map<T, S> &v);
59 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, unordered_map<T, S> &v);
60 template <
class T,
class S>
string object_json(unordered_map<const T, const S> &v);
61 template <
class T,
class S>
string object_json(
const unordered_map<const T, const S> &v);
62 template <
class T,
class S>
string object_json(
const unordered_map<T, S> &v);
63 template <
class T>
size_t get_size(unordered_set<T> &v);
64 template <
class T>
size_t serialize(
unsigned char *blob, unordered_set<T> &v);
65 template <
class T>
size_t deserialize(
unsigned char *blob, unordered_set<T> &v);
66 template <
class T>
string object_json(unordered_set<const T> &v);
67 template <
class T>
string object_json(
const unordered_set<const T> &v);
68 template <
class T>
string object_json(
const unordered_set<T> &v);
69 template <
class T>
size_t get_size(set<T> &v);
70 template <
class T>
size_t serialize(
unsigned char *blob, set<T> &v);
71 template <
class T>
size_t deserialize(
unsigned char *blob, set<T> &v);
72 template <
class T>
string object_json(set<const T> &v);
73 template <
class T>
string object_json(
const set<const T> &v);
74 template <
class T>
string object_json(
const set<T> &v);
75 template <
class T>
size_t get_size(unique_ptr<T> &v);
76 template <
class T>
size_t serialize(
unsigned char *blob, unique_ptr<T> &v);
77 template <
class T>
size_t deserialize(
unsigned char *blob, unique_ptr<T> &v);
78 template <
class T>
string object_json(unique_ptr<T> &v);
79 template <
class T>
string object_json(unique_ptr<const T> &v);
88 template <
class T>
size_t get_size(T &elem)
95 template <
class T>
size_t serialize(
unsigned char *blob, T &elem)
98 memcpy(blob, &elem,
sizeof(T));
105 template <
class T>
size_t deserialize(
unsigned char *blob, T &elem)
108 memcpy(&elem, blob,
sizeof(T));
115 string object_json_spec(T& v, std::true_type , std::false_type) {
118 string enum_nm =
typeid(v).name();
120 enum_nm = boost::regex_replace(enum_nm, boost::regex(
"^([0-9]*|enum )"),
"");
121 str << enum_nm <<
"::" << (int)v;
125 string object_json_spec(T& v, std::false_type, std::true_type ) {
127 str << v.object_json();
131 string object_json_spec(T& v, std::false_type, std::false_type) {
134 str <<
"UNSUPPORTED::" <<
typeid(v).name();
139 string object_json(T& v) {
140 return object_json_spec(v, std::is_enum<T>{}, std::is_base_of<SerializableObject, T>{});
147 template <
class T>
size_t get_size(T *&elem)
155 size += MedSerialize::get_size(s);
158 string s = elem->my_class_name();
160 size += MedSerialize::get_size(s);
161 size += MedSerialize::get_size((*elem));
168 template <
class T>
size_t serialize(
unsigned char *blob, T *&elem)
177 s = elem->my_class_name();
178 pos += MedSerialize::serialize<string>(blob + pos, s);
183 pos += MedSerialize::serialize(blob + pos, (*elem));
190 template <
class T>
size_t deserialize(
unsigned char *blob, T *&elem)
198 pos += MedSerialize::deserialize<string>(blob + pos, cname);
202 if (cname ==
"NULL") {
208 elem = (T *)dummy.new_polymorphic(cname);
217 pos += MedSerialize::deserialize(blob + pos, (*elem));
224 template <
class T>
string object_json(T *&v) {
228 str << MedSerialize::object_json((
const T&)*v);
234 template <
class T>
string object_json(
const T *&v) {
238 str << MedSerialize::object_json((
const T&)*v);
244 template <
class T>
string object_json(T *
const &v) {
248 str << MedSerialize::object_json((
const T&)*v);
257 template <
class T>
size_t get_size(unique_ptr<T> &elem)
262 if (elem.get() == NULL) {
265 size += MedSerialize::get_size(s);
268 string s = elem->my_class_name();
270 size += MedSerialize::get_size(s);
271 size += MedSerialize::get_size((*elem));
278 template <
class T>
size_t serialize(
unsigned char *blob, unique_ptr<T> &elem)
286 if (elem.get() != NULL)
287 s = elem->my_class_name();
288 pos += MedSerialize::serialize<string>(blob + pos, s);
292 if (elem.get() != NULL)
293 pos += MedSerialize::serialize(blob + pos, (*elem));
300 template <
class T>
size_t deserialize(
unsigned char *blob, unique_ptr<T> &elem)
308 pos += MedSerialize::deserialize<string>(blob + pos, cname);
312 if (cname ==
"NULL") {
318 elem = unique_ptr<T>((T *)dummy.new_polymorphic(cname));
320 if (elem.get() == NULL) {
321 elem = unique_ptr<T>(
new T);
327 pos += MedSerialize::deserialize(blob + pos, (*elem.get()));
334 template <
class T>
string object_json(unique_ptr<T> &v) {
337 if (v.get() != NULL) {
338 str << MedSerialize::object_json((
const T&)*v);
344 template <
class T>
string object_json(unique_ptr<const T> &v) {
347 if (v.get() != NULL) {
348 str << MedSerialize::object_json((
const T&)*v);
359 template<>
inline size_t get_size<string>(
string &str) {
361 size +=
sizeof(size_t);
362 size += str.length() + 1;
367 template<>
inline size_t serialize<string>(
unsigned char *blob,
string &str)
370 size_t len = str.length();
372 memcpy(blob, &len,
sizeof(
size_t)); pos +=
sizeof(size_t);
373 memcpy(blob + pos, str.c_str(), len); pos += len;
374 blob[pos] = 0; pos++;
380 template<>
inline size_t deserialize<string>(
unsigned char *blob,
string &str)
385 memcpy(&len, blob,
sizeof(
size_t)); pos +=
sizeof(size_t);
387 string new_s((
char *)&blob[pos]);
397 template<
class T>
size_t get_size(vector<T> &v)
400 size_t size = 0, len = v.size();
401 size += MedSerialize::get_size<size_t>(len);
403 size += MedSerialize::get_size(elem);
409 template <
class T>
size_t serialize(
unsigned char *blob, vector<T> &v)
413 size_t pos = 0, len = v.size();
414 pos += MedSerialize::serialize<size_t>(blob + pos, len);
417 pos += MedSerialize::serialize(blob + pos, elem);
422 template <
class T>
size_t deserialize(
unsigned char *blob, vector<T> &v)
427 pos += MedSerialize::deserialize<size_t>(blob + pos, len);
428 if (len != v.size()) v.clear();
432 pos += MedSerialize::deserialize(blob + pos, elem);
437 template <
class T>
string object_json(
const vector<const T> &v) {
441 for (
size_t i = 0; i < v.size(); ++i)
445 str << MedSerialize::object_json(v[i]);
450 template <
class T>
string object_json(
const vector<T> &v) {
454 for (
size_t i = 0; i < v.size(); ++i)
458 str << MedSerialize::object_json(v[i]);
463 template <
class T>
string object_json(vector<const T> &v) {
467 for (
size_t i = 0; i < v.size(); ++i)
471 str << MedSerialize::object_json(v[i]);
481 template <
class T>
size_t get_size(set<T> &v)
483 size_t size = 0, len = v.size();
484 size += MedSerialize::get_size<size_t>(len);
486 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
487 size += MedSerialize::get_size(*it);
493 template <
class T>
size_t serialize(
unsigned char *blob, set<T> &v)
496 size_t pos = 0, len = v.size();
497 pos += MedSerialize::serialize<size_t>(blob + pos, len);
500 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
501 pos += MedSerialize::serialize(blob + pos, (T &)(*it));
508 template <
class T>
size_t deserialize(
unsigned char *blob, set<T> &v)
511 pos += MedSerialize::deserialize<size_t>(blob + pos, len);
515 for (
int i = 0; i < len; i++) {
516 pos += MedSerialize::deserialize(blob + pos, elem);
523 template <
class T>
string object_json(set<const T> &v) {
528 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
532 str << MedSerialize::object_json((
const T &)(*it));
538 template <
class T>
string object_json(
const set<const T> &v) {
543 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
547 str << MedSerialize::object_json((
const T &)(*it));
553 template <
class T>
string object_json(
const set<T> &v) {
558 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
562 str << MedSerialize::object_json((T &)(*it));
573 template <
class T>
size_t get_size(unordered_set<T> &v)
575 size_t size = 0, len = v.size();
576 size += MedSerialize::get_size<size_t>(len);
578 size += MedSerialize::get_size(elem);
584 template <
class T>
size_t serialize(
unsigned char *blob, unordered_set<T> &v)
587 size_t pos = 0, len = v.size();
588 pos += MedSerialize::serialize<size_t>(blob + pos, len);
591 pos += MedSerialize::serialize(blob + pos, elem);
597 template <
class T>
size_t deserialize(
unsigned char *blob, unordered_set<T> &v)
600 pos += MedSerialize::deserialize<size_t>(blob + pos, len);
604 for (
int i = 0; i < len; i++) {
605 pos += MedSerialize::deserialize(blob + pos, elem);
612 template <
class T>
string object_json(unordered_set<const T> &v) {
621 str << MedSerialize::object_json(e);
627 template <
class T>
string object_json(
const unordered_set<const T> &v) {
636 str << MedSerialize::object_json(e);
642 template <
class T>
string object_json(
const unordered_set<T> &v) {
651 str << MedSerialize::object_json(e);
663 template <
class T,
class S>
size_t get_size(pair<T, S> &v)
666 T *t = (T *)&v.first;
667 S *s = (S *)&v.second;
668 size += MedSerialize::get_size((*t));
669 size += MedSerialize::get_size((*s));
675 template <
class T,
class S>
size_t serialize(
unsigned char *blob, pair<T, S> &v)
679 T *t = (T *)&v.first;
680 S *s = (S *)&v.second;
681 pos += MedSerialize::serialize(blob + pos, (*t));
682 pos += MedSerialize::serialize(blob + pos, (*s));
688 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, pair<T, S> &v)
694 pos += MedSerialize::deserialize(blob + pos, t);
695 pos += MedSerialize::deserialize(blob + pos, s);
702 template <
class T,
class S>
string object_json(
const pair<const T, const S> &v) {
705 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
708 str << MedSerialize::object_json(v.first);
709 str <<
"," << MedSerialize::object_json(v.second);
714 template <
class T,
class S>
string object_json(pair<const T, const S> &v) {
717 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
720 str << MedSerialize::object_json(v.first);
721 str <<
"," << MedSerialize::object_json(v.second);
726 template <
class T,
class S>
string object_json(
const pair<T, S> &v) {
729 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
732 str << MedSerialize::object_json(v.first);
733 str <<
"," << MedSerialize::object_json(v.second);
742 template <
class T,
class S>
size_t get_size(map<T, S> &v)
744 size_t size = 0, len = v.size();
745 size += MedSerialize::get_size<size_t>(len);
746 for (
auto &elem : v) {
747 T *t = (T *)&elem.first;
748 S *s = (S *)&elem.second;
749 size += MedSerialize::get_size((*t));
750 size += MedSerialize::get_size((*s));
756 template <
class T,
class S>
size_t serialize(
unsigned char *blob, map<T, S> &v)
759 size_t pos = 0, len = v.size();
760 pos += MedSerialize::serialize<size_t>(blob + pos, len);
762 for (
auto &elem : v) {
763 T *t = (T *)&elem.first;
764 S *s = (S *)&elem.second;
765 pos += MedSerialize::serialize(blob + pos, (*t));
766 pos += MedSerialize::serialize(blob + pos, (*s));
772 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, map<T, S> &v)
776 pos += MedSerialize::deserialize(blob + pos, len);
781 for (
int i = 0; i < len; i++) {
782 pos += MedSerialize::deserialize(blob + pos, t);
783 pos += MedSerialize::deserialize(blob + pos, s);
790 template <
class T,
class S>
string object_json(map<const T, const S> &v) {
795 for (
auto it = v.begin(); it != v.end(); ++it)
801 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
804 str << MedSerialize::object_json((
const T&)it->first);
805 str <<
"," << MedSerialize::object_json((
const S &)it->second);
814 template <
class T,
class S>
string object_json(
const map<const T, const S> &v) {
819 for (
auto it = v.begin(); it != v.end(); ++it)
825 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
828 str << MedSerialize::object_json((
const T&)it->first);
829 str <<
"," << MedSerialize::object_json((
const S &)it->second);
838 template <
class T,
class S>
string object_json(
const map<T, S> &v) {
843 for (
auto it = v.begin(); it != v.end(); ++it)
849 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
852 str << MedSerialize::object_json((T&)it->first);
853 str <<
"," << MedSerialize::object_json((S &)it->second);
867 template <
class T,
class S>
size_t get_size(unordered_map<T, S> &v)
869 size_t size = 0, len = v.size();
870 size += MedSerialize::get_size<size_t>(len);
872 for (
typename unordered_map<T, S>::iterator it = v.begin(); it != v.end(); ++it) {
873 size += MedSerialize::get_size((T &)(it->first));
874 size += MedSerialize::get_size((S &)(it->second));
881 template <
class T,
class S>
size_t serialize(
unsigned char *blob, unordered_map<T, S> &v)
883 size_t pos = 0, len = v.size();
884 pos += MedSerialize::serialize<size_t>(blob + pos, len);
887 for (
typename unordered_map<T, S>::iterator it = v.begin(); it != v.end(); ++it) {
888 pos += MedSerialize::serialize(blob + pos, (T &)(it->first));
889 pos += MedSerialize::serialize(blob + pos, (S &)(it->second));
896 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, unordered_map<T, S> &v)
899 pos += MedSerialize::deserialize(blob + pos, len);
904 for (
int i = 0; i < len; i++) {
905 pos += MedSerialize::deserialize(blob + pos, t);
906 pos += MedSerialize::deserialize(blob + pos, s);
913 template <
class T,
class S>
string object_json(unordered_map<const T, const S> &v) {
918 for (
auto it = v.begin(); it != v.end(); ++it)
924 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
927 str << MedSerialize::object_json((
const T &)it->first);
928 str <<
"," << MedSerialize::object_json((
const S &)it->second);
937 template <
class T,
class S>
string object_json(
const unordered_map<const T, const S> &v) {
942 for (
auto it = v.begin(); it != v.end(); ++it)
948 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
951 str << MedSerialize::object_json((
const T &)it->first);
952 str <<
"," << MedSerialize::object_json((
const S &)it->second);
961 template <
class T,
class S>
string object_json(
const unordered_map<T, S> &v) {
966 for (
auto it = v.begin(); it != v.end(); ++it)
972 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
975 str << MedSerialize::object_json((T &)it->first);
976 str <<
"," << MedSerialize::object_json((S &)it->second);
990 template <
class T,
class... Ts>
size_t get_size(T& elem, Ts&... args)
994 size += MedSerialize::get_size(elem);
995 size += MedSerialize::get_size(args...);
1002 template <
class T,
class... Ts>
size_t serialize(
unsigned char *blob, T& elem, Ts&... args)
1006 pos += MedSerialize::serialize(blob, elem);
1007 pos += MedSerialize::serialize(blob + pos, args...);
1014 template <
typename T,
typename... Ts>
size_t deserialize(
unsigned char *blob, T& elem, Ts&... args)
1018 pos += MedSerialize::deserialize(blob, elem);
1019 pos += MedSerialize::deserialize(blob + pos, args...);
1025 template <
class T,
class... Ts>
string object_json(T& elem, Ts&... args) {
1028 str << MedSerialize::object_json(elem);
1029 str <<
"," << MedSerialize::object_json(args...);
1039 template<
class T>
size_t serializer(
unsigned char *blob,
int counter, vector<string> &names, T &elem)
1045 pos += MedSerialize::serialize(blob + pos, pos);
1049 pos += MedSerialize::serialize(blob + pos, names[counter]);
1053 pos += MedSerialize::serialize(blob + pos, elem);
1058 MedSerialize::serialize(blob, pos);
1066 template<
class T,
class... Ts>
size_t serializer(
unsigned char *blob,
int counter, vector<string> &names, T &elem, Ts&...args)
1072 pos += MedSerialize::serializer(blob, counter, names, elem);
1077 pos += MedSerialize::serializer(blob + pos, counter + 1, names, args...);
1085 template<typename T, enable_if_t<std::is_base_of<SerializableObject, T>::value,
int> = 0>
string get_name() {
1086 unique_ptr<T> cl = unique_ptr<T>(
new T);
1087 string cl_name = cl->my_class_name();
1092 template<typename T, enable_if_t<!std::is_base_of<SerializableObject, T>::value,
int> = 0>
string get_name() {
1093 string cl_name =
"primitive_type";
1099 template<
class T>
size_t deserializer(
unsigned char *blob,
int counter, vector<string> &names, map <string, size_t> &name2pos, T &elem)
1101 string name = names[counter];
1106 if (name2pos.find(name) != name2pos.end()) {
1108 pos += MedSerialize::deserialize(blob + name2pos[name], elem);
1113 string cl_name = get_name<T>();
1114 cerr <<
"WARNING: In \"" << cl_name <<
"\" element " << name <<
" not serialized... will be deserialized to its default\n";
1122 template<
class T,
class... Ts>
size_t deserializer(
unsigned char *blob,
int counter, vector<string> &names, map<string, size_t> &name2pos, T &elem, Ts&...args)
1124 string name = names[counter];
1130 pos += MedSerialize::deserializer(blob, counter, names, name2pos, elem);
1133 pos += MedSerialize::deserializer(blob, counter + 1, names, name2pos, args...);
1141 template<
class T>
size_t get_sizer(
int counter, vector<string> &names, T &elem)
1143 string name = names[counter];
1148 size += MedSerialize::get_size(size);
1149 size += MedSerialize::get_size(name);
1150 size += MedSerialize::get_size(elem);
1157 template<
class T,
class... Ts>
size_t get_sizer(
int counter, vector<string> &names, T &elem, Ts&...args)
1159 string name = names[counter];
1165 size += MedSerialize::get_sizer(counter, names, elem);
1168 size += MedSerialize::get_sizer(counter + 1, names, args...);
1175 template<
class... Ts>
size_t get_size_top(
char *list_of_args, Ts&...args) {
1181 vector<string> names;
1182 MedSerialize::get_list_names(list_of_args, names);
1184 size += MedSerialize::get_size(size);
1185 size += MedSerialize::get_sizer(0, names, args...);
1193 template<
class... Ts>
size_t serialize_top(
unsigned char *blob,
char *list_of_args, Ts&...args) {
1198 vector<string> names;
1199 MedSerialize::get_list_names(list_of_args, names);
1203 pos += MedSerialize::serialize(blob + pos, pos);
1206 pos += MedSerialize::serializer(blob + pos, 0, names, args...);
1210 MedSerialize::serialize(blob, pos);
1217 template<
class... Ts>
size_t deserialize_top(
unsigned char *blob,
char *list_of_args, Ts&...args) {
1222 vector<string> names;
1223 MedSerialize::get_list_names(list_of_args, names);
1226 size_t tot_size = 0, pos = 0;
1227 pos += MedSerialize::deserialize(blob, tot_size);
1231 map<string, size_t> name2pos;
1232 while (pos < tot_size) {
1236 size_t orig_curr_pos = pos;
1238 pos += MedSerialize::deserialize(blob + pos, curr_size);
1243 pos += MedSerialize::deserialize(blob + pos, name);
1247 name2pos[name] = pos;
1250 pos = orig_curr_pos + curr_size;
1256 for (
auto &e : name2pos)
1257 SRL_LOG(
"dtop(5.5) name2pos [ %s ] = %d\n", e.first.
c_str(), e.second);
1260 MedSerialize::deserializer(blob, 0, names, name2pos, args...);
1269 template<
class T>
string object_json_rec(
int counter, vector<string> &names, T &elem) {
1273 str <<
"\n\t\"" << names[counter] <<
"\": ";
1274 str << MedSerialize::object_json(elem);
1279 template<
class T,
class... Ts>
string object_json_rec(
int counter, vector<string> &names, T &elem, Ts&...args) {
1283 str << MedSerialize::object_json_rec(counter, names, elem);
1285 if (counter + 1 < names.size())
1288 str << MedSerialize::object_json_rec(counter + 1, names, args...);
1293 template<
class... Ts>
string object_json_start(
const string &cls_name,
int vers,
char *list_of_args, Ts&...args) {
1294 vector<string> names;
1295 MedSerialize::get_list_names(list_of_args, names);
1299 str <<
"{\n\t\"Object\":\"" << cls_name <<
"\",\n\t\"Version\":" << vers <<
",";
1309 string res = MedSerialize::object_json_rec(0, names, args...);
1310 str << res <<
"\n}";
1316 template<>
inline string object_json<const int>(
const int &v) {
return to_string(v); }
1317 template<>
inline string object_json<const unsigned int>(
const unsigned int &v) {
return to_string(v); }
1318 template<>
inline string object_json<const float>(
const float &v) {
return to_string(v); }
1319 template<>
inline string object_json<const double>(
const double &v) {
return to_string(v); }
1320 template<>
inline string object_json<const char>(
const char &v) {
return to_string(v); }
1321 template<>
inline string object_json<const unsigned char>(
const unsigned char &v) {
return to_string(v); }
1322 template<>
inline string object_json<const long>(
const long &v) {
return to_string(v); }
1323 template<>
inline string object_json<const long long>(
const long long &v) {
return to_string(v); }
1324 template<>
inline string object_json<const unsigned long long>(
const unsigned long long &v) {
return to_string(v); }
1325 template<>
inline string object_json<const bool>(
const bool &v) {
return to_string(v); }
1326 template<>
inline string object_json<int>(
int &v) {
return to_string(v); }
1327 template<>
inline string object_json<unsigned int>(
unsigned int &v) {
return to_string(v); }
1328 template<>
inline string object_json<float>(
float &v) {
return to_string(v); }
1329 template<>
inline string object_json<double>(
double &v) {
return to_string(v); }
1330 template<>
inline string object_json<char>(
char &v) {
return to_string(v); }
1331 template<>
inline string object_json<unsigned char>(
unsigned char &v) {
return to_string(v); }
1332 template<>
inline string object_json<long>(
long &v) {
return to_string(v); }
1333 template<>
inline string object_json<long long>(
long long &v) {
return to_string(v); }
1334 template<>
inline string object_json<unsigned long long>(
unsigned long long &v) {
return to_string(v); }
1335 template<>
inline string object_json<bool>(
bool &v) {
return to_string(v); }
1336 template<>
inline string object_json<string>(
string &str) {
return "\"" + str +
"\""; }
1337 template<>
inline string object_json<const string>(
const string &str) {
return "\"" + str +
"\""; }
1338 inline string object_json(
int v) {
return to_string(v); }
1339 inline string object_json(
unsigned int v) {
return to_string(v); }
1340 inline string object_json(
float v) {
return to_string(v); }
1341 inline string object_json(
double v) {
return to_string(v); }
1342 inline string object_json(
char v) {
return to_string(v); }
1343 inline string object_json(
unsigned char v) {
return to_string(v); }
1344 inline string object_json(
long v) {
return to_string(v); }
1345 inline string object_json(
long long v) {
return to_string(v); }
1346 inline string object_json(
unsigned long long v) {
return to_string(v); }
1347 inline string object_json(
bool v) {
return to_string(v); }
1352void mes_trim(
string &s);
1355namespace MedSerialize {
1357 inline int read_binary_data_alloc(
const string &fname,
unsigned char *&data,
unsigned long long &size)
1361 inf.open(fname, ios::in | ios::binary | ios::ate);
1364 SRL_ERR(
"read_binary_data_alloc(): can't open file %s for read\n", fname.c_str());
1369 data =
new unsigned char[size];
1370 inf.seekg(0, ios::beg);
1371 inf.read((
char *)data, size);
1373 boost::crc_32_type checksum_agent;
1374 checksum_agent.process_bytes(data, size);
1375 SRL_LOG(
"read_binary_data_alloc [%s] with crc32 [%zu]\n", fname.c_str(), checksum_agent.checksum());
1382 inline int write_binary_data(
const string &fname,
unsigned char *data,
unsigned long long size)
1387 of.open(fname, ios::out | ios::binary);
1390 SRL_ERR(
"write_binary_data(): can't open file %s for write\n", fname.c_str());
1394 of.write((
char *)data, size);
1402 static int initialization_text_to_map(
const string& text, map<string, string>& init_map) {
1414 vector<size_t> start_pos;
1415 vector<size_t> end_pos;
1416 vector<pair<size_t, size_t>> from_to;
1419 size_t pos = text.find(
"={", 0);
1420 while (pos != string::npos) {
1421 start_pos.push_back(pos);
1422 pos = text.find(
"={", pos + 1);
1426 pos = text.find(
"}", 0);
1427 while (pos != string::npos) {
1428 end_pos.push_back(pos);
1429 pos = text.find(
"}", pos + 1);
1433 if (start_pos.size() > 0 && end_pos.size() > 0) {
1435 int i = 0, j = 0, stack = 0, stack_first = -1, stack_last = -1;
1437 while (j < end_pos.size()) {
1438 if (i < (
int)start_pos.size() && start_pos[i] < end_pos[j]) {
1439 if (stack_first < 0) stack_first = (int)start_pos[i];
1445 SRL_ERR(
"ERROR: Unmatched {} in string %s\n", text.c_str());
1449 if (stack == 0) stack_last = (int)end_pos[j];
1454 from_to.push_back(pair<size_t, size_t>(stack_first, stack_last));
1459 for (
auto &ft : from_to) {
1460 SRL_LOG_D(
"found substring: %d-%d : %s\n", ft.first, ft.second, text.substr(ft.first, ft.second - ft.first + 1).c_str());
1467 string new_text =
"";
1468 map<string, string> replacers;
1469 if (from_to.size() == 0) new_text = text;
1471 new_text = text.substr(0, from_to[0].first + 1);
1473 for (j = 0; j < from_to.size(); j++) {
1474 string name =
"REPLACE_ME_LATER_NUMBER_" + to_string(j);
1475 string replacer = text.substr(from_to[j].first + 2, from_to[j].second - from_to[j].first - 2);
1476 SRL_LOG_D(
"replacer %d : %s -> %s\n", j, name.c_str(), replacer.c_str());
1478 replacers[name] = replacer;
1479 if (j < from_to.size() - 1)
1480 new_text += text.substr(from_to[j].second + 1, from_to[j + 1].first - from_to[j].second);
1482 new_text += text.substr(from_to[j - 1].second + 1, text.length() - from_to[j - 1].second);
1483 SRL_LOG_D(
"new_text is %s\n", new_text.c_str());
1491 vector<string> fields;
1492 boost::split(fields, new_text, boost::is_any_of(
";"));
1495 vector<string> sub_fields;
1497 for (
string& field : fields) {
1498 if (field.size() == 0)
1501 boost::split(sub_fields, field, boost::is_any_of(
"="));
1502 if (sub_fields.size() != 2) {
1503 SRL_ERR(
"Cannot parse \'%s\' from \'%s\'\n", field.c_str(), text.c_str());
1506 mes_trim(sub_fields[0]);
1507 mes_trim(sub_fields[1]);
1508 init_map[sub_fields[0]] = sub_fields[1];
1511 for (
auto &el : init_map) {
1512 if (el.second.compare(0, 24,
"REPLACE_ME_LATER_NUMBER_") == 0) {
1513 init_map[el.first] = replacers[el.second];
1520 static int init_map_from_string(
string text, map<string, string>& init_map) {
1522 if (text ==
"")
return 0;
1527 text.erase(remove_if(text.begin(), text.end(), ::isspace), text.end());
1529 if (MedSerialize::initialization_text_to_map(text, init_map) == -1)
1539 static int read_file_into_string(
const string &fname,
string &data)
1541 ifstream inf(fname);
1543 SRL_ERR(
"MedSerialize::read_file_into_string: Can't open file %s\n", fname.c_str());
1549 while (getline(inf, curr_line)) {
1550 if ((curr_line.size() > 1) && (curr_line[0] !=
'#')) {
1553 string fixed_spaces = boost::regex_replace(curr_line, boost::regex(
"^ +| +$|( ) +|\r|\n|\t+"),
string(
"$1"));
1554 data += fixed_spaces;
1563 static int read_list_into_string(
const string &fname,
string &data)
1565 ifstream inf(fname);
1567 SRL_ERR(
"MedSerialize::read_file_into_string: Can't open file %s\n", fname.c_str());
1573 while (getline(inf, curr_line)) {
1575 if ((curr_line.size() > 1) && (curr_line[0] !=
'#')) {
1577 string fixed_spaces = boost::regex_replace(fixed_spaces, boost::regex(
"\t+"),
" ");
1580 fixed_spaces = boost::regex_replace(curr_line, boost::regex(
"^ +| +$|\r|\n"),
"");
1582 fixed_spaces +=
"\n";
1585 fixed_spaces = boost::regex_replace(fixed_spaces, boost::regex(
" +|\n"),
",");
1588 fixed_spaces = boost::regex_replace(fixed_spaces, boost::regex(
",,+"),
",");
1591 data += fixed_spaces;
1596 if (data.back() ==
',') data.pop_back();
c_str(string)
Definition basic.py:110