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

 



Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.


_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users