[serialization] Serializing derived template class via base pointer
Hi everyone, has any progress has been made on a general solution for this problem? Given classes like these, struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() = default; }; template<typename T> struct derived : base { template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & data; } T data; }; is it possible to serialize the derived type (with T unknown) via a base pointer? Sticking a call to register_type<derived> in derived::serialize doesn't seem to do it.
Daniel Mitchell wrote:
Hi everyone, has any progress has been made on a general solution for this problem? Given classes like these,
struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() = default; };
template<typename T> struct derived : base { template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & data; } T data; };
is it possible to serialize the derived type (with T unknown) via a base pointer? Sticking a call to register_type<derived> in derived::serialize doesn't seem to do it.
There are several demos in the examples directory which show how to do this. Robert Ramey
Hi Robert, thanks for your reply. Unfortunately I couldn't find any of the demos to which you refer, but I did find a 2005 boost-users thread on the subject:
http://thread.gmane.org/gmane.comp.lib.boost.user/13244/
In that discussion you said you did not have a general solution. I'm just wondering if anything has changed since then e.g maybe C++11 makes a solution possible.
On Jan 17, 2013, at 2:01 PM, "Robert Ramey"
Daniel Mitchell wrote:
Hi everyone, has any progress has been made on a general solution for this problem? Given classes like these,
struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() = default; };
template<typename T> struct derived : base { template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & data; } T data; };
is it possible to serialize the derived type (with T unknown) via a base pointer? Sticking a call to register_type<derived> in derived::serialize doesn't seem to do it.
There are several demos in the examples directory which show how to do this.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Jan 17, 2013, at 2:01 PM, "Robert Ramey"
wrote: Daniel Mitchell wrote:
Hi everyone, has any progress has been made on a general solution for this problem? Given classes like these,
struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() = default; };
template<typename T> struct derived : base { template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & data; } T data; };
is it possible to serialize the derived type (with T unknown) via a base pointer? Sticking a call to register_type<derived> in derived::serialize doesn't seem to do it.
There are several demos in the examples directory which show how to do this.
Robert Ramey
Daniel Mitchell wrote:
Hi Robert, thanks for your reply. Unfortunately I couldn't find any of the demos to which you refer, but I did find a 2005 boost-users thread on the subject:
http://thread.gmane.org/gmane.comp.lib.boost.user/13244/
In that discussion you said you did not have a general solution. I'm just wondering if anything has changed since then e.g maybe C++11 makes a solution possible.
I looked at this thread and don't think it's relevant to your question. But then I looked at your question again and see that I don't really understand it. case 1 given a virtual base class A and a derived class B, serialize B (and any other derivatives of A) through a pointer to the base class A. This case is amply covered by the documentation and by examples in test and example library directories - see test_dll_exported, etc.. case 2 we have a class A which is a template so it's declared like: template<class T> class A { ... }; and class B is derived from it. Currently one would have to apply the "export" or "registration" functionality to each instance of A<T> created. I believe that the thread referred to above addresses this question. And indeed, I have no answer to this question. It might be possible using some version of "enable_if" but I have done no work in this area. Robert Ramey
On 2013-01-17 16:35, Daniel Mitchell wrote:
Hi Robert, thanks for your reply. Unfortunately I couldn't find any of the demos to which you refer, but I did find a 2005 boost-users thread on the subject:
http://thread.gmane.org/gmane.comp.lib.boost.user/13244/
In that discussion you said you did not have a general solution. I'm just wondering if anything has changed since then e.g maybe C++11 makes a solution possible.
On Jan 17, 2013, at 2:01 PM, "Robert Ramey"
wrote: Daniel Mitchell wrote:
Hi everyone, has any progress has been made on a general solution for this problem? Given classes like these,
struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { } virtual ~base() = default; };
template<typename T> struct derived : base { template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & data; } T data; };
is it possible to serialize the derived type (with T unknown) via a base pointer? Sticking a call to register_type<derived> in derived::serialize doesn't seem to do it.
There are several demos in the examples directory which show how to do this.
Robert Ramey
I don't totally understand your original issue, but does this possibly
do what you are wanting ??
#include <fstream>
#include
On Jan 17, 2013, at 4:29 PM, Tim Moore
On 2013-01-17 16:35, Daniel Mitchell wrote:
I don't totally understand your original issue, but does this possibly do what you are wanting ??
#include <fstream> #include
#include #include #include struct base { template<typename Archive> void serialize(Archive& ar, unsigned version) { }
virtual ~base() { } };
BOOST_CLASS_EXPORT_KEY(base); BOOST_CLASS_EXPORT_IMPLEMENT(base);
template<typename T> struct derived : base { derived() { } derived(int v) : data(v) { }
template<typename Archive> void serialize(Archive& ar, unsigned version) { ar & boost::serialization::base_object<base>(*this); ar & data; }
T data; };
BOOST_CLASS_EXPORT_KEY(derived<int>); BOOST_CLASS_EXPORT_IMPLEMENT(derived<int>);
int main() { base* b = new derived<int>(99); std::ofstream o("test.out", std::ios::binary); if (o.good()) { boost::archive::binary_oarchive oa(o); oa & b; } o.close(); delete b;
std::ifstream i("test.out", std::ios::binary); if (i.good()) { base* b; boost::archive::binary_iarchive ia(i); ia & b; std::cout << static_cast
(b)->data << std::endl; } i.close(); }
Hi Tim, thanks for your reply. I don't think that works for my purposes. The classes base and derived<T> are library implementation details. The type T is supplied by library users (this what I meant when I said it was an unknown type--it is unknown to me, the library author). Although I could require users to "register" their types in the way you suggest, it is very unappealing to do so, just as it would be very unappealing if the STL required types to be "registered" before they could be used in containers. That's not the kind of design I want to pursue. I don't want to impose any additional burdens on my users other than making their own types serializable (these types become the T in derived<T>). In case more context would help, look at the type object_t here: https://github.com/boostcon/cppnow_presentations_2012/blob/master/fri/value_... Basically that's the class I want to serialize. I have a container of these things and I want to use them with boost.mpi. The nested classes concept_t and model<T> correspond to base and derived<T>. The type T is the user supplied type.
participants (3)
-
Daniel Mitchell
-
Robert Ramey
-
Tim Moore