Serialization, shared_ptr and namespaces
Hi, I'm trying to serialize a list of shared_ptr's to objects. Here's a summary of the code I have: namespace MyNamespace { class B { //implementation plus standard serialization function }; class A { private: list< shared_ptr<B> > myBs; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(myBs); } }; } If I just try to serialize it, I get an unregistered_class exception. So I think I have to use BOOST_SHARED_POINTER_EXPORT(_GUID) to register the shared_ptr. When I include this inside the namespace (please note that the error messages are from my real code, so they have slightly other names: BOOST_SHARED_POINTER_EXPORT(B) I get this: f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2146: syntax error : missing ';' before identifier 'BOOST_CLASS_EXPORT_GUID' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(109) : error C2143: syntax error : missing ';' before '}' When I put it outside the namespace I have to use a fully qualified name including the namespace for class B, so it becomes BOOST_SHARED_POINTER_EXPORT(MyNamespace::B) Then I get (not Xenocide is here what MyNamespace is in my example): f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name I also tried various combinations with BOOST_SHARED_POINTER_EXPORT_GUID but it didn't help. Can someone explain to me how I have to declare shared_ptr's in order for them to work with boost::serialization? I was unable to find some more indepth documentation. Thanks, Jan Eickmann
Hello, sorry to bump this but I still haven't found a solution or any information on how to use shared_ptr together with serialization. Anybody can shed some light on this? Thanks a lot, Jan Eickmann Jan Eickmann wrote:
Hi,
I'm trying to serialize a list of shared_ptr's to objects.
Here's a summary of the code I have:
namespace MyNamespace {
class B { //implementation plus standard serialization function };
class A { private: list< shared_ptr<B> > myBs; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(myBs); } };
}
If I just try to serialize it, I get an unregistered_class exception. So I think I have to use BOOST_SHARED_POINTER_EXPORT(_GUID) to register the shared_ptr.
When I include this inside the namespace (please note that the error messages are from my real code, so they have slightly other names:
BOOST_SHARED_POINTER_EXPORT(B)
I get this: f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2146: syntax error : missing ';' before identifier 'BOOST_CLASS_EXPORT_GUID' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(109) : error C2143: syntax error : missing ';' before '}'
When I put it outside the namespace I have to use a fully qualified name including the namespace for class B, so it becomes
BOOST_SHARED_POINTER_EXPORT(MyNamespace::B)
Then I get (not Xenocide is here what MyNamespace is in my example): f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name
I also tried various combinations with BOOST_SHARED_POINTER_EXPORT_GUID but it didn't help.
Can someone explain to me how I have to declare shared_ptr's in order for them to work with boost::serialization? I was unable to find some more indepth documentation.
Thanks,
Jan Eickmann
"Jan Eickmann"
Hello,
...
I'm trying to serialize a list of shared_ptr's to objects.
Here's a summary of the code I have:
namespace MyNamespace {
class B { //implementation plus standard serialization function };
class A { private: list< shared_ptr<B> > myBs; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(myBs); } };
}
If I just try to serialize it, I get an unregistered_class exception. So I think I have to use BOOST_SHARED_POINTER_EXPORT(_GUID) to register the shared_ptr.
When I include this inside the namespace (please note that the error messages are from my real code, so they have slightly other names:
BOOST_SHARED_POINTER_EXPORT(B)
I get this: f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2146: syntax error : missing ';' before identifier 'BOOST_CLASS_EXPORT_GUID' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(109) : error C2143: syntax error : missing ';' before '}'
When I put it outside the namespace I have to use a fully qualified name including the namespace for class B, so it becomes
BOOST_SHARED_POINTER_EXPORT(MyNamespace::B)
Then I get (not Xenocide is here what MyNamespace is in my example): f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name
I also tried various combinations with BOOST_SHARED_POINTER_EXPORT_GUID but it didn't help.
Can someone explain to me how I have to declare shared_ptr's in order for them to work with boost::serialization? I was unable to find some more indepth documentation.
I've modified shared_ptr.hpp as follows:
// This macro is used to export GUIDS for shared pointers to allow
// the serialization system to export them properly. David Tonge
//#define BOOST_SHARED_POINTER_EXPORT_GUID(T, K) \
// typedef boost::detail::sp_counted_base_impl< \
// T *, \
// boost::checked_deleter< T > \
// > __shared_ptr_ ## T; \
// BOOST_CLASS_EXPORT_GUID(__shared_ptr_ ## T, "__shared_ptr_" K)\
// BOOST_CLASS_EXPORT_GUID(T, K) \
// /**/
/**/
#define BOOST_SHARED_POINTER_EXPORT_GUID(T,N) \
typedef boost::detail::sp_counted_base_impl< \
T *, \
boost::checked_deleter< T > \
> __shared_ptr_ ## N; \
BOOST_CLASS_EXPORT(__shared_ptr_ ## N) \
BOOST_CLASS_EXPORT(T) \
/**/
#define BOOST_SHARED_POINTER_EXPORT(T) \
BOOST_SHARED_POINTER_EXPORT_GUID( \
T, \
BOOST_PP_STRINGIZE(T) \
) \
/**/
#endif // BOOST_SERIALIZATION_SHARED_PTR_HPP
Then I've got a separate .cpp file and use like so:
#define BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
#include
In libs/serialization/test there is a test_list_ptrs.cpp that shows how to serialize a list with pointers. Then, in libs/example there is a demo_shared_ptr.cpp that shows how to serialize shared_ptrs. Hopefully you can combine those and it should show how to serialize a list of shared_ptrs Hope this helps, -delfin
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Jan Eickmann Sent: Tuesday, May 03, 2005 1:44 PM To: boost-users@lists.boost.org Subject: [Boost-users] Re: Serialization, shared_ptr and namespaces
Hello,
sorry to bump this but I still haven't found a solution or any information on how to use shared_ptr together with serialization.
Anybody can shed some light on this?
Thanks a lot,
Jan Eickmann
Jan Eickmann wrote:
Hi,
I'm trying to serialize a list of shared_ptr's to objects.
Here's a summary of the code I have:
namespace MyNamespace {
class B { //implementation plus standard serialization function };
class A { private: list< shared_ptr<B> > myBs; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(myBs); } };
}
If I just try to serialize it, I get an unregistered_class exception. So I think I have to use BOOST_SHARED_POINTER_EXPORT(_GUID) to register the shared_ptr.
When I include this inside the namespace (please note that the error messages are from my real code, so they have slightly other names:
BOOST_SHARED_POINTER_EXPORT(B)
I get this: f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2146: syntax error : missing ';' before identifier 'BOOST_CLASS_EXPORT_GUID' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(109) : error C2143: syntax error : missing ';' before '}'
When I put it outside the namespace I have to use a fully qualified name including the namespace for class B, so it becomes
BOOST_SHARED_POINTER_EXPORT(MyNamespace::B)
Then I get (not Xenocide is here what MyNamespace is in my example): f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name
I also tried various combinations with BOOST_SHARED_POINTER_EXPORT_GUID but it didn't help.
Can someone explain to me how I have to declare shared_ptr's in order for them to work with boost::serialization? I was unable to find some more indepth documentation.
Thanks,
Jan Eickmann
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I concede this is somewhat confusing. I made the shared_ptr as a case study and several people helped me out with issues such as this. I don't know if all of them have been address. Its better and worse. 1.33 will have much simpler and robust implementation of shared_ptr (and weak_ptr) serialization based on work done by Martin Ecker. I believe it will address these problems. Also, the requirement that serializations be placed in a particular namespace will be relaxed and ADL will work as expected in all cases. I'm currently making it work on the older compilers. I still have pending the issue of backward compatibility so it will be a while yet. As to your particular issue, I'll have to consider it Jan Eickmann wrote:
Hello,
sorry to bump this but I still haven't found a solution or any information on how to use shared_ptr together with serialization.
Anybody can shed some light on this?
Thanks a lot,
Jan Eickmann
Jan Eickmann wrote:
Hi,
I'm trying to serialize a list of shared_ptr's to objects.
Here's a summary of the code I have:
namespace MyNamespace {
class B { //implementation plus standard serialization function };
class A { private: list< shared_ptr<B> > myBs; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(myBs); } };
}
If I just try to serialize it, I get an unregistered_class exception. So I think I have to use BOOST_SHARED_POINTER_EXPORT(_GUID) to register the shared_ptr.
When I include this inside the namespace (please note that the error messages are from my real code, so they have slightly other names:
BOOST_SHARED_POINTER_EXPORT(B)
I get this: f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2146: syntax error : missing ';' before identifier 'BOOST_CLASS_EXPORT_GUID' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(109) : error C2143: syntax error : missing ';' before '}'
When I put it outside the namespace I have to use a fully qualified name including the namespace for class B, so it becomes
BOOST_SHARED_POINTER_EXPORT(MyNamespace::B)
Then I get (not Xenocide is here what MyNamespace is in my example): f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name
I also tried various combinations with BOOST_SHARED_POINTER_EXPORT_GUID but it didn't help.
Can someone explain to me how I have to declare shared_ptr's in order for them to work with boost::serialization? I was unable to find some more indepth documentation.
Thanks,
Jan Eickmann
Thanks for all your replies. I will try the proposed ideas and pinters to documentation on the weekend and report back. Cheers, Jan Eickmann Jan Eickmann wrote:
Hello,
sorry to bump this but I still haven't found a solution or any information on how to use shared_ptr together with serialization.
Anybody can shed some light on this?
Thanks a lot,
Jan Eickmann
Jan Eickmann wrote:
Hi,
I'm trying to serialize a list of shared_ptr's to objects.
Here's a summary of the code I have:
namespace MyNamespace {
class B { //implementation plus standard serialization function };
class A { private: list< shared_ptr<B> > myBs; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(myBs); } };
}
If I just try to serialize it, I get an unregistered_class exception. So I think I have to use BOOST_SHARED_POINTER_EXPORT(_GUID) to register the shared_ptr.
When I include this inside the namespace (please note that the error messages are from my real code, so they have slightly other names:
BOOST_SHARED_POINTER_EXPORT(B)
I get this: f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2146: syntax error : missing ';' before identifier 'BOOST_CLASS_EXPORT_GUID' f:\xenocide\xenocide\src\common\simulation\simulation.h(108) : error C2059: syntax error : 'string' f:\xenocide\xenocide\src\common\simulation\simulation.h(109) : error C2143: syntax error : missing ';' before '}'
When I put it outside the namespace I have to use a fully qualified name including the namespace for class B, so it becomes
BOOST_SHARED_POINTER_EXPORT(MyNamespace::B)
Then I get (not Xenocide is here what MyNamespace is in my example): f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name f:\xenocide\xenocide\src\common\simulation\simulation.h(115) : error C2653: '__shared_ptr_Xenocide' : is not a class or namespace name
I also tried various combinations with BOOST_SHARED_POINTER_EXPORT_GUID but it didn't help.
Can someone explain to me how I have to declare shared_ptr's in order for them to work with boost::serialization? I was unable to find some more indepth documentation.
Thanks,
Jan Eickmann
participants (4)
-
Delfin Rojas
-
Jan Eickmann
-
Jeff Flinn
-
Robert Ramey