boost serialization: intrusive and non-intrusive

Posted cutepig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boost serialization: intrusive and non-intrusive相关的知识,希望对你有一定的参考价值。

boost serialization

it has 2 modes: intrusive and non-intrusive

intrusive mode

class gps_position
{
public:
	friend class boost::serialization::access;
	// When the class Archive corresponds to an output archive, the
	// & operator is defined similar to <<.  Likewise, when the class Archive
	// is a type of input archive the & operator is defined similar to >>.
	template<class Archive>
	void serialize(Archive & ar, const unsigned int version)
	{
		ar & degrees;
		ar & minutes;
		ar & seconds;
	}
	int degrees;
	int minutes;
	float seconds;
public:
	gps_position(){};
	gps_position(int d, int m, float s) :
	degrees(d), minutes(m), seconds(s)
	{}
};

non-intrusive mode


namespace boost {
	namespace serialization {

		template<class Archive>
		void serialize(Archive & ar, gps_position & g, const unsigned int version)
		{
			ar & g.degrees;
			ar & g.minutes;
			ar & g.seconds;
		}

	} // namespace serialization
} // namespace boost

How does boost impl the 2 modes?

A: By function overloading. by parameter specialization

For intrusive mode, following template functions are called

boost::serialization::access::serialize<boost::archive::text_oarchive,gps_position>(boost::archive::text_oarchive & ar, gps_position & t, const unsigned int file_version);

Note non intrusive mode serialization‘s priority is always higher than intrusive mode as it is implemented in boost::serialization name space. and it is more "specific"

以上是关于boost serialization: intrusive and non-intrusive的主要内容,如果未能解决你的问题,请参考以下文章

如何为 boost::serialization 指定一个 nvp 包装器?

对 boost::serialization 的未定义引用

来自boost / serialization / vector #include的链接器错误

Boost.Serialization 在不同平台上的序列化是不是不同?

如何让 boost::serialization 工作?

boost::serialization::traits 的具体例子?