-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
130 lines (101 loc) · 4.3 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <vector>
#include <fstream>
#include <persistence/persistence.hpp>
#include <persistence/stl/vector.hpp>
#include <persistence/tools/uninitialized.hpp>
/// \brief a class we want to be serialized / deserialized
struct my_struct
{
int s_int;
std::vector<int> s_vector;
};
// the serialization meta-data
namespace neam
{
namespace cr
{
// This is a metadata for persistence. This construct will tell persistence what members
// to serialize and deserialize.
//
// If some of those members are private, in your class put a
// `friend neam::cr::persistence;`.
//
// If you don't want to serialize all of the members of the class, you'll have to use
// a post deserialization callback that will call the constructors of the members
// The "simple" sample shows how to deal with that.
//
// NOTE: You can totaly generate "generic" serialization metadata for template classes.
// The "simple" sample shows how to deal with that.
template<typename Backend>
class persistence::serializable<Backend, my_struct> : public persistence::serializable_object
<
Backend, // < the backend (here: all backends)
my_struct, // < the class type to handle
// simply list here the members you want to serialize / deserialize
//
// Some backends (like json or verbose) will ask a name for the property.
// The "simple" sample shows how to deal with that.
NCRP_TYPED_OFFSET(my_struct, s_int),
NCRP_TYPED_OFFSET(my_struct, s_vector)
> {};
} // namespace cr
} // namespace neam
int main()
{
// This is the instance we want to serialize and deserialize
my_struct my_instance;
my_instance.s_int = 24;
my_instance.s_vector.resize(10, 0x5A5A5A5A);
// We serialize my_instance. This will create a neam::cr::raw_data,
// that is the result of the serialization (it contains a data and a size fields).
neam::cr::raw_data rd = neam::cr::persistence::serialize<neam::cr::persistence_backend::neam>(my_instance);
{
// We create a non-initialized instance of my_struct.
// If we have it already initialized (if the constructor has been called),
// we would have to call the destructor to avoid memory leaks.
neam::cr::uninitialized<my_struct> my_copy_instance;
// This will deserialize 'in place' the content of rd
neam::cr::persistence::deserialize<neam::cr::persistence_backend::neam>(rd, &my_copy_instance);
// As we have now an initialized instance, we have to tell the uninitialized<> object
// to call the destructor when its life ends.
my_copy_instance.call_destructor(true);
// We can do whatever we want on the object now:
my_copy_instance->s_int += 1;
my_copy_instance->s_vector.reserve(10);
// ...
}
{
// This will not deserialize over an existing object, but instead will allocate a new one.
// (notice the my_struct as template parameter)
my_struct *my_copy_ptr = neam::cr::persistence::deserialize<neam::cr::persistence_backend::neam, my_struct>(rd);
// We can do whatever we want on the object now:
my_copy_ptr->s_int += 1;
my_copy_ptr->s_vector.reserve(10);
// ...
// Don't forget to delete the object (or use a shared/unique ptr):
delete my_copy_ptr;
}
{
// If you plan to write the data to a file/stream/..., this is how it could be done:
std::ofstream file("./temp-file.tmp", std::ios::binary);
file.write(reinterpret_cast<char *>(rd.data), rd.size);
}
{
// If you want to load the data from a file/stream/..., this is how it could be done:
std::ifstream file("./temp-file.tmp", std::ios::binary | std::ios::ate);
// get the size of the file & allocate the memory to hold the data
size_t size = file.tellg();
file.seekg(0, std::ios::beg);
int8_t *data = reinterpret_cast<int8_t *>(operator new(size));
// read the file
file.read(reinterpret_cast<char *>(data), size);
// create the raw_data object. Note the `neam::assume_ownership`
// that tells the raw_data to free the memory at the end of its life
neam::cr::raw_data rd_from_file(size, data, neam::assume_ownership);
// We can now deserialize it:
my_struct *my_copy_ptr = neam::cr::persistence::deserialize<neam::cr::persistence_backend::neam, my_struct>(rd_from_file);
delete my_copy_ptr;
}
return 0;
}