Issue :: serialization of Class with Non Default Constructor.

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::eek: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
 
V

Victor Bazarov

I am Facing problem while serialization of Class with Non Default
Constructor.

///////////////////////////////////////////////////////////////////////
[... code snipped ...]
////////////////////////////////////////////////////////////////////////

Our sympathies. Can you maybe actually *describe* the problem? Our
crystal ball is kind of murky today...

V
 
P

Pallav singh

On 9/1/2011 7:56 AM, Pallav singh wrote:> I am Facing problem while serialization of  Class with Non Default
Constructor.
///////////////////////////////////////////////////////////////////////

 > [... code snipped ...]
////////////////////////////////////////////////////////////////////////

Our sympathies.  Can you maybe actually *describe* the problem?  Our
crystal ball is kind of murky today...

V

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++

HI ,

we need to serialize polymorphic class Hierarchy which donot have
default constructor.
Hence i was trying to program and find how this can be done.

Thanks
Pallav Singh
 
V

Victor Bazarov

On 9/1/2011 7:56 AM, Pallav singh wrote:> I am Facing problem while serialization of Class with Non Default
Constructor.
///////////////////////////////////////////////////////////////////////

[... code snipped ...]
////////////////////////////////////////////////////////////////////////

Our sympathies. Can you maybe actually *describe* the problem? Our
crystal ball is kind of murky today...

V

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++

HI ,

we need to serialize polymorphic class Hierarchy which donot have
default constructor.

So, serialize it. Write code.
Hence i was trying to program and find how this can be done.

You were *trying* to program? What is this, kindergarten?

This is not a problem, it's a programming task. What book on
serializing are you reading that doesn't explain different aspects of it
(including this one)?

V
 
J

Jorgen Grahn

On 9/1/2011 12:52 PM, Pallav singh wrote: ....

So, serialize it. Write code.


You were *trying* to program? What is this, kindergarten?

This guy's english is clearly not perfect. Frankly, you are being rude.
This is not a problem, it's a programming task. What book on
serializing are you reading that doesn't explain different aspects of it
(including this one)?

Suggesting books (which ones?) is fine, but surely I'm not required to
buy and read a book on subject Foo before asking questions about Foo
here?

As for the actual problem Mr Singh has, I can't say. IMHO, both
run-time polymorphism and general serialization are overused
techniques, and I very rarely have to deal with them.

/Jorgen
 
V

Victor Bazarov

[..]
Suggesting books (which ones?) is fine, but surely I'm not required to
buy and read a book on subject Foo before asking questions about Foo
here?

Posting about Foo here has some prerequisites, and while *buying* a book
is too much to ask sometimes, there are *free* books that one can
download, that I am sure cover Foo to some extend.

If the OP needs to know about basic programming techniques, the OP needs
to attend school or study some other way before asking questions about
those basic programming techniques here. A newsgroup should not be
viewed as a tool for general education. And there is also
'alt.comp.lang.learn.c-c++' where questions like that are better tolerated.

V
 
N

Noah Roberts

Hi

I am Facing problem while serialization of  Class with Non Default
Constructor.

///////////////////////////////////////////////////////////////////////
[snip large code chunk]

Please minimize the sample program and include what exact problem you
are running into. Immediate review of the upper most class in your
large chunk leads me to believe that though you're storing the data
twice, it should technically work. I'm not going to debug a novel
though, especially when I haven't been provided with a starting point.

Also note that technically speaking, Boost.Serialization is not
topical for the group. You may find better help in the boost user's
list, which can be accessed through email or usenet interfaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top