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#define LOCAL_SECTION LOG_INFRA
23
24#define SRL_LOG(...) global_logger.log(LOG_SRL, LOG_DEF_LEVEL, __VA_ARGS__)
25#define SRL_LOG_D(...) global_logger.log(LOG_SRL, DEBUG_LOG_LEVEL, __VA_ARGS__)
26#define SRL_ERR(...) global_logger.log(LOG_SRL, MAX_LOG_LEVEL, __VA_ARGS__)
27
33public:
36 virtual int version() const { return 0; }
37
41 virtual string my_class_name() const { return "SerializableObject"; }
42
45 virtual void serialized_fields_name(vector<string> &field_names) const {};
46
49 virtual void *new_polymorphic(string derived_name) { return NULL; }
50
51 // next adds an option to add some actions before and after a serialization is done
52 virtual void pre_serialization() {};
53 virtual void post_deserialization() {};
54
55 // Virtual serialization
56 virtual size_t get_size() { return 0; }
57 virtual size_t serialize(unsigned char *blob) { return 0; }
58 virtual size_t deserialize(unsigned char *blob) { return 0; }
59
60 // APIs for vectors
61 size_t serialize_vec(vector<unsigned char> &blob) { size_t size = get_size(); blob.resize(size); return serialize(&blob[0]); }
62 size_t deserialize_vec(vector<unsigned char> &blob) { return deserialize(&blob[0]); }
63 virtual size_t serialize(vector<unsigned char> &blob) { return serialize_vec(blob); }
64 virtual size_t deserialize(vector<unsigned char> &blob) { return deserialize_vec(blob); }
65
66
67
69 virtual int read_from_file(const string &fname);
70
72 virtual int write_to_file(const string &fname);
73
75 virtual int read_from_file_unsafe(const string &fname);
76
78 int init_from_string(string init_string);
79 int init_params_from_file(string init_file);
80 int init_param_from_file(string file_str, string &param);
81 virtual int init(map<string, string>& map) { return 0; }
82
83 int update_from_string(const string &init_string);
84 virtual int update(map<string, string>& map) { return init(map); }
85
86 virtual string object_json() const;
87private:
88 void _read_from_file(const string &fname, bool throw_on_version_error);
89};
90
91template <class T> void copy_serializable_object(T &source, T& dst) {
92 vector<unsigned char> blob;
93 source.serialize_vec(blob);
94 dst.deserialize_vec(blob);
95}
96
97
98// generalized safe stoi/stof
99inline float med_stof(const string &_Str)
100{
101 try
102 {
103 return stof(_Str);
104 }
105 catch (exception &e)
106 {
107 HMTHROW_AND_ERR("invalid stof argument [%s]\n", _Str.c_str());
108 }
109}
110
111inline int med_stoi(const string &_Str)
112{
113 try
114 {
115 return stoi(_Str);
116 }
117 catch (exception &e)
118 {
119 HMTHROW_AND_ERR("invalid stoi argument [%s]\n", _Str.c_str());
120 }
121}
122
123inline long long med_stoll(const string &_Str)
124{
125 try
126 {
127 return stoll(_Str);
128 }
129 catch (exception &e)
130 {
131 HMTHROW_AND_ERR("invalid stoll argument [%s]\n", _Str.c_str());
132 }
133}
134
141#define MEDSERIALIZE_SUPPORT(Type) \
142namespace MedSerialize { \
143 template<> inline size_t get_size<Type>(Type &elem) { return elem.get_size(); } \
144 template<> inline size_t serialize<Type>(unsigned char *blob, Type &elem) { return elem.serialize(blob); } \
145 template<> inline size_t deserialize<Type>(unsigned char *blob, Type &elem) { return elem.deserialize(blob); } \
146 template<> inline string object_json<const Type>(const Type &elem) { return elem.object_json(); } \
147}
148
153//size_t serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize(blob, __VA_ARGS__); }
154
155#define ADD_SERIALIZATION_FUNCS(...) \
156 virtual size_t get_size() { pre_serialization(); return MedSerialize::get_size_top(#__VA_ARGS__, __VA_ARGS__); } \
157 virtual size_t serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize_top(blob, #__VA_ARGS__, __VA_ARGS__); } \
158 virtual size_t deserialize(unsigned char *blob) { size_t size = MedSerialize::deserialize_top(blob, #__VA_ARGS__, __VA_ARGS__); post_deserialization(); return size;} \
159 virtual void serialized_fields_name(vector<string> &field_names) const { MedSerialize::get_list_names(#__VA_ARGS__, field_names); } \
160 virtual string object_json() const { return MedSerialize::object_json_start(my_class_name(), this->version(), #__VA_ARGS__, __VA_ARGS__); }
161
162// in some cases we must add the serializations in the implementation file due to forward declerations issues
163// the following is the same as the previous but should be placed in the cpp file
164#define ADD_SERIALIZATION_HEADERS() \
165 virtual size_t get_size(); \
166 virtual size_t serialize(unsigned char *blob); \
167 virtual size_t deserialize(unsigned char *blob); \
168 virtual void serialized_fields_name(vector<string> &field_names) const; \
169 virtual string object_json() const;
170
171#define ADD_SERIALIZATION_FUNCS_CPP(ClassName,...) \
172 size_t ClassName::get_size() { pre_serialization(); return MedSerialize::get_size_top(#__VA_ARGS__, __VA_ARGS__); } \
173 size_t ClassName::serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize_top(blob, #__VA_ARGS__, __VA_ARGS__); } \
174 size_t ClassName::deserialize(unsigned char *blob) { return MedSerialize::deserialize_top(blob, #__VA_ARGS__, __VA_ARGS__); post_deserialization();} \
175 void ClassName::serialized_fields_name(vector<string> &field_names) const { MedSerialize::get_list_names(#__VA_ARGS__, field_names); } \
176 string ClassName::object_json() const { return MedSerialize::object_json_start(my_class_name(), this->version(), #__VA_ARGS__, __VA_ARGS__); }
177
178
179#define ADD_CLASS_NAME(Type) string my_class_name() const {return string(#Type);}
180
181#define CONDITIONAL_NEW_CLASS(s,c) \
182 if (s == string(#c)) return new c;
183
184
185#include "SerializableObject_imp.h"
186
187#endif
Logger.h - allowing logs with more control.
Definition SerializableObject.h:32
virtual size_t get_size()
Gets bytes sizes for serializations.
Definition SerializableObject.h:56
int init_from_string(string init_string)
Init from string.
Definition SerializableObject.cpp:111
virtual int write_to_file(const string &fname)
serialize model and write to file
Definition SerializableObject.cpp:80
virtual int read_from_file(const string &fname)
read and deserialize model
Definition SerializableObject.cpp:73
virtual size_t serialize(unsigned char *blob)
Serialiazing object to blob memory. return number ob bytes wrote to memory.
Definition SerializableObject.h:57
virtual string my_class_name() const
For better handling of serializations it is highly recommended that each SerializableObject inheritin...
Definition SerializableObject.h:41
virtual int update(map< string, string > &map)
Virtual to update object from parsed fields.
Definition SerializableObject.h:84
virtual int read_from_file_unsafe(const string &fname)
read and deserialize model without checking version number - unsafe read
Definition SerializableObject.cpp:66
virtual void serialized_fields_name(vector< string > &field_names) const
The names of the serialized fields.
Definition SerializableObject.h:45
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:49
virtual size_t deserialize(unsigned char *blob)
Deserialiazing blob to object. returns number of bytes read.
Definition SerializableObject.h:58
virtual int init(map< string, string > &map)
Virtual to init object from parsed fields.
Definition SerializableObject.h:81
virtual int version() const
Relevant for serializations.
Definition SerializableObject.h:36
float stof(const std::string &value, size_t *pos=nullptr)
A faster implementation of stof(). See documentation of std::stof() for more information....
Definition strtonum.h:467
Definition BFloat16.h:88