|
Boost Users : |
Subject: [Boost-users] [serialization] polymorphic archives in dlls and pointer to derived problems
From: Kolb, Jeremy (jkolb_at_[hidden])
Date: 2011-01-04 11:13:39
I'm having a lot of trouble getting serialization of derived classes
through a base pointer working across dlls. I have the following:
DLL (data_types.dll) code:
stdafx.h:
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff
from Windows headers
// Windows Header Files:
#include <windows.h>
#pragma warning(disable: 4308)
#include "boost/archive/polymorphic_iarchive.hpp"
#include "boost/archive/polymorphic_oarchive.hpp"
#include "boost/serialization/nvp.hpp"
Base.h:
#pragma once
#include "boost/serialization/export.hpp"
class __declspec(dllexport) Base
{
friend class boost::serialization::access;
template<class Archive> void serialize(Archive& ar, const unsigned
int version);
public:
Base() {}
virtual ~Base() {}
int i;
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Base); // Doesn't make a difference.
BOOST_CLASS_EXPORT_KEY(Base);
Base.cpp:
#include "stdafx.h"
#include "Base.h"
template<class Archive>
void Base::serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(i);
}
BOOST_CLASS_EXPORT_IMPLEMENT(Base);
template void __declspec(dllexport)
Base::serialize(boost::archive::polymorphic_iarchive& ar, const unsigned
int version);
template void __declspec(dllexport)
Base::serialize(boost::archive::polymorphic_oarchive& ar, const unsigned
int version);
Derived.h:
#include "Base.h"
class __declspec(dllexport) Derived : public Base
{
friend class boost::serialization::access;
template<class Archive> void serialize(Archive& ar, const unsigned
int version);
public:
Derived() {}
Derived(int num) : j(num) {}
int j;
};
BOOST_CLASS_EXPORT_KEY(Derived);
Derived.cpp:
#include "stdafx.h"
#include "Derived.h"
template<class Archive>
void Derived::serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
ar & BOOST_SERIALIZATION_NVP(j);
}
BOOST_CLASS_EXPORT_IMPLEMENT(Derived);
template void __declspec(dllexport)
Derived::serialize(boost::archive::polymorphic_iarchive& ar, const
unsigned int version);
template void __declspec(dllexport)
Derived::serialize(boost::archive::polymorphic_oarchive& ar, const
unsigned int version);
My exe links against the DLL and contains the following:
#include "stdafx.h"
#include "boost/archive/polymorphic_xml_iarchive.hpp"
#include "boost/archive/polymorphic_xml_oarchive.hpp"
#include "data_types/Derived.h"
#include <fstream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ofstream f("c:\\test.xml");
boost::archive::polymorphic_xml_oarchive xml(f);
Base* b = new Derived(123);
xml & BOOST_SERIALIZATION_NVP(b);
delete b;
return 0;
}
When serializing this fails with unregistered_class even though I export
both Base and Derived.
Jeremy
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