Menu Home

Boost Serialization : Use Non-Default Constructors

#include <fstream>
#include <cstring>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class gps_position {
public: 

	gps_position(int size) {
		std::cout << "constructor: one" << std::endl;
		m_size = size;
		buf = (unsigned char *)malloc(size);	
		memset(buf, 0, sizeof(buf));
	}

	gps_position(int d, int m, float s, int bufSZ) : degrees(d), minutes(m), seconds(s) {
		std::cout << "constructor: two" << std::endl;
		m_size = bufSZ;
		buf = (unsigned char *)malloc(m_size);	
		memset(buf, 0, sizeof(buf));

		buf[0] = 'a';
		buf[1] = 'b';
		buf[2] = 'c';
		buf[3] = '\0';
	}

	~gps_position() {
		if(buf) 
			free(buf);
	}

	void show() {
		std::cout << "degree = " << degrees << " minutes = " << minutes << " seconds = " << seconds
			<< " buf = " << buf << std::endl;
	}

private:
	friend class boost::serialization::access;
    template<class Archive> 
		friend inline  void load_construct_data(Archive &ar, gps_position *t, const unsigned int file_version);
	template<class Archive> 
		friend inline  void save_construct_data(Archive &ar, const gps_position *t, const unsigned int file_version);

	template<class Archive>
	void serialize(Archive &ar, const unsigned int version) {

		ar & degrees;
		ar & minutes;
		ar & seconds;

		for(int i = 0; i < m_size; i++) {
			ar & buf[i];
		}
	}

private:
	int degrees;
	int minutes;
	float seconds;
	
	unsigned char *buf;
	int m_size;
};

template<class Archive>
inline void save_construct_data(Archive &ar, const gps_position *t, const unsigned int file_version) {

	std::cout << "save_construct_data: size = " << t->m_size  << std::endl;
	ar << t->m_size;
}

template<class Archive>
	inline void load_construct_data(Archive &ar, gps_position *t, const unsigned int file_version) {
	int size;

	ar >> size;
	std::cout << "load_construct_data: size = "  << size << std::endl;
	::new(t)gps_position(size);			
}

int main() {
	std::ofstream ofs("file");
	gps_position* g = new gps_position(35, 59, 24.567f, 1024);

	std::cout << "before : ";
	g->show();
	std::cout <<  std::endl;

	{
		boost::archive::text_oarchive oa(ofs);
		oa << g;
	}

	gps_position *newg = NULL;	
	std::ifstream ifs("file");
	{
		boost::archive::text_iarchive ia(ifs);
		ia >> newg;
	} 
	std::cout << "after : ";
	newg->show(); 
	std::cout << std::endl;

	return 0;
}
g++ -Wall -g -o test test.cpp -lboost_serialization

output:

constructor: two
before serialization: degree = 35 minutes = 59 seconds = 24.567 buf = abc

save_construct_data: size = 1024
load_construct_data: size = 1024
constructor: one
after deserialization: degree = 35 minutes = 59 seconds = 24.567 buf = abc

ref: http://www.boost.org/doc/libs/1_47_0/libs/serialization/test/test_non_default_ctor.cpp

Categories: 未分类

Tagged as:

lnmcc

2 replies

  1. Thank you so much for this post! I simply couldn’t get this working after reading the docs and other examples, which never were as complete and concise as this one.
    You saved me another couple of hours of painful experimentation, thanks!
    Cheers

Leave a Reply

Your email address will not be published.