Advertisement

How to trick boost::serialize into returning a base member from a derived class using the base class

Started by February 16, 2015 05:58 AM
-1 comments, last by ankhd 9 years, 10 months ago

Hi all.

if I have a base class


class base
{
   int Type;

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 & Type; 
	
    }

}

derived class

.


class Foo: public base{
   int other;

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)
    {
		// invoke serialization of the base class 
		ar & boost::serialization::base_object<base>(*this);
		// save/load class member variables
		ar & other;
    }

class that holds serialized data.

.


class Serialize
{
std::string archive_stream;
	//----------------------------------------------------------------------
	//
	//-----------------------------------------------------------------------
	template <typename T>
	bool SerializeMessage(const T& t)
	{
		std::ostringstream  outstream;
		
		try
		{
			// Serialize the data
			boost::archive::text_oarchive archive(outstream);
			archive << t;
			archive_stream = outstream.str();
		}
		catch (std::exception& e)
		{
			return false;
		}

		if(archive_stream.empty())
			return false;

		return true;
	}//end SerializeMessage
	////////////////////////////////////////////////////////////////////////


	//----------------------------------------------------------------------
	//
	//-----------------------------------------------------------------------
	template <typename T>
	bool DeSerializeMessage(T& t)
	{
		if(archive_stream.empty())
			return false;

		try
		{
			//DeSerialize the data 
			std::istringstream archivestream(archive_stream);

			boost::archive::text_iarchive archive(archivestream);
			archive >> t;
		}
		catch (std::exception& e)
		{
			return false;
		}
		
		return true;
	}//end DeSerializeMessage
	////////////////////////////////////////////////////////////////////////

	//-------------------------------------------------------------------
	//returns the type member
	//--------------------------------------------------------------------
	bool GetType(Base &type)
	{
		
		bool v = DeSerializeMessage(type);

		
		return v;
	}//end GetType
	//////////////////////////////////////////////////////////////////////

Now if I use

Foo foo;

serialize.Serialize(foo);

then if I call

serialize.GetType(base);

it comes out as zero.

I can do it if I create a empty derived class

.


class Type: public Base

{

private:

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)

{

// invoke serialization of the base class

ar & boost::serialization::base_object<base>(*this);

}

public

:

Type()

{

Type = -1;

//message type

}

//end

~Type()

{

}

};

then GetType looks Like this now

.


//-------------------------------------------------------------------
//returns the type member
//--------------------------------------------------------------------
bool GetType(base &type)
{
   cType Type;
   bool v = DeSerializeMessage(Type);

   type.Type = Type.Type;
   return v;
}//end GetType
//////////////////////////////////////////////////////////////////////

Is there a way to do it with out the derived type class

This topic is closed to new replies.

Advertisement