P
Pallav singh
Hi
I am Facing problem while serialization of Class with Non Default
Constructor.
///////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
/* Header file to Include FileStreams */
#include <fstream>
#include <boost/serialization/serialization.hpp>
/* Header file to Include Archieving Files */
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
/* Derived classes should include serializations of their base classes
*/
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
////////////////////////////////////////////////////////////////////////
class gps_position
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
template<class Archive>
inline void save_construct_data(Archive & ar, const gps_position *
t, const unsigned int file_version) {
// save data required to construct instance
ar << t->degrees;
ar << t->minutes;
ar << t->seconds;
}
// load data required for construction and invoke constructor in
place
template<class Archive>
inline void load_construct_data (Archive & ar, gps_position * t,
const unsigned int file_version) {
// default just uses the default constructor to initialize
// previously allocated memory.
int degrees;
int minutes;
float seconds;
ar >> degrees;
ar >> minutes;
ar >> seconds;
::new(t)gps_position(degrees, minutes, seconds);
}
int degrees;
int minutes;
float seconds;
public:
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};
////////////////////////////////////////////////////////////////////////
class bus_stop
{
/* to proivide ACCESS TO private member variable of class */
friend class boost::serialization::access;
/* Always keep Serilization funtions in Private part of class
Else
* Object Tracking will be creating Problem for Polymorphic class
object
*/
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & latitude;
ar & longitude;
}
template<class Archive>
inline void save_construct_data(Archive & ar, const bus_stop * t,
const unsigned int file_version) {
// save data required to construct instance
ar << t->latitude;
ar << t->longitude;
}
// load data required for construction and invoke constructor in
place
template<class Archive>
inline void load_construct_data (Archive & ar, bus_stop * t, const
unsigned int file_version){
// default just uses the default constructor to initialize
// previously allocated memory.
gps_position latitude;
gps_position longitude;
ar >> t->latitude;
ar >> t->longitude;
::new(t)bus_stop( latitude, longitude );
}
gps_position latitude;
gps_position longitude;
protected:
bus_stop(const gps_position & lat_, const gps_position & long_) :
latitude(lat_), longitude(long_)
{}
public:
virtual ~bus_stop(){}
};
////////////////////////////////////////////////////////////////////////////
class bus_stop_corner : public bus_stop
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
ar & boost::serialization::base_object<bus_stop>(*this);
ar & street1;
ar & street2;
}
template<class Archive>
inline void save_construct_data(Archive & ar, const
bus_stop_corner * t, const unsigned int file_version) {
// save data required to construct instance
// serialize base class information
ar & boost::serialization::base_object<bus_stop>(*this);
ar << t->street1 ;
ar << t->street2 ;
}
// load data required for construction and invoke constructor in
place
template<class Archive>
inline void load_construct_data (Archive & ar, bus_stop_corner *
t, const unsigned int file_version){
// default just uses the default constructor to initialize
// previously allocated memory.
gps_position latitude;
gps_position longitude;
std::string street1;
std::string street2;
ar >> t->latitude;
ar >> t->longitude;
ar >> t->street1;
ar >> t->street2;
::new(t) bus_stop_corner (latitude, longitude, street1, street2);
}
std::string street1;
std::string street2;
virtual std::string description() const
{
return street1 + " and " + street2;
}
public:
bus_stop_corner(const gps_position & lat_, const gps_position &
long_,
const std::string & s1_, const std::string &
s2_ ) :
bus_stop(lat_, long_), street1(s1_), street2(s2_)
{}
};
//////////////////////////////////////////////////////////////////////////
int main() {
std:fstream ofs("msg_file_store");
gps_position position_1 (10, 10, 10.567f);
gps_position position_2 (20, 20, 20.567f);
std::string s1("New Delhi");
std::string s2("Mumbai");
bus_stop_corner bus_stop_corner_1( position_1, position_2, s1 ,
s2);
{
boost::archive::text_oarchive oa(ofs);
oa << bus_stop_corner_1;
}
// Create a Dummy Object Location where we need to retieve Data
bus_stop_corner new_bus_stop_corner_2();
{
std::ifstream ifs("msg_file_store");
boost::archive::text_iarchive ia(ifs);
ia >> new_bus_stop_corner_2;
}
return 0;
}
////////////////////////////////////////////////////////////////////////
Thanks
Pallav Singh
I am Facing problem while serialization of Class with Non Default
Constructor.
///////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <sstream>
/* Header file to Include FileStreams */
#include <fstream>
#include <boost/serialization/serialization.hpp>
/* Header file to Include Archieving Files */
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
/* Derived classes should include serializations of their base classes
*/
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
////////////////////////////////////////////////////////////////////////
class gps_position
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
template<class Archive>
inline void save_construct_data(Archive & ar, const gps_position *
t, const unsigned int file_version) {
// save data required to construct instance
ar << t->degrees;
ar << t->minutes;
ar << t->seconds;
}
// load data required for construction and invoke constructor in
place
template<class Archive>
inline void load_construct_data (Archive & ar, gps_position * t,
const unsigned int file_version) {
// default just uses the default constructor to initialize
// previously allocated memory.
int degrees;
int minutes;
float seconds;
ar >> degrees;
ar >> minutes;
ar >> seconds;
::new(t)gps_position(degrees, minutes, seconds);
}
int degrees;
int minutes;
float seconds;
public:
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};
////////////////////////////////////////////////////////////////////////
class bus_stop
{
/* to proivide ACCESS TO private member variable of class */
friend class boost::serialization::access;
/* Always keep Serilization funtions in Private part of class
Else
* Object Tracking will be creating Problem for Polymorphic class
object
*/
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & latitude;
ar & longitude;
}
template<class Archive>
inline void save_construct_data(Archive & ar, const bus_stop * t,
const unsigned int file_version) {
// save data required to construct instance
ar << t->latitude;
ar << t->longitude;
}
// load data required for construction and invoke constructor in
place
template<class Archive>
inline void load_construct_data (Archive & ar, bus_stop * t, const
unsigned int file_version){
// default just uses the default constructor to initialize
// previously allocated memory.
gps_position latitude;
gps_position longitude;
ar >> t->latitude;
ar >> t->longitude;
::new(t)bus_stop( latitude, longitude );
}
gps_position latitude;
gps_position longitude;
protected:
bus_stop(const gps_position & lat_, const gps_position & long_) :
latitude(lat_), longitude(long_)
{}
public:
virtual ~bus_stop(){}
};
////////////////////////////////////////////////////////////////////////////
class bus_stop_corner : public bus_stop
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
ar & boost::serialization::base_object<bus_stop>(*this);
ar & street1;
ar & street2;
}
template<class Archive>
inline void save_construct_data(Archive & ar, const
bus_stop_corner * t, const unsigned int file_version) {
// save data required to construct instance
// serialize base class information
ar & boost::serialization::base_object<bus_stop>(*this);
ar << t->street1 ;
ar << t->street2 ;
}
// load data required for construction and invoke constructor in
place
template<class Archive>
inline void load_construct_data (Archive & ar, bus_stop_corner *
t, const unsigned int file_version){
// default just uses the default constructor to initialize
// previously allocated memory.
gps_position latitude;
gps_position longitude;
std::string street1;
std::string street2;
ar >> t->latitude;
ar >> t->longitude;
ar >> t->street1;
ar >> t->street2;
::new(t) bus_stop_corner (latitude, longitude, street1, street2);
}
std::string street1;
std::string street2;
virtual std::string description() const
{
return street1 + " and " + street2;
}
public:
bus_stop_corner(const gps_position & lat_, const gps_position &
long_,
const std::string & s1_, const std::string &
s2_ ) :
bus_stop(lat_, long_), street1(s1_), street2(s2_)
{}
};
//////////////////////////////////////////////////////////////////////////
int main() {
std:fstream ofs("msg_file_store");
gps_position position_1 (10, 10, 10.567f);
gps_position position_2 (20, 20, 20.567f);
std::string s1("New Delhi");
std::string s2("Mumbai");
bus_stop_corner bus_stop_corner_1( position_1, position_2, s1 ,
s2);
{
boost::archive::text_oarchive oa(ofs);
oa << bus_stop_corner_1;
}
// Create a Dummy Object Location where we need to retieve Data
bus_stop_corner new_bus_stop_corner_2();
{
std::ifstream ifs("msg_file_store");
boost::archive::text_iarchive ia(ifs);
ia >> new_bus_stop_corner_2;
}
return 0;
}
////////////////////////////////////////////////////////////////////////
Thanks
Pallav Singh