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>
10#include <boost/regex.hpp>
14#include <unordered_set>
16#include <unordered_map>
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);
99float med_stof(
const string& _Str);
100int med_stoi(
const string& _Str);
108#define MEDSERIALIZE_SUPPORT(Type) \
109namespace MedSerialize { \
110 template<> inline size_t get_size<Type>(Type &elem) { return elem.get_size(); } \
111 template<> inline size_t serialize<Type>(unsigned char *blob, Type &elem) { return elem.serialize(blob); } \
112 template<> inline size_t deserialize<Type>(unsigned char *blob, Type &elem) { return elem.deserialize(blob); } \
113 template<> inline string object_json<const Type>(const Type &elem) { return elem.object_json(); } \
122#define ADD_SERIALIZATION_FUNCS(...) \
123 virtual size_t get_size() { pre_serialization(); return MedSerialize::get_size_top(#__VA_ARGS__, __VA_ARGS__); } \
124 virtual size_t serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize_top(blob, #__VA_ARGS__, __VA_ARGS__); } \
125 virtual size_t deserialize(unsigned char *blob) { size_t size = MedSerialize::deserialize_top(blob, #__VA_ARGS__, __VA_ARGS__); post_deserialization(); return size;} \
126 virtual void serialized_fields_name(vector<string> &field_names) const { MedSerialize::get_list_names(#__VA_ARGS__, field_names); } \
127 virtual string object_json() const { return MedSerialize::object_json_start(my_class_name(), this->version(), #__VA_ARGS__, __VA_ARGS__); }
131#define ADD_SERIALIZATION_HEADERS() \
132 virtual size_t get_size(); \
133 virtual size_t serialize(unsigned char *blob); \
134 virtual size_t deserialize(unsigned char *blob); \
135 virtual void serialized_fields_name(vector<string> &field_names) const; \
136 virtual string object_json() const;
138#define ADD_SERIALIZATION_FUNCS_CPP(ClassName,...) \
139 size_t ClassName::get_size() { pre_serialization(); return MedSerialize::get_size_top(#__VA_ARGS__, __VA_ARGS__); } \
140 size_t ClassName::serialize(unsigned char *blob) { pre_serialization(); return MedSerialize::serialize_top(blob, #__VA_ARGS__, __VA_ARGS__); } \
141 size_t ClassName::deserialize(unsigned char *blob) { return MedSerialize::deserialize_top(blob, #__VA_ARGS__, __VA_ARGS__); post_deserialization();} \
142 void ClassName::serialized_fields_name(vector<string> &field_names) const { MedSerialize::get_list_names(#__VA_ARGS__, field_names); } \
143 string ClassName::object_json() const { return MedSerialize::object_json_start(my_class_name(), this->version(), #__VA_ARGS__, __VA_ARGS__); }
146#define ADD_CLASS_NAME(Type) string my_class_name() const {return string(#Type);}
148#define CONDITIONAL_NEW_CLASS(s,c) \
149 if (s == string(#c)) return new c;
152#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:121
virtual int write_to_file(const string &fname)
serialize model and write to file
Definition SerializableObject.cpp:92
virtual int read_from_file(const string &fname)
read and deserialize model
Definition SerializableObject.cpp:86
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:80
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