You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Developer/Serialization.md
+27-19Lines changed: 27 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
This guide explains how to implement serialization using the Cereal library within Graphitti. If you're looking to add serialization to your class, follow this guide. For more comprehensive information on Cereal, refer to their [official documentation](https://uscilab.github.io/cereal/index.html).
4
4
5
5
<strong>
6
-
Understanding Serialization and Deserialization
6
+
What is Serialization and Deserialization?
7
7
</strong>
8
8
9
9
Serialization involves converting an object or data structure into a format that can be stored or transmitted, while deserialization is the reverse process— reconstructing an object from the serialized format.
@@ -21,7 +21,7 @@ Cereal is a lightweight C++11 library designed for object serialization and dese
21
21
Why Graphitti Uses Serialization?
22
22
</strong>
23
23
24
-
Graphitti utilizes Cereal to enable efficient serialization and deserialization of the network structure. Serialized data can serve as a checkpoint for large simulations or as input for subsequent simulations with varying conditions. This flexibility enhances Graphitti's efficiency and adaptability in modeling scenarios.
24
+
Graphitti utilizes Cereal to enable efficient serialization and deserialization of its simulation state and network structure. Serialized data can serve as a checkpoint for large simulations or as input for subsequent simulations with varying conditions. This flexibility enhances Graphitti's efficiency and adaptability in modeling scenarios.
25
25
<br>
26
26
27
27
## Understand Cereal at a High Level
@@ -106,7 +106,7 @@ Before implementing serialization in your class, you need to include the appropr
106
106
107
107
Within your class header file
108
108
- Firstly, declare the `serialize` function inside the class:
109
-
- Add the following template function signature in the public section of your class to allow serialization for any archive type.
109
+
- Add the following template function signature in the **public** section of your class to allow serialization for any archive type.
110
110
```cpp
111
111
template <classArchive>
112
112
voidserialize(Archive & archive);
@@ -139,26 +139,34 @@ class MyCoolClass
139
139
140
140
private:
141
141
std::vector<int> myVector_;
142
-
int X_;
142
+
int x_;
143
143
};
144
144
145
145
//STEP 02 (b): Define the serialize function outside the class
Adjust the function names and data member names as per your specific requirements.
157
157
158
-
NOTE:
159
-
- The `template <class Archive>` declaration allows the function to work with any type of Cereal archive (e.g., JSON, XML, binary).
160
-
- When defining the `serialize` function, use Cereal’s `make_nvp()` for custom names or `CEREAL_NVP()` for automatic name-value pair serialization.
161
-
- Defining the function outside the class maintains a consistent style for serialized code and makes the function easier to locate, especially in larger projects like Graphitti. This approach also keeps your class declaration cleaner and less cluttered.
158
+
NOTE:
159
+
160
+
- The `template <class Archive>` declaration allows the serialize function to be flexible, enabling it to work with different types of Cereal archives, such as JSON, XML, or binary formats.
161
+
162
+
- When defining the `serialize` function, use `make_nvp()` and `CEREAL_NVP()` for each member variable:
163
+
- `make_nvp()` is used when you want to assign custom names to your serialized member variables, which can be helpful for clarity in the serialized output.
164
+
- `CEREAL_NVP()` automatically uses the variable's name for serialization without the need to explicitly name it.
165
+
166
+
- Why define `serialize` in the header?
167
+
- Cereal relies heavily on templates, and C++ templates require full implementation details to be available during compilation for proper instantiation. Since templates must be instantiated at compile time, placing the serialize function in a `.cpp` file could result in missing template information, leading to linker errors. By defining the function in the header file, the compiler has all the necessary information to properly instantiate the serialize function for various data types.
168
+
169
+
- Defining the function outside the class (but still in the header) promotes a clean code style, making the class declaration less cluttered and easier to maintain. It also makes serialized code easier to find, which is especially important in larger projects like Graphitti.
162
170
163
171
### **STEP 03: SPECIAL CASES**
164
172
@@ -175,7 +183,7 @@ If your derived class uses virtual inheritance (`class Derived : virtual Base`),
175
183
176
184
class MyDerived : virtual MyBase
177
185
{
178
-
int X_;
186
+
int x_;
179
187
template <class Archive>
180
188
void serialize( Archive & ar );
181
189
};
@@ -184,7 +192,7 @@ template <class Archive>
184
192
void MyDerived::serialize( Archive & archive )
185
193
{
186
194
// We pass this cast to the base type for each base type we need to serialize.
0 commit comments