1#ifndef __SERIALIZABLE_OBJECT_IMP_LIB_H__
2#define __SERIALIZABLE_OBJECT_IMP_LIB_H__
9#if __cplusplus < 201402L
10template<
bool B,
class T =
void >
11using enable_if_t =
typename enable_if<B, T>::type;
15namespace MedSerialize {
18 static void get_list_names(
char *list_of_args, vector<string> &names) {
20 string s(list_of_args);
21 boost::replace_all(s,
" ",
"");
22 boost::replace_all(s,
"\t",
"");
23 boost::replace_all(s,
"\n",
"");
24 boost::split(names, s, boost::is_any_of(
","));
34 template <
class T>
size_t get_size(T *&v);
35 template <
class T>
size_t serialize(
unsigned char *blob, T *&v);
36 template <
class T>
size_t deserialize(
unsigned char *blob, T *&v);
37 template <
class T>
string object_json(T *&v);
38 template <
class T>
string object_json(
const T *&v);
39 template <
class T>
string object_json(T *
const &v);
40 template <
class T>
size_t get_size(vector<T> &v);
41 template <
class T>
size_t serialize(
unsigned char *blob, vector<T> &v);
42 template <
class T>
size_t deserialize(
unsigned char *blob, vector<T> &v);
43 template <
class T>
string object_json(
const vector<const T> &v);
44 template <
class T>
string object_json(
const vector<T> &v);
45 template <
class T>
string object_json(vector<const T> &v);
46 template <
class T,
class S>
size_t get_size(pair<T, S> &v);
47 template <
class T,
class S>
size_t serialize(
unsigned char *blob, pair<T, S> &v);
48 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, pair<T, S> &v);
49 template <
class T,
class S>
string object_json(pair<const T, const S> &v);
50 template <
class T,
class S>
string object_json(
const pair<T, S> &v);
51 template <
class T,
class S>
string object_json(
const pair<const T, const S> &v);
52 template <
class T,
class S>
size_t get_size(map<T, S> &v);
53 template <
class T,
class S>
size_t serialize(
unsigned char *blob, map<T, S> &v);
54 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, map<T, S> &v);
55 template <
class T,
class S>
string object_json(map<const T, const S> &v);
56 template <
class T,
class S>
string object_json(
const map<const T, const S> &v);
57 template <
class T,
class S>
string object_json(
const map<T, S> &v);
58 template <
class T,
class S>
size_t get_size(unordered_map<T, S> &v);
59 template <
class T,
class S>
size_t serialize(
unsigned char *blob, unordered_map<T, S> &v);
60 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, unordered_map<T, S> &v);
61 template <
class T,
class S>
string object_json(unordered_map<const T, const S> &v);
62 template <
class T,
class S>
string object_json(
const unordered_map<const T, const S> &v);
63 template <
class T,
class S>
string object_json(
const unordered_map<T, S> &v);
64 template <
class T>
size_t get_size(unordered_set<T> &v);
65 template <
class T>
size_t serialize(
unsigned char *blob, unordered_set<T> &v);
66 template <
class T>
size_t deserialize(
unsigned char *blob, unordered_set<T> &v);
67 template <
class T>
string object_json(unordered_set<const T> &v);
68 template <
class T>
string object_json(
const unordered_set<const T> &v);
69 template <
class T>
string object_json(
const unordered_set<T> &v);
70 template <
class T>
size_t get_size(set<T> &v);
71 template <
class T>
size_t serialize(
unsigned char *blob, set<T> &v);
72 template <
class T>
size_t deserialize(
unsigned char *blob, set<T> &v);
73 template <
class T>
string object_json(set<const T> &v);
74 template <
class T>
string object_json(
const set<const T> &v);
75 template <
class T>
string object_json(
const set<T> &v);
76 template <
class T>
size_t get_size(unique_ptr<T> &v);
77 template <
class T>
size_t serialize(
unsigned char *blob, unique_ptr<T> &v);
78 template <
class T>
size_t deserialize(
unsigned char *blob, unique_ptr<T> &v);
79 template <
class T>
string object_json(unique_ptr<T> &v);
80 template <
class T>
string object_json(unique_ptr<const T> &v);
89 template <
class T>
size_t get_size(T &elem)
96 template <
class T>
size_t serialize(
unsigned char *blob, T &elem)
99 memcpy(blob, &elem,
sizeof(T));
106 template <
class T>
size_t deserialize(
unsigned char *blob, T &elem)
109 memcpy(&elem, blob,
sizeof(T));
116 string object_json_spec(T& v, std::true_type , std::false_type) {
119 string enum_nm =
typeid(v).name();
121 enum_nm = std::regex_replace(enum_nm, std::regex(
"^([0-9]*|enum )"),
"");
122 str << enum_nm <<
"::" << (int)v;
126 string object_json_spec(T& v, std::false_type, std::true_type ) {
128 str << v.object_json();
132 string object_json_spec(T& v, std::false_type, std::false_type) {
135 str <<
"UNSUPPORTED::" <<
typeid(v).name();
140 string object_json(T& v) {
141 return object_json_spec(v, std::is_enum<T>{}, std::is_base_of<SerializableObject, T>{});
148 template <
class T>
size_t get_size(T *&elem)
156 size += MedSerialize::get_size(s);
159 string s = elem->my_class_name();
161 size += MedSerialize::get_size(s);
162 size += MedSerialize::get_size((*elem));
169 template <
class T>
size_t serialize(
unsigned char *blob, T *&elem)
178 s = elem->my_class_name();
179 pos += MedSerialize::serialize<string>(blob + pos, s);
184 pos += MedSerialize::serialize(blob + pos, (*elem));
191 template <
class T>
size_t deserialize(
unsigned char *blob, T *&elem)
199 pos += MedSerialize::deserialize<string>(blob + pos, cname);
203 if (cname ==
"NULL") {
209 elem = (T *)dummy.new_polymorphic(cname);
218 pos += MedSerialize::deserialize(blob + pos, (*elem));
225 template <
class T>
string object_json(T *&v) {
229 str << MedSerialize::object_json((
const T&)*v);
235 template <
class T>
string object_json(
const T *&v) {
239 str << MedSerialize::object_json((
const T&)*v);
245 template <
class T>
string object_json(T *
const &v) {
249 str << MedSerialize::object_json((
const T&)*v);
258 template <
class T>
size_t get_size(unique_ptr<T> &elem)
263 if (elem.get() == NULL) {
266 size += MedSerialize::get_size(s);
269 string s = elem->my_class_name();
271 size += MedSerialize::get_size(s);
272 size += MedSerialize::get_size((*elem));
279 template <
class T>
size_t serialize(
unsigned char *blob, unique_ptr<T> &elem)
287 if (elem.get() != NULL)
288 s = elem->my_class_name();
289 pos += MedSerialize::serialize<string>(blob + pos, s);
293 if (elem.get() != NULL)
294 pos += MedSerialize::serialize(blob + pos, (*elem));
301 template <
class T>
size_t deserialize(
unsigned char *blob, unique_ptr<T> &elem)
309 pos += MedSerialize::deserialize<string>(blob + pos, cname);
313 if (cname ==
"NULL") {
319 elem = unique_ptr<T>((T *)dummy.new_polymorphic(cname));
321 if (elem.get() == NULL) {
322 elem = unique_ptr<T>(
new T);
328 pos += MedSerialize::deserialize(blob + pos, (*elem.get()));
335 template <
class T>
string object_json(unique_ptr<T> &v) {
338 if (v.get() != NULL) {
339 str << MedSerialize::object_json((
const T&)*v);
345 template <
class T>
string object_json(unique_ptr<const T> &v) {
348 if (v.get() != NULL) {
349 str << MedSerialize::object_json((
const T&)*v);
360 template<>
inline size_t get_size<string>(
string &str) {
362 size +=
sizeof(size_t);
363 size += str.length() + 1;
368 template<>
inline size_t serialize<string>(
unsigned char *blob,
string &str)
371 size_t len = str.length();
373 memcpy(blob, &len,
sizeof(
size_t)); pos +=
sizeof(size_t);
374 memcpy(blob + pos, str.c_str(), len); pos += len;
375 blob[pos] = 0; pos++;
381 template<>
inline size_t deserialize<string>(
unsigned char *blob,
string &str)
386 memcpy(&len, blob,
sizeof(
size_t)); pos +=
sizeof(size_t);
388 string new_s((
char *)&blob[pos]);
398 template<
class T>
size_t get_size(vector<T> &v)
401 size_t size = 0, len = v.size();
402 size += MedSerialize::get_size<size_t>(len);
404 size += MedSerialize::get_size(elem);
410 template <
class T>
size_t serialize(
unsigned char *blob, vector<T> &v)
414 size_t pos = 0, len = v.size();
415 pos += MedSerialize::serialize<size_t>(blob + pos, len);
418 pos += MedSerialize::serialize(blob + pos, elem);
423 template <
class T>
size_t deserialize(
unsigned char *blob, vector<T> &v)
428 pos += MedSerialize::deserialize<size_t>(blob + pos, len);
429 if (len != v.size()) v.clear();
433 pos += MedSerialize::deserialize(blob + pos, elem);
438 template <
class T>
string object_json(
const vector<const T> &v) {
442 for (
size_t i = 0; i < v.size(); ++i)
446 str << MedSerialize::object_json(v[i]);
451 template <
class T>
string object_json(
const vector<T> &v) {
455 for (
size_t i = 0; i < v.size(); ++i)
459 str << MedSerialize::object_json(v[i]);
464 template <
class T>
string object_json(vector<const T> &v) {
468 for (
size_t i = 0; i < v.size(); ++i)
472 str << MedSerialize::object_json(v[i]);
482 template <
class T>
size_t get_size(set<T> &v)
484 size_t size = 0, len = v.size();
485 size += MedSerialize::get_size<size_t>(len);
487 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
488 size += MedSerialize::get_size(*it);
494 template <
class T>
size_t serialize(
unsigned char *blob, set<T> &v)
497 size_t pos = 0, len = v.size();
498 pos += MedSerialize::serialize<size_t>(blob + pos, len);
501 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
502 pos += MedSerialize::serialize(blob + pos, (T &)(*it));
509 template <
class T>
size_t deserialize(
unsigned char *blob, set<T> &v)
512 pos += MedSerialize::deserialize<size_t>(blob + pos, len);
516 for (
int i = 0; i < len; i++) {
517 pos += MedSerialize::deserialize(blob + pos, elem);
524 template <
class T>
string object_json(set<const T> &v) {
529 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
533 str << MedSerialize::object_json((
const T &)(*it));
539 template <
class T>
string object_json(
const set<const T> &v) {
544 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
548 str << MedSerialize::object_json((
const T &)(*it));
554 template <
class T>
string object_json(
const set<T> &v) {
559 for (
typename set<T>::iterator it = v.begin(); it != v.end(); ++it)
563 str << MedSerialize::object_json((T &)(*it));
574 template <
class T>
size_t get_size(unordered_set<T> &v)
576 size_t size = 0, len = v.size();
577 size += MedSerialize::get_size<size_t>(len);
579 size += MedSerialize::get_size(elem);
585 template <
class T>
size_t serialize(
unsigned char *blob, unordered_set<T> &v)
588 size_t pos = 0, len = v.size();
589 pos += MedSerialize::serialize<size_t>(blob + pos, len);
592 pos += MedSerialize::serialize(blob + pos, elem);
598 template <
class T>
size_t deserialize(
unsigned char *blob, unordered_set<T> &v)
601 pos += MedSerialize::deserialize<size_t>(blob + pos, len);
605 for (
int i = 0; i < len; i++) {
606 pos += MedSerialize::deserialize(blob + pos, elem);
613 template <
class T>
string object_json(unordered_set<const T> &v) {
622 str << MedSerialize::object_json(e);
628 template <
class T>
string object_json(
const unordered_set<const T> &v) {
637 str << MedSerialize::object_json(e);
643 template <
class T>
string object_json(
const unordered_set<T> &v) {
652 str << MedSerialize::object_json(e);
664 template <
class T,
class S>
size_t get_size(pair<T, S> &v)
667 T *t = (T *)&v.first;
668 S *s = (S *)&v.second;
669 size += MedSerialize::get_size((*t));
670 size += MedSerialize::get_size((*s));
676 template <
class T,
class S>
size_t serialize(
unsigned char *blob, pair<T, S> &v)
680 T *t = (T *)&v.first;
681 S *s = (S *)&v.second;
682 pos += MedSerialize::serialize(blob + pos, (*t));
683 pos += MedSerialize::serialize(blob + pos, (*s));
689 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, pair<T, S> &v)
695 pos += MedSerialize::deserialize(blob + pos, t);
696 pos += MedSerialize::deserialize(blob + pos, s);
703 template <
class T,
class S>
string object_json(
const pair<const T, const S> &v) {
706 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
709 str << MedSerialize::object_json(v.first);
710 str <<
"," << MedSerialize::object_json(v.second);
715 template <
class T,
class S>
string object_json(pair<const T, const S> &v) {
718 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
721 str << MedSerialize::object_json(v.first);
722 str <<
"," << MedSerialize::object_json(v.second);
727 template <
class T,
class S>
string object_json(
const pair<T, S> &v) {
730 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
733 str << MedSerialize::object_json(v.first);
734 str <<
"," << MedSerialize::object_json(v.second);
743 template <
class T,
class S>
size_t get_size(map<T, S> &v)
745 size_t size = 0, len = v.size();
746 size += MedSerialize::get_size<size_t>(len);
747 for (
auto &elem : v) {
748 T *t = (T *)&elem.first;
749 S *s = (S *)&elem.second;
750 size += MedSerialize::get_size((*t));
751 size += MedSerialize::get_size((*s));
757 template <
class T,
class S>
size_t serialize(
unsigned char *blob, map<T, S> &v)
760 size_t pos = 0, len = v.size();
761 pos += MedSerialize::serialize<size_t>(blob + pos, len);
763 for (
auto &elem : v) {
764 T *t = (T *)&elem.first;
765 S *s = (S *)&elem.second;
766 pos += MedSerialize::serialize(blob + pos, (*t));
767 pos += MedSerialize::serialize(blob + pos, (*s));
773 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, map<T, S> &v)
777 pos += MedSerialize::deserialize(blob + pos, len);
782 for (
int i = 0; i < len; i++) {
783 pos += MedSerialize::deserialize(blob + pos, t);
784 pos += MedSerialize::deserialize(blob + pos, s);
791 template <
class T,
class S>
string object_json(map<const T, const S> &v) {
796 for (
auto it = v.begin(); it != v.end(); ++it)
802 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
805 str << MedSerialize::object_json((
const T&)it->first);
806 str <<
"," << MedSerialize::object_json((
const S &)it->second);
815 template <
class T,
class S>
string object_json(
const map<const T, const S> &v) {
820 for (
auto it = v.begin(); it != v.end(); ++it)
826 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
829 str << MedSerialize::object_json((
const T&)it->first);
830 str <<
"," << MedSerialize::object_json((
const S &)it->second);
839 template <
class T,
class S>
string object_json(
const map<T, S> &v) {
844 for (
auto it = v.begin(); it != v.end(); ++it)
850 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
853 str << MedSerialize::object_json((T&)it->first);
854 str <<
"," << MedSerialize::object_json((S &)it->second);
868 template <
class T,
class S>
size_t get_size(unordered_map<T, S> &v)
870 size_t size = 0, len = v.size();
871 size += MedSerialize::get_size<size_t>(len);
873 for (
typename unordered_map<T, S>::iterator it = v.begin(); it != v.end(); ++it) {
874 size += MedSerialize::get_size((T &)(it->first));
875 size += MedSerialize::get_size((S &)(it->second));
882 template <
class T,
class S>
size_t serialize(
unsigned char *blob, unordered_map<T, S> &v)
884 size_t pos = 0, len = v.size();
885 pos += MedSerialize::serialize<size_t>(blob + pos, len);
888 for (
typename unordered_map<T, S>::iterator it = v.begin(); it != v.end(); ++it) {
889 pos += MedSerialize::serialize(blob + pos, (T &)(it->first));
890 pos += MedSerialize::serialize(blob + pos, (S &)(it->second));
897 template <
class T,
class S>
size_t deserialize(
unsigned char *blob, unordered_map<T, S> &v)
900 pos += MedSerialize::deserialize(blob + pos, len);
905 for (
int i = 0; i < len; i++) {
906 pos += MedSerialize::deserialize(blob + pos, t);
907 pos += MedSerialize::deserialize(blob + pos, s);
914 template <
class T,
class S>
string object_json(unordered_map<const T, const S> &v) {
919 for (
auto it = v.begin(); it != v.end(); ++it)
925 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
928 str << MedSerialize::object_json((
const T &)it->first);
929 str <<
"," << MedSerialize::object_json((
const S &)it->second);
938 template <
class T,
class S>
string object_json(
const unordered_map<const T, const S> &v) {
943 for (
auto it = v.begin(); it != v.end(); ++it)
949 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
952 str << MedSerialize::object_json((
const T &)it->first);
953 str <<
"," << MedSerialize::object_json((
const S &)it->second);
962 template <
class T,
class S>
string object_json(
const unordered_map<T, S> &v) {
967 for (
auto it = v.begin(); it != v.end(); ++it)
973 str <<
"{ \"Object\":\"pair<" <<
typeid(T).name() <<
", " <<
typeid(S).name() <<
">\", ";
976 str << MedSerialize::object_json((T &)it->first);
977 str <<
"," << MedSerialize::object_json((S &)it->second);
991 template <
class T,
class... Ts>
size_t get_size(T& elem, Ts&... args)
995 size += MedSerialize::get_size(elem);
996 size += MedSerialize::get_size(args...);
1003 template <
class T,
class... Ts>
size_t serialize(
unsigned char *blob, T& elem, Ts&... args)
1007 pos += MedSerialize::serialize(blob, elem);
1008 pos += MedSerialize::serialize(blob + pos, args...);
1015 template <
typename T,
typename... Ts>
size_t deserialize(
unsigned char *blob, T& elem, Ts&... args)
1019 pos += MedSerialize::deserialize(blob, elem);
1020 pos += MedSerialize::deserialize(blob + pos, args...);
1026 template <
class T,
class... Ts>
string object_json(T& elem, Ts&... args) {
1029 str << MedSerialize::object_json(elem);
1030 str <<
"," << MedSerialize::object_json(args...);
1040 template<
class T>
size_t serializer(
unsigned char *blob,
int counter, vector<string> &names, T &elem)
1046 pos += MedSerialize::serialize(blob + pos, pos);
1050 pos += MedSerialize::serialize(blob + pos, names[counter]);
1054 pos += MedSerialize::serialize(blob + pos, elem);
1059 MedSerialize::serialize(blob, pos);
1067 template<
class T,
class... Ts>
size_t serializer(
unsigned char *blob,
int counter, vector<string> &names, T &elem, Ts&...args)
1073 pos += MedSerialize::serializer(blob, counter, names, elem);
1078 pos += MedSerialize::serializer(blob + pos, counter + 1, names, args...);
1086 template<typename T, enable_if_t<std::is_base_of<SerializableObject, T>::value,
int> = 0>
string get_name() {
1087 unique_ptr<T> cl = unique_ptr<T>(
new T);
1088 string cl_name = cl->my_class_name();
1093 template<typename T, enable_if_t<!std::is_base_of<SerializableObject, T>::value,
int> = 0>
string get_name() {
1094 string cl_name =
"primitive_type";
1100 template<
class T>
size_t deserializer(
unsigned char *blob,
int counter, vector<string> &names, map <string, size_t> &name2pos, T &elem)
1102 string name = names[counter];
1107 if (name2pos.find(name) != name2pos.end()) {
1109 pos += MedSerialize::deserialize(blob + name2pos[name], elem);
1114 string cl_name = get_name<T>();
1115 cerr <<
"WARNING: In \"" << cl_name <<
"\" element " << name <<
" not serialized... will be deserialized to its default\n";
1123 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)
1125 string name = names[counter];
1131 pos += MedSerialize::deserializer(blob, counter, names, name2pos, elem);
1134 pos += MedSerialize::deserializer(blob, counter + 1, names, name2pos, args...);
1142 template<
class T>
size_t get_sizer(
int counter, vector<string> &names, T &elem)
1144 string name = names[counter];
1149 size += MedSerialize::get_size(size);
1150 size += MedSerialize::get_size(name);
1151 size += MedSerialize::get_size(elem);
1158 template<
class T,
class... Ts>
size_t get_sizer(
int counter, vector<string> &names, T &elem, Ts&...args)
1160 string name = names[counter];
1166 size += MedSerialize::get_sizer(counter, names, elem);
1169 size += MedSerialize::get_sizer(counter + 1, names, args...);
1176 template<
class... Ts>
size_t get_size_top(
char *list_of_args, Ts&...args) {
1182 vector<string> names;
1183 MedSerialize::get_list_names(list_of_args, names);
1185 size += MedSerialize::get_size(size);
1186 size += MedSerialize::get_sizer(0, names, args...);
1194 template<
class... Ts>
size_t serialize_top(
unsigned char *blob,
char *list_of_args, Ts&...args) {
1199 vector<string> names;
1200 MedSerialize::get_list_names(list_of_args, names);
1204 pos += MedSerialize::serialize(blob + pos, pos);
1207 pos += MedSerialize::serializer(blob + pos, 0, names, args...);
1211 MedSerialize::serialize(blob, pos);
1218 template<
class... Ts>
size_t deserialize_top(
unsigned char *blob,
char *list_of_args, Ts&...args) {
1223 vector<string> names;
1224 MedSerialize::get_list_names(list_of_args, names);
1227 size_t tot_size = 0, pos = 0;
1228 pos += MedSerialize::deserialize(blob, tot_size);
1232 map<string, size_t> name2pos;
1233 while (pos < tot_size) {
1237 size_t orig_curr_pos = pos;
1239 pos += MedSerialize::deserialize(blob + pos, curr_size);
1244 pos += MedSerialize::deserialize(blob + pos, name);
1248 name2pos[name] = pos;
1251 pos = orig_curr_pos + curr_size;
1257 for (
auto &e : name2pos)
1258 SRL_LOG(
"dtop(5.5) name2pos [ %s ] = %d\n", e.first.
c_str(), e.second);
1261 MedSerialize::deserializer(blob, 0, names, name2pos, args...);
1270 template<
class T>
string object_json_rec(
int counter, vector<string> &names, T &elem) {
1274 str <<
"\n\t\"" << names[counter] <<
"\": ";
1275 str << MedSerialize::object_json(elem);
1280 template<
class T,
class... Ts>
string object_json_rec(
int counter, vector<string> &names, T &elem, Ts&...args) {
1284 str << MedSerialize::object_json_rec(counter, names, elem);
1286 if (counter + 1 < names.size())
1289 str << MedSerialize::object_json_rec(counter + 1, names, args...);
1294 template<
class... Ts>
string object_json_start(
const string &cls_name,
int vers,
char *list_of_args, Ts&...args) {
1295 vector<string> names;
1296 MedSerialize::get_list_names(list_of_args, names);
1300 str <<
"{\n\t\"Object\":\"" << cls_name <<
"\",\n\t\"Version\":" << vers <<
",";
1310 string res = MedSerialize::object_json_rec(0, names, args...);
1311 str << res <<
"\n}";
1317 template<>
inline string object_json<const int>(
const int &v) {
return to_string(v); }
1318 template<>
inline string object_json<const unsigned int>(
const unsigned int &v) {
return to_string(v); }
1319 template<>
inline string object_json<const float>(
const float &v) {
return to_string(v); }
1320 template<>
inline string object_json<const double>(
const double &v) {
return to_string(v); }
1321 template<>
inline string object_json<const char>(
const char &v) {
return to_string(v); }
1322 template<>
inline string object_json<const unsigned char>(
const unsigned char &v) {
return to_string(v); }
1323 template<>
inline string object_json<const long>(
const long &v) {
return to_string(v); }
1324 template<>
inline string object_json<const long long>(
const long long &v) {
return to_string(v); }
1325 template<>
inline string object_json<const unsigned long long>(
const unsigned long long &v) {
return to_string(v); }
1326 template<>
inline string object_json<const bool>(
const bool &v) {
return to_string(v); }
1327 template<>
inline string object_json<int>(
int &v) {
return to_string(v); }
1328 template<>
inline string object_json<unsigned int>(
unsigned int &v) {
return to_string(v); }
1329 template<>
inline string object_json<float>(
float &v) {
return to_string(v); }
1330 template<>
inline string object_json<double>(
double &v) {
return to_string(v); }
1331 template<>
inline string object_json<char>(
char &v) {
return to_string(v); }
1332 template<>
inline string object_json<unsigned char>(
unsigned char &v) {
return to_string(v); }
1333 template<>
inline string object_json<long>(
long &v) {
return to_string(v); }
1334 template<>
inline string object_json<long long>(
long long &v) {
return to_string(v); }
1335 template<>
inline string object_json<unsigned long long>(
unsigned long long &v) {
return to_string(v); }
1336 template<>
inline string object_json<bool>(
bool &v) {
return to_string(v); }
1337 template<>
inline string object_json<string>(
string &str) {
return "\"" + str +
"\""; }
1338 template<>
inline string object_json<const string>(
const string &str) {
return "\"" + str +
"\""; }
1339 inline string object_json(
int v) {
return to_string(v); }
1340 inline string object_json(
unsigned int v) {
return to_string(v); }
1341 inline string object_json(
float v) {
return to_string(v); }
1342 inline string object_json(
double v) {
return to_string(v); }
1343 inline string object_json(
char v) {
return to_string(v); }
1344 inline string object_json(
unsigned char v) {
return to_string(v); }
1345 inline string object_json(
long v) {
return to_string(v); }
1346 inline string object_json(
long long v) {
return to_string(v); }
1347 inline string object_json(
unsigned long long v) {
return to_string(v); }
1348 inline string object_json(
bool v) {
return to_string(v); }
1353void mes_trim(
string &s);
1356namespace MedSerialize {
1358 inline int read_binary_data_alloc(
const string &fname,
unsigned char *&data,
unsigned long long &size)
1362 inf.open(fname, ios::in | ios::binary | ios::ate);
1365 SRL_ERR(
"read_binary_data_alloc(): can't open file %s for read\n", fname.c_str());
1370 data =
new unsigned char[size];
1371 inf.seekg(0, ios::beg);
1372 inf.read((
char *)data, size);
1374 boost::crc_32_type checksum_agent;
1375 checksum_agent.process_bytes(data, size);
1376 SRL_LOG(
"read_binary_data_alloc [%s] with crc32 [%zu]\n", fname.c_str(), checksum_agent.checksum());
1383 inline int write_binary_data(
const string &fname,
unsigned char *data,
unsigned long long size)
1388 of.open(fname, ios::out | ios::binary);
1391 SRL_ERR(
"write_binary_data(): can't open file %s for write\n", fname.c_str());
1395 of.write((
char *)data, size);
1403 static int initialization_text_to_map(
const string& text, map<string, string>& init_map) {
1415 vector<size_t> start_pos;
1416 vector<size_t> end_pos;
1417 vector<pair<size_t, size_t>> from_to;
1420 size_t pos = text.find(
"={", 0);
1421 while (pos != string::npos) {
1422 start_pos.push_back(pos);
1423 pos = text.find(
"={", pos + 1);
1427 pos = text.find(
"}", 0);
1428 while (pos != string::npos) {
1429 end_pos.push_back(pos);
1430 pos = text.find(
"}", pos + 1);
1434 if (start_pos.size() > 0 && end_pos.size() > 0) {
1436 int i = 0, j = 0, stack = 0, stack_first = -1, stack_last = -1;
1438 while (j < end_pos.size()) {
1439 if (i < (
int)start_pos.size() && start_pos[i] < end_pos[j]) {
1440 if (stack_first < 0) stack_first = (int)start_pos[i];
1446 SRL_ERR(
"ERROR: Unmatched {} in string %s\n", text.c_str());
1450 if (stack == 0) stack_last = (int)end_pos[j];
1455 from_to.push_back(pair<size_t, size_t>(stack_first, stack_last));
1460 for (
auto &ft : from_to) {
1461 SRL_LOG_D(
"found substring: %d-%d : %s\n", ft.first, ft.second, text.substr(ft.first, ft.second - ft.first + 1).c_str());
1468 string new_text =
"";
1469 map<string, string> replacers;
1470 if (from_to.size() == 0) new_text = text;
1472 new_text = text.substr(0, from_to[0].first + 1);
1474 for (j = 0; j < from_to.size(); j++) {
1475 string name =
"REPLACE_ME_LATER_NUMBER_" + to_string(j);
1476 string replacer = text.substr(from_to[j].first + 2, from_to[j].second - from_to[j].first - 2);
1477 SRL_LOG_D(
"replacer %d : %s -> %s\n", j, name.c_str(), replacer.c_str());
1479 replacers[name] = replacer;
1480 if (j < from_to.size() - 1)
1481 new_text += text.substr(from_to[j].second + 1, from_to[j + 1].first - from_to[j].second);
1483 new_text += text.substr(from_to[j - 1].second + 1, text.length() - from_to[j - 1].second);
1484 SRL_LOG_D(
"new_text is %s\n", new_text.c_str());
1492 vector<string> fields;
1493 boost::split(fields, new_text, boost::is_any_of(
";"));
1496 vector<string> sub_fields;
1498 for (
string& field : fields) {
1499 if (field.size() == 0)
1502 boost::split(sub_fields, field, boost::is_any_of(
"="));
1503 if (sub_fields.size() != 2) {
1504 SRL_ERR(
"Cannot parse \'%s\' from \'%s\'\n", field.c_str(), text.c_str());
1507 mes_trim(sub_fields[0]);
1508 mes_trim(sub_fields[1]);
1509 init_map[sub_fields[0]] = sub_fields[1];
1512 for (
auto &el : init_map) {
1513 if (el.second.compare(0, 24,
"REPLACE_ME_LATER_NUMBER_") == 0) {
1514 init_map[el.first] = replacers[el.second];
1521 static int init_map_from_string(
string text, map<string, string>& init_map) {
1523 if (text ==
"")
return 0;
1528 text.erase(remove_if(text.begin(), text.end(), ::isspace), text.end());
1530 if (MedSerialize::initialization_text_to_map(text, init_map) == -1)
1540 static int read_file_into_string(
const string &fname,
string &data)
1542 ifstream inf(fname);
1544 SRL_ERR(
"MedSerialize::read_file_into_string: Can't open file %s\n", fname.c_str());
1550 while (getline(inf, curr_line)) {
1551 if ((curr_line.size() > 1) && (curr_line[0] !=
'#')) {
1554 string fixed_spaces = std::regex_replace(curr_line, std::regex(
"^ +| +$|( ) +|\r|\n|\t+"),
string(
"$1"));
1555 data += fixed_spaces;
1564 static int read_list_into_string(
const string &fname,
string &data)
1566 ifstream inf(fname);
1568 SRL_ERR(
"MedSerialize::read_file_into_string: Can't open file %s\n", fname.c_str());
1574 while (getline(inf, curr_line)) {
1576 if ((curr_line.size() > 1) && (curr_line[0] !=
'#')) {
1578 string fixed_spaces = std::regex_replace(fixed_spaces, std::regex(
"\t+"),
" ");
1581 fixed_spaces = std::regex_replace(curr_line, std::regex(
"^ +| +$|\r|\n"),
"");
1583 fixed_spaces +=
"\n";
1586 fixed_spaces = std::regex_replace(fixed_spaces, std::regex(
" +|\n"),
",");
1589 fixed_spaces = std::regex_replace(fixed_spaces, std::regex(
",,+"),
",");
1592 data += fixed_spaces;
1597 if (data.back() ==
',') data.pop_back();
c_str(string)
Definition basic.py:110