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>
22#define LOCAL_SECTION LOG_INFRA
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__)
52 virtual void pre_serialization() {};
53 virtual void post_deserialization() {};
57 virtual size_t serialize(
unsigned char *blob) {
return 0; }
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); }
79 int init_params_from_file(
string init_file);
80 int init_param_from_file(
string file_str,
string ¶m);
81 virtual int init(map<string, string>& map) {
return 0; }
83 int update_from_string(
const string &init_string);
84 virtual int update(map<string, string>& map) {
return init(map); }
86 virtual string object_json()
const;
88 void _read_from_file(
const string &fname,
bool throw_on_version_error);
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);
99inline float med_stof(
const string &_Str)
107 HMTHROW_AND_ERR(
"invalid stof argument [%s]\n", _Str.c_str());
111inline int med_stoi(
const string &_Str)
119 HMTHROW_AND_ERR(
"invalid stoi argument [%s]\n", _Str.c_str());
123inline long long med_stoll(
const string &_Str)
131 HMTHROW_AND_ERR(
"invalid stoll argument [%s]\n", _Str.c_str());
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(); } \
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__); }
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;
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__); }
179#define ADD_CLASS_NAME(Type) string my_class_name() const {return string(#Type);}
181#define CONDITIONAL_NEW_CLASS(s,c) \
182 if (s == string(#c)) return new c;
185#include "SerializableObject_imp.h"
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