Medial Code Documentation
Loading...
Searching...
No Matches
SerializableObject.h
Go to the documentation of this file.
1#ifndef _SERIALIZABLE_OBJECT_LIB_H_
2#define _SERIALIZABLE_OBJECT_LIB_H_
3
5#include <boost/algorithm/string/split.hpp>
6#include <boost/algorithm/string/classification.hpp>
7#include <boost/algorithm/string.hpp>
8#include <boost/crc.hpp>
9#include <boost/algorithm/string/replace.hpp>
10
11#include <cstring>
12#include <string>
13#include <unordered_set>
14#include <set>
15#include <unordered_map>
16#include <typeinfo>
17#include <map>
18#include <vector>
19#include <regex>
20
21using namespace std;
22#undef LOCAL_SECTION
23#define LOCAL_SECTION LOG_INFRA
24
25#define SRL_LOG(...) global_logger.log(LOG_SRL, LOG_DEF_LEVEL, __VA_ARGS__)
26#define SRL_LOG_D(...) global_logger.log(LOG_SRL, DEBUG_LOG_LEVEL, __VA_ARGS__)
27#define SRL_ERR(...) global_logger.log(LOG_SRL, MAX_LOG_LEVEL, __VA_ARGS__)
28
34public:
37 virtual int version() const { return 0; }
38
42 virtual string my_class_name() const { return "SerializableObject"; }
43
46 virtual void serialized_fields_name(vector<string> &field_names) const {};
47
50 virtual void *new_polymorphic(string derived_name) { return NULL; }
51
52 // next adds an option to add some actions before and after a serialization is done
53 virtual void pre_serialization() {};
54 virtual void post_deserialization() {};
55
56 // Virtual serialization
57 virtual size_t get_size() { return 0; }
58 virtual size_t serialize(unsigned char *blob) { return 0; }
59 virtual size_t deserialize(unsigned char *blob) { return 0; }
60
61 // APIs for vectors
62 size_t serialize_vec(vector<unsigned char> &blob) { size_t size = get_size(); blob.resize(size); return serialize(&blob[0]); }
63 size_t deserialize_vec(vector<unsigned char> &blob) { return deserialize(&blob[0]); }
64 virtual size_t serialize(vector<unsigned char> &blob) { return serialize_vec(blob); }
65 virtual size_t deserialize(vector<unsigned char> &blob) { return deserialize_vec(blob); }
66
67
68
70 virtual int read_from_file(const string &fname);
71
73 virtual int write_to_file(const string &fname);
74
76 virtual int read_from_file_unsafe(const string &fname);
77
79 int init_from_string(string init_string);
80 int init_params_from_file(string init_file);
81 int init_param_from_file(string file_str, string &param);
82 virtual int init(map<string, string>& map) { return 0; }
83
84 int update_from_string(const string &init_string);
85 virtual int update(map<string, string>& map) { return init(map); }
86
87 virtual string object_json() const;
88private:
89 void _read_from_file(const string &fname, bool throw_on_version_error);
90};
91
92template <class T> void copy_serializable_object(T &source, T& dst) {
93 vector<unsigned char> blob;
94 source.serialize_vec(blob);
95 dst.deserialize_vec(blob);
96}
97
98
99// generalized safe stoi/stof
100inline float med_stof(const string &_Str)
101{
102 try
103 {
104 return stof(_Str);
105 }
106 catch (exception &e)
107 {
108 HMTHROW_AND_ERR("invalid stof argument [%s]\n", _Str.c_str());
109 }
110}
111
112inline int med_stoi(const string &_Str)
113{
114 try
115 {
116 return stoi(_Str);
117 }
118 catch (exception &e)
119 {
120 HMTHROW_AND_ERR("invalid stoi argument [%s]\n", _Str.c_str());
121 }
122}
123
124inline long long med_stoll(const string &_Str)
125{
126 try
127 {
128 return stoll(_Str);
129 }
130 catch (exception &e)
131 {
132 HMTHROW_AND_ERR("invalid stoll argument [%s]\n", _Str.c_str());
133 }
134}
135
142#define MEDSERIALIZE_SUPPORT(Type) \
143namespace MedSerialize { \
144 template<> inline size_t get_size<Type>(Type &elem) { return elem.get_size(); } \
145 template<> inline size_t serialize<Type>(unsigned char *blob, Type &elem) { return elem.serialize(blob); } \
146 template<> inline size_t deserialize<Type>(unsigned char *blob, Type &elem) { return elem.deserialize(blob); } \
147 template<> inline string object_json<const Type>(const Type &elem) { return elem.object_json(); } \
148}
149
154//size_t serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize(blob, __VA_ARGS__); }
155
156#define ADD_SERIALIZATION_FUNCS(...) \
157 virtual size_t get_size() { pre_serialization(); return MedSerialize::get_size_top(#__VA_ARGS__, __VA_ARGS__); } \
158 virtual size_t serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize_top(blob, #__VA_ARGS__, __VA_ARGS__); } \
159 virtual size_t deserialize(unsigned char *blob) { size_t size = MedSerialize::deserialize_top(blob, #__VA_ARGS__, __VA_ARGS__); post_deserialization(); return size;} \
160 virtual void serialized_fields_name(vector<string> &field_names) const { MedSerialize::get_list_names(#__VA_ARGS__, field_names); } \
161 virtual string object_json() const { return MedSerialize::object_json_start(my_class_name(), this->version(), #__VA_ARGS__, __VA_ARGS__); }
162
163// in some cases we must add the serializations in the implementation file due to forward declerations issues
164// the following is the same as the previous but should be placed in the cpp file
165#define ADD_SERIALIZATION_HEADERS() \
166 virtual size_t get_size(); \
167 virtual size_t serialize(unsigned char *blob); \
168 virtual size_t deserialize(unsigned char *blob); \
169 virtual void serialized_fields_name(vector<string> &field_names) const; \
170 virtual string object_json() const;
171
172#define ADD_SERIALIZATION_FUNCS_CPP(ClassName,...) \
173 size_t ClassName::get_size() { pre_serialization(); return MedSerialize::get_size_top(#__VA_ARGS__, __VA_ARGS__); } \
174 size_t ClassName::serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize_top(blob, #__VA_ARGS__, __VA_ARGS__); } \
175 size_t ClassName::deserialize(unsigned char *blob) { return MedSerialize::deserialize_top(blob, #__VA_ARGS__, __VA_ARGS__); post_deserialization();} \
176 void ClassName::serialized_fields_name(vector<string> &field_names) const { MedSerialize::get_list_names(#__VA_ARGS__, field_names); } \
177 string ClassName::object_json() const { return MedSerialize::object_json_start(my_class_name(), this->version(), #__VA_ARGS__, __VA_ARGS__); }
178
179
180#define ADD_CLASS_NAME(Type) string my_class_name() const {return string(#Type);}
181
182#define CONDITIONAL_NEW_CLASS(s,c) \
183 if (s == string(#c)) return new c;
184
185
186#include "SerializableObject_imp.h"
187
188#endif
Logger.h - allowing logs with more control.
Definition SerializableObject.h:33
virtual size_t get_size()
Gets bytes sizes for serializations.
Definition SerializableObject.h:57
int init_from_string(string init_string)
Init from string.
Definition SerializableObject.cpp:112
virtual int write_to_file(const string &fname)
serialize model and write to file
Definition SerializableObject.cpp:81
virtual int read_from_file(const string &fname)
read and deserialize model
Definition SerializableObject.cpp:74
virtual size_t serialize(unsigned char *blob)
Serialiazing object to blob memory. return number ob bytes wrote to memory.
Definition SerializableObject.h:58
virtual string my_class_name() const
For better handling of serializations it is highly recommended that each SerializableObject inheritin...
Definition SerializableObject.h:42
virtual int update(map< string, string > &map)
Virtual to update object from parsed fields.
Definition SerializableObject.h:85
virtual int read_from_file_unsafe(const string &fname)
read and deserialize model without checking version number - unsafe read
Definition SerializableObject.cpp:67
virtual void serialized_fields_name(vector< string > &field_names) const
The names of the serialized fields.
Definition SerializableObject.h:46
virtual void * new_polymorphic(string derived_name)
for polymorphic classes that want to be able to serialize/deserialize a pointer * to the derived clas...
Definition SerializableObject.h:50
virtual size_t deserialize(unsigned char *blob)
Deserialiazing blob to object. returns number of bytes read.
Definition SerializableObject.h:59
virtual int init(map< string, string > &map)
Virtual to init object from parsed fields.
Definition SerializableObject.h:82
virtual int version() const
Relevant for serializations.
Definition SerializableObject.h:37