what you would really need is
something like
BOOST_CLASS_EXPORT(KukuBase<int,
5>);
BUT the the
shorthand macro BOOST_CLASS_EXPORT doesn't like template instantiations so
you'll have to
expand the macro "by hand" for the class for each set of
template
parameters
used. This is explained in the manual.
Robert
Ramey
"ari vol" <avf99avf@yahoo.com> wrote in message
news:9202.25698.qm@web45110.mail.sp1.yahoo.com...
Hey,
Boost serialization: how to
register template derived class.
The problem in
general:
I have the
following classes:
- Virtual
based class:
class
TemplateBase { }
- Derived
class:
template
<class KUKU, int KUKUINT>
class
KukuBase : public TemplateBase{ }
And I am
trying to serialize the derived class KukuBase<KUKU,
KUKUINT> through a pointer of the base
type (polymorphism…).
The problem is
that I can not find a way to
register the class KukuBase<KUKU,
KUKUINT> and thus get unregistered_class
exception.
How to make it
works?
The cpp
file:
#include
<boost/archive/text_oarchive.hpp>
#include
<boost/archive/text_iarchive.hpp>
//
Serialization
#include
<boost/serialization/string.hpp>
#include
<boost/serialization/vector.hpp>
#include
<boost/serialization/access.hpp>
#include
<boost/serialization/base_object.hpp>
#include
<boost/serialization/serialization.hpp>
#include
<boost/serialization/export.hpp>
#include
<fstream>
class
TemplateBase
{
public:
TemplateBase()
: mI(0) {}
virtual
TemplateBase* Clone() = 0;
int
mI;
private:
friend
class boost::serialization::access;
template<class
Archive>
void
serialize(Archive & ar, const unsigned int
version)
{
ar
& mI;
}
};
template
<class KUKU, int KUKUINT>
class KukuBase
: public TemplateBase
{
public:
KukuBase() : mZ(1) {}
TemplateBase* Clone() { return new KukuBase<KUKU, KUKUINT>(*this);
}
private:
friend
class boost::serialization::access;
template<class Archive>
void
serialize(Archive & ar, const unsigned int
version)
{
ar &
boost::serialization::base_object<TemplateBase>(*this);
ar & mZ;
}
float
mZ;
};
class foo {
public:
foo() :
mT(new KukuBase<int, 5>) {};
template<class Archive>
void
serialize(Archive & ar, const unsigned int
version)
{
ar & mT;
}
TemplateBase* mT;
};
BOOST_CLASS_EXPORT(foo);
//BOOST_CLASS_EXPORT((KukuBase));
BOOST_CLASS_EXPORT(TemplateBase);
int
main()
{
std::ofstream
ofs("/tmp/kuku");
boost::archive::text_oarchive
ar(ofs);
const
foo bar;
ar
<< bar;
}
The program
returns:
terminate called after throwing an instance of
'boost::archive::archive_exception'
what():
unregistered class
Abort
And the stuck
shows:
(gdb)
bt
#0
0x000000000049e409 in kill ()
#1
0x0000000000470582 in abort () at
./sysdeps/generic/abort.c:88
#2
0x0000000000461bf8 in __gnu_cxx::__verbose_terminate_handler
()
#3
0x000000000045e216 in __cxxabiv1::__terminate ()
#4
0x000000000045e243 in std::terminate ()
#5
0x000000000045e343 in __cxa_throw ()
#6
0x000000000040190c in
boost::throw_exception<boost::archive::archive_exception>
()
#7
0x00000000004015e1 in
boost::archive::detail::save_pointer_type<boost::archive::text_oarchive,
TemplateBase*>::polymorphic<TemplateBase>::save
()
#8
0x000000000040152d in
boost::archive::detail::save_pointer_type<boost::archive::text_oarchive,
TemplateBase*>::save<TemplateBase> ()
#9
0x000000000040142e in
boost::archive::detail::save_pointer_type<boost::archive::text_oarchive,
TemplateBase*>::invoke ()
#10
0x00000000004013aa in boost::archive::save<boost::archive::text_oarchive,
TemplateBase*> ()
#11
0x0000000000401344 in
boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::save_override<TemplateBase*
const> ()
#12
0x00000000004012e8 in
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::operator<<
<TemplateBase* const> ()
#13
0x0000000000401299 in
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::operator&<TemplateBase*>
()
#14
0x000000000040124c in foo::serialize<boost::archive::text_oarchive>
()
#15
0x0000000000401197 in
boost::serialization::access::serialize<boost::archive::text_oarchive,
foo> ()
#16
0x0000000000401107 in
boost::serialization::serialize<boost::archive::text_oarchive, foo>
()
#17
0x0000000000400f83 in
boost::serialization::serialize_adl<boost::archive::text_oarchive, foo>
()
#18
0x00000000004022c6 in
boost::archive::detail::oserializer<boost::archive::text_oarchive,
foo>::save_object_data ()
#19
0x0000000000405c59 in boost::archive::detail::basic_oarchive_impl::save_object
()
#20
0x0000000000400d43 in
boost::archive::detail::save_non_pointer_type<boost::archive::text_oarchive,
foo>::save_standard::invoke ()
#21
0x0000000000400c6f in
boost::archive::detail::save_non_pointer_type<boost::archive::text_oarchive,
foo>::invoke ()
#22
0x0000000000400aff in boost::archive::save<boost::archive::text_oarchive,
foo> ()
#23
0x0000000000400a38 in
boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::save_override<foo
const> ()
#24
0x000000000040090e in
boost::archive::detail::interface_oarchive<boost::archive::text_oarchive>::operator<<
<foo const> ()
#25
0x0000000000400423 in main ()
(gdb)
Quit
Thanks,
-Ariella
From: Voloshin,
Ariella
Sent: Tuesday, May 20,
2008 11:10 AM
To: Lifshitz,
Yair
Subject: RE:
Hey
Yes I will use
it,
I guess you
will need to register the: boost-users@lists.boost.org.
The guy that
wrote the boost serialization library is Robert Ramey ramey@rrsd.com.
If you like to
answer from my email thread please let me know.
From: Lifshitz,
Yair
Sent: Friday, May 16,
2008 8:55 AM
To: Voloshin,
Ariella
Cc: Lifshitz,
Yair
Subject: RE:
Hey
Great! :)
So I hope
you're going to use it.
Can you send me the
Boost owner's mail - I want to have a look and maybe have a chat with
him.
From: Voloshin,
Ariella
Sent: Thursday, May
15, 2008 11:01 PM
To: Lifshitz,
Yair
Subject:
Hey
It is working,
Took some time to compile,
long story but it is working.
The string is also
ok.
Thanks,
-Ariella
--------------------------------------------------------------------- Intel Israel (74) Limited This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.