Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-12-01 13:22:10


I made a couple of changes to your program and compiled it with
both vc 7.1 and borland 5.64 on the current version of the library in the
HEAD.
Here is a summary of the changes.

a) I reordered the headers to conform with a header order requirement
of 1.33 - 1.35 no longer has this requirement.

b) I elminated headers which are not part of the "public" api and
as such are automatically included when necessary.

c) I moved "#include <boost/serialization/extended_type_info_no_rtti.hpp>"
to be the first in the list of headers. The default extended_type_info
system is the first one seen. If none is explicitly specified, then the
default extended_type_info_rtti would be used. This will make the
no_rtti ( which should be renamed to something like "by_name") the
default type_info system.

d) I replaced BOOST_CLASS_EXPORT with BOOST_CLASS_EXPORT_GUID
so that I wouldn't have to include <, >, and : characters in the class
export name.

(note this revealed a small error in the newest copy of export.hpp which I
have
fixed on my machine but haven't tested and uploaded yet.

I'm not sure if this helps, but that's what I did.

Robert Ramey

#include <boost/serialization/extended_type_info_no_rtti.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/type_info_implementation.hpp>

// includes for xml serialization
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include<iostream>
#include<fstream>
#include <vector>

#include <boost/serialization/vector.hpp>

class BaseClass
{
protected:
 friend class boost::serialization::access;

        template<class Archive>
 void serialize(Archive& ar, const unsigned int version)
 {
                ar & BOOST_SERIALIZATION_NVP(m_degrees);
  ar & BOOST_SERIALIZATION_NVP(m_minutes);
  ar & BOOST_SERIALIZATION_NVP(m_seconds);
        }

        int m_degrees;
 int m_minutes;
 float m_seconds;

public:
        BaseClass()
        {
                m_degrees = 0;
                m_minutes = 0;
                m_seconds = 0.0;
        }

 BaseClass(int degrees, int minutes, float seconds)
        {
                m_degrees = degrees;
                m_minutes = minutes;
                m_seconds = seconds;
        }

 virtual void display() const
        {
                std::cout << "------------------------------------" <<
std::endl;
                std::cout << "m_degrees= " << m_degrees << std::endl;
                std::cout << "m_minutes= " << m_minutes << std::endl;
                std::cout << "m_seconds= " << m_seconds << std::endl;
                std::cout << "------------------------------------" <<
std::endl;
        }
};

BOOST_CLASS_TYPE_INFO(
    BaseClass,
    extended_type_info_no_rtti<BaseClass>
)

BOOST_CLASS_EXPORT(BaseClass)

class DerivedClass1: public BaseClass
{
protected:
 friend class boost::serialization::access;

        template<class Archive>
 void serialize(Archive& ar, const unsigned int version)
 {
                ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseClass);
                // ar & BOOST_SERIALIZATION_NVP(m_vector);
        }

        mutable std::vector<int> m_vector;

public:
        DerivedClass1(): BaseClass()
        {
        }

 DerivedClass1(int degrees, int minutes, float seconds): BaseClass(degrees,
minutes, seconds)
        {
        }

        void push_back(int arg) const
        {
                m_vector.push_back(arg);
        }

 virtual void display() const
        {
                std::cout <<
"***********************************************" << std::endl;
                BaseClass::display();
                for (std::vector<int>::const_iterator iter=m_vector.begin();
iter!=m_vector.end(); iter++)
                {
                       std::cout << " value= " << *iter << std::endl;
                }
                std::cout <<
"***********************************************" << std::endl;
        }
};

BOOST_CLASS_TYPE_INFO(
    DerivedClass1,
    extended_type_info_no_rtti<DerivedClass1>
)

BOOST_CLASS_EXPORT(DerivedClass1)

BOOST_CLASS_TYPE_INFO(
    std::vector<double>,
    extended_type_info_no_rtti<std::vector<double> >
)

BOOST_CLASS_EXPORT_GUID(std::vector<double>, "std_vector_double")

int main()
{
       const DerivedClass1 g(35, 59, 24.567f);

        g.push_back(10);
        g.push_back(20);
        g.push_back(30);

        g.display();

        {
                std::ofstream ofs("filename");
                boost::archive::xml_oarchive oa(ofs);
                oa << boost::serialization::make_nvp("g1", g);
        }

        DerivedClass1 newg;

        {
                std::ifstream ifs("filename");
                boost::archive::xml_iarchive ia(ifs);
                ia >> boost::serialization::make_nvp("g1", newg);
        }

        newg.display();

        return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net