1#ifndef _SERIALIZABLE_OBJECT_LIB_H_
2#define _SERIALIZABLE_OBJECT_LIB_H_
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>
13#include <unordered_set>
15#include <unordered_map>
23#define LOCAL_SECTION LOG_INFRA
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__)
53 virtual void pre_serialization() {};
54 virtual void post_deserialization() {};
58 virtual size_t serialize(
unsigned char *blob) {
return 0; }
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); }
80 int init_params_from_file(
string init_file);
81 int init_param_from_file(
string file_str,
string ¶m);
82 virtual int init(map<string, string>& map) {
return 0; }
84 int update_from_string(
const string &init_string);
85 virtual int update(map<string, string>& map) {
return init(map); }
87 virtual string object_json()
const;
89 void _read_from_file(
const string &fname,
bool throw_on_version_error);
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);
100inline float med_stof(
const string &_Str)
108 HMTHROW_AND_ERR(
"invalid stof argument [%s]\n", _Str.c_str());
112inline int med_stoi(
const string &_Str)
120 HMTHROW_AND_ERR(
"invalid stoi argument [%s]\n", _Str.c_str());
124inline long long med_stoll(
const string &_Str)
132 HMTHROW_AND_ERR(
"invalid stoll argument [%s]\n", _Str.c_str());
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(); } \
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__); }
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;
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__); }
180#define ADD_CLASS_NAME(Type) string my_class_name() const {return string(#Type);}
182#define CONDITIONAL_NEW_CLASS(s,c) \
183 if (s == string(#c)) return new c;
186#include "SerializableObject_imp.h"
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