Skip to content

Commit b6de0d4

Browse files
author
Divya Kamath
committed
Addressed comments
1 parent dacab8b commit b6de0d4

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

docs/Developer/Serialization.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
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).
44

55
<strong>
6-
Understanding Serialization and Deserialization
6+
What is Serialization and Deserialization?
77
</strong>
88

99
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
2121
Why Graphitti Uses Serialization?
2222
</strong>
2323

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.
2525
<br>
2626

2727
## Understand Cereal at a High Level
@@ -106,7 +106,7 @@ Before implementing serialization in your class, you need to include the appropr
106106

107107
Within your class header file
108108
- 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.
110110
```cpp
111111
template <class Archive>
112112
void serialize(Archive & archive);
@@ -139,26 +139,34 @@ class MyCoolClass
139139

140140
private:
141141
std::vector<int> myVector_;
142-
int X_;
142+
int x_;
143143
};
144144

145145
//STEP 02 (b): Define the serialize function outside the class
146146

147147
template <class Archive>
148148
void MyCoolClass::serialize(Archive &archive)
149149
{
150-
archive(cereal::make_nvp("myVector", myVector_), cereal::make_nvp("myInt", X_));
150+
archive(cereal::make_nvp("myVector", myVector_), cereal::make_nvp("myInt", x_));
151151
}
152152

153153

154154
```
155155
156156
Adjust the function names and data member names as per your specific requirements.
157157
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.
162170
163171
### **STEP 03: SPECIAL CASES**
164172
@@ -175,7 +183,7 @@ If your derived class uses virtual inheritance (`class Derived : virtual Base`),
175183
176184
class MyDerived : virtual MyBase
177185
{
178-
int X_;
186+
int x_;
179187
template <class Archive>
180188
void serialize( Archive & ar );
181189
};
@@ -184,7 +192,7 @@ template <class Archive>
184192
void MyDerived::serialize( Archive & archive )
185193
{
186194
// We pass this cast to the base type for each base type we need to serialize.
187-
archive(cereal::virtual_base_class<MyBase>(this), cereal::make_nvp("myInt", X_));
195+
archive(cereal::virtual_base_class<MyBase>(this), cereal::make_nvp("myInt", x_));
188196
189197
// For multiple inheritance, link all the base classes one after the other
190198
//archive(cereal::virtual_base_class<MyBase1>(this), cereal::virtual_base_class<MyBase2>(this), cereal::make_nvp("myInt", X_));
@@ -200,7 +208,7 @@ For non-virtual inheritance (`class Derived : public Base`), use `cereal::base_c
200208

201209
class MyDerived : public MyBase
202210
{
203-
int X_;
211+
int x_;
204212
template <class Archive>
205213
void serialize( Archive & ar );
206214
};
@@ -209,7 +217,7 @@ template <class Archive>
209217
void MyDerived::serialize( Archive & archive )
210218
{
211219
// We pass this cast to the base type for each base type we need to serialize.
212-
archive(cereal::base_class<MyBase>(this), cereal::make_nvp("myInt", X_));
220+
archive(cereal::base_class<MyBase>(this), cereal::make_nvp("myInt", x_));
213221

214222
// For multiple inheritance, link all the base classes one after the other
215223
//archive(cereal::base_class<MyBase1>(this), cereal::base_class<MyBase2>(this), cereal::make_nvp("myInt", X_));
@@ -243,7 +251,7 @@ Register each derived class above the definition of the `serialize` function usi
243251
244252
class MyDerived : public MyBase
245253
{
246-
int y;
254+
int x_;
247255
template <class Archive>
248256
void serialize( Archive & ar );
249257
};
@@ -254,7 +262,7 @@ CEREAL_REGISTER_TYPE(MyDerived);
254262
template <class Archive>
255263
void MyDerived::serialize( Archive & archive )
256264
{
257-
archive(cereal::base_class<MyBase>(this), y);
265+
archive(cereal::base_class<MyBase>(this), cereal::make_nvp("myInt", x_));
258266
}
259267
```
260268

@@ -271,7 +279,7 @@ struct MyEmptyBase
271279
struct MyDerived: MyEmptyBase
272280
{
273281
void foo() {}
274-
double y;
282+
double y_;
275283
template <class Archive>
276284
void serialize( Archive & archive );
277285
};
@@ -284,7 +292,7 @@ CEREAL_REGISTER_POLYMORPHIC_RELATION(MyEmptyBase, MyDerived)
284292
template <class Archive>
285293
void MyDerived::serialize( Archive & archive )
286294
{
287-
archive( y );
295+
archive( cereal::make_nvp("myDouble", y_) );
288296
}
289297
```
290298
@@ -316,13 +324,13 @@ struct BaseClass
316324
template <typename T>
317325
struct DerivedClassTemplate : public BaseClass
318326
{
319-
T value;
327+
T value_;
320328
void sayType();
321329
322330
template <class Archive>
323331
void serialize(Archive & archive)
324332
{
325-
archive(cereal::virtual_base_class<MyEmptyBase>(this), value);
333+
archive(cereal::virtual_base_class<MyEmptyBase>(this), cereal::make_nvp("myValue", value_));
326334
}
327335
};
328336

0 commit comments

Comments
 (0)