问题
I have a MyFile.hpp header file which contains various types and enums. How do i do serialization/ desrialization of given example code.
//MyFile.hpp
namespace A {
namespace B {
typedef std::string MyString;
typedef std::map<std::string,std::string> my_type;
typedef bool result;
struct MyTimer
{
int time;
private :
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & time;
}
};
enum MODE
{
Sleep=1,
HybridSleep,
Hybernate
}
}
}
I need to do implementation in corresponding MyFile.cpp but don't know how do i go ahead.
Thanks,
回答1:
Maps, strings etc. can just be serialized by including the relevant header:
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>
The enum counts as a primitive type:
A type T is Serializable if and only if one of the following is true:
it is a primitive type.
By primitive type we mean a C++ built-in type and ONLY a C++ built-in type. Arithmetic (including characters), bool, enum are primitive types. Below in serialization traits, we define a "primitive" implementation level in a different way for a different purpose. This can be a source of confusion.
- It is a class type and one of the following has been declared according to the prototypes detailed below:
- a class member function serialize
- a global function serialize
- it is a pointer to a Serializable type.
- it is a reference to a Serializable type.
- it is a native C++ Array of Serializable type.
For more tricky cases there is BOOST_STRONG_TYPEDEF
(see documentation "Serialization Wrappers")
来源:https://stackoverflow.com/questions/22533863/boost-serialization-of-native-type-defined-with-typedef-contained-within-struct