[ create a new paste ] login | about

Link: http://codepad.org/6ETp1Ac0    [ raw code | fork ]

C++, pasted on Aug 5:
#include	<iostream>
#include	<fstream>

#include	<boost/archive/binary_oarchive.hpp>
#include	<boost/archive/binary_iarchive.hpp>
#include	<boost/serialization/base_object.hpp>
#include	<boost/serialization/string.hpp>
#include	<boost/serialization/export.hpp>
#include	<boost/noncopyable.hpp>

using namespace std;

//////////////////////////////////////////////////// B.hpp

class B : private boost::noncopyable
{
public:
	virtual void print();
	void set( string str );
	virtual string name();
	virtual void archv();

protected:
	string s;

private:
	friend class boost::serialization::access;

	template<typename Archive>
	void serialize( Archive &ar, const unsigned int /* version */ )
	{
		ar & s;
	}
};

BOOST_SERIALIZATION_ASSUME_ABSTRACT( B )

///////////////////////////////////////////////////// B.cpp

void B::print() { cout << s << endl; }
void B::set( string str ) { s = str; }
string B::name() { return string("B"); }
void B::archv()
{
	ofstream file((string("test")+name()+".db").c_str(), ios::binary);
	boost::archive::binary_oarchive oa(file);
	oa << (*this);
}

////////////////////////////////////////////////////// D.hpp

class D : public B
{
public:
	void print();
	void set( string str1, string str2 );
	string name();

protected:
	string t;

private:
	friend class boost::serialization::access;

	template<typename Archive>
	void serialize( Archive &ar, const unsigned int /* version */ )
	{
		ar & boost::serialization::base_object<B>(*this);
		ar & t;
	}
};

BOOST_CLASS_EXPORT( D )

///////////////////////////////////////////////////// D.cpp

void D::print() { B::print(); cout << t << endl; }
void D::set( string str1, string str2 ) { s = str1; t = str2; }
string D::name() { return string("D"); }

///////////////////////////////////////////////////

int main ( int argc, char *argv[] )
{
	B b; D d;
	b.set(string("HEY"));
	d.set(string("HO"), string("Let's Go!"));
	b.archv(); d.archv();

	B b_; D d_;
	ifstream file1( "testB.db", ios::binary );
	boost::archive::binary_iarchive ia1( file1 );
	ia1 >> b_;

	ifstream file2( "testD.db", ios::binary );
	boost::archive::binary_iarchive ia2( file2 );
	ia2 >> d_;

	b_.print(); d_.print();

	return 0;
}  // ----------  end of function main  ----------


Create a new paste based on this one


Comments: