[Serialization] How to serialize third party library function
Hello First all, sorry for my long post, and I hope I will be clear with the question. In my project I have a lot of classes, which I plan to serialize with boost serialization library. Most of my classes have as members third party library classes. The thing is that the third party library classes are very complex and I can't serialize them directly in the standard way i.e I can't say this : template<class Arhive,class MyThirdPartyClass> void serialize(Archive& ar, MyThirdPartyClass& t, const unsigned int version) { ar & t.meber1; ar & t.member2; .... } MyThirdparty library classes all provide their own method for serialization ( storing in custom archive and reading from the archive), so I was thinking to do this : namespace boost { namespace serialization { template<class Archive> inline void save(Archive & ar, const CustomClass& t, const unsigned int version) { /// save t to the custom archive here . } template<class Archive> inline void load(Archive & ar, CustomClass& t, const unsigned int version) { ////// read data from archive and assign to t } } } Now when I do the following : // save data CustomClass* cc=new CustomClass(.....); outarchive & cc; /// later on read CustomClass * rcc=0; inarchive & rcc; delete rcc; /// (*) crash here. delete is operator implemented in my CustomClass I notice that the crash maybe has to do something with that, that my CustomClass implements its own new and delete operators. I know that the default implementation of load_construct data uses operator placement ::new ( global new operator ) to allocate object when loading through pointer, and I am deleting the object above ( * ) with its specific class delete operator. To avoid the mismatch I did this : template<class Archive> inline void load_construct_data(Archive & ar, CustomClass * t, const unsigned int file_version) { new (t) CustomClass; // notice that this is the custom class operator , not :: new } In my suprise (*) is still crashing. Why ? Also I notice that if in load_construct data I am using the global new operator, and when deleting the global ::delete ( i.e :: delete rcc) operator there is no problem at all. But this is not solution, cause I want my CustomClass to use their own specific operators new and delete.
I notice that the crash maybe has to do something with that, that my CustomClass implements its own new and delete operators. I know that the default implementation of load_construct data uses operator placement ::new ( global new operator ) to allocate object when loading through pointer, a ** I don't know which version of boost you're using. The most recent version(s) make use of the type trait "has_new_operator" to call a class specific new operator. Without having a specific case to play with I don't know for sure, but it may address you're situation. Robert Ramey
OK, I know installed the latest version of boost, which uses has_new_operator type trait. But , know I am getting compiler errors , for this minimal test program : #include <boost/type_traits/has_new_operator.hpp> class A { public: void* operator new(std::size_t); void* operator new[] (size_t); void* operator new(std::size_t, void*); }; int main() { boost::has_new_operator<A>(); } The compiler errors are : Error 1: error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100 Error 2 : error C2866: 'boost::detail::has_new_operator_impl<T>::s1' : a const static data member of a managed type must be initialized at the point of declaration \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 93 Error 3: Error 1 error C2668: 'boost::detail::has_new_operator_impl<T>::check_sig' : ambiguous call to overloaded function \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 93 If you can see what the problem is Thank you for your time On Fri, Dec 4, 2009 at 5:25 PM, Robert Ramey <ramey@rrsd.com> wrote:
I notice that the crash maybe has to do something with that, that my CustomClass implements its own new and delete operators.
I know that the default implementation of load_construct data uses operator placement ::new ( global new operator ) to allocate object when loading through pointer, a
** I don't know which version of boost you're using. The most recent version(s) make use of the type trait "has_new_operator" to call a class specific new operator. Without having a specific case to play with I don't know for sure, but it may address you're situation.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG elizabeta petreska wrote:
OK, I know installed the latest version of boost, which uses has_new_operator type trait.
But , know I am getting compiler errors , for this minimal test program :
#include <boost/type_traits/has_new_operator.hpp> class A { public: void* operator new(std::size_t); void* operator new[] (size_t); void* operator new(std::size_t, void*); };
int main() { boost::has_new_operator<A>(); }
The compiler errors are :
Error 1: error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100
What compile are you using? The code compiles fine for me with Visual C++ 8.0 and 9.0. In Christ, Steven Watanabe
Hello I am using Visual C++ 8.0 , and can't compile it :( Can someone else check if the above is compiling with Visual Studio 2005 Thanks On Mon, Dec 7, 2009 at 10:01 PM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
elizabeta petreska wrote:
OK, I know installed the latest version of boost, which uses has_new_operator type trait.
But , know I am getting compiler errors , for this minimal test program :
#include <boost/type_traits/has_new_operator.hpp> class A { public: void* operator new(std::size_t); void* operator new[] (size_t); void* operator new(std::size_t, void*); };
int main() { boost::has_new_operator<A>(); }
The compiler errors are :
Error 1: error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100
What compile are you using? The code compiles fine for me with Visual C++ 8.0 and 9.0.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
----- Original Message ----- From: "elizabeta petreska"
error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100
I just tried compiling your code with VS8 (Express Edition) and got the same error. Are you also using Express Edition?
John
Hello You could also make a template class that handles your serialization of the third party library classes (you probably want to split save/load). A wrapper or holder class if you will that should only exist on the stack.. That way you can serialize the classes in any way you want to - while still getting benifits from the serialization library. The downside is the added complexity and in my case i had to make sure that pointers are not released before your done serializing, since the library tracks them (otherwise the same address could be reused and the serialization library will identify it as the same object). Not sure if that is the kind of solution you are looking for though, might be easier to have the compile error fixed. Regards Rune On Tue, Dec 8, 2009 at 10:12 AM, John Emmas <johne53@tiscali.co.uk> wrote:
----- Original Message ----- From: "elizabeta petreska"
error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100
I just tried compiling your code with VS8 (Express Edition) and got the same error. Are you also using Express Edition?
John
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You could also make a template class that handles your serialization of
I am using the professional edition of Visual Studio 2005. the third party library classes (you probably want to split save/load). A
wrapper or holder class if you will that should only exist on the stack..
I don't understand you what you mean with this. Sorry , can you explain me a little ? Also in this moment I solved the problem with making explicit specialization for the heap_allocator template , i.e I made the following : namespace boost { namespace archive { namespace detail { template<> struct heap_allocator<CustomClass> { static CustomClass* invoke() { return static_cast<CustomClass *>( new CustomClass ); // this one uses CustomClass specific new operator } }; } } } And I also made load/save pair for my CustomClass. The problem is that I have a lot of such custom classes , and I should write explicit specialization for heap_allocator template for each of them. :( So the most easier way would be to have the compiler error fixed. On Tue, Dec 8, 2009 at 11:49 AM, Rune Lund Olesen <rune.olesen@gmail.com>wrote:
Hello
You could also make a template class that handles your serialization of the third party library classes (you probably want to split save/load). A wrapper or holder class if you will that should only exist on the stack.. That way you can serialize the classes in any way you want to - while still getting benifits from the serialization library. The downside is the added complexity and in my case i had to make sure that pointers are not released before your done serializing, since the library tracks them (otherwise the same address could be reused and the serialization library will identify it as the same object).
Not sure if that is the kind of solution you are looking for though, might be easier to have the compile error fixed.
Regards Rune
On Tue, Dec 8, 2009 at 10:12 AM, John Emmas <johne53@tiscali.co.uk> wrote:
----- Original Message ----- From: "elizabeta petreska"
error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100
I just tried compiling your code with VS8 (Express Edition) and got the same error. Are you also using Express Edition?
John
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
The issue here is the new type_trait and it's usage "has_new_operator". This is a recent addition which likely requires some more fine tuning. There is a track item on this in either the the serialization and/or type_traits libraries. For now this is a pending issue - patches welcome. Robert Ramey "elizabeta petreska" <elizabeta.petreska@gmail.com> wrote in message news:ff8898bd0912080051p2fde1bc1t8db88e8b89ab5c05@mail.gmail.com... Hello I am using Visual C++ 8.0 , and can't compile it :( Can someone else check if the above is compiling with Visual Studio 2005 Thanks On Mon, Dec 7, 2009 at 10:01 PM, Steven Watanabe <watanabesj@gmail.com> wrote: AMDG elizabeta petreska wrote: OK, I know installed the latest version of boost, which uses has_new_operator type trait. But , know I am getting compiler errors , for this minimal test program : #include <boost/type_traits/has_new_operator.hpp> class A { public: void* operator new(std::size_t); void* operator new[] (size_t); void* operator new(std::size_t, void*); }; int main() { boost::has_new_operator<A>(); } The compiler errors are : Error 1: error C2975: 'b1' : invalid template argument for 'boost::type_traits::ice_or', expected compile-time constant expression \libs\boost\inc\boost-1_41\boost\type_traits\has_new_operator.hpp 100 What compile are you using? The code compiles fine for me with Visual C++ 8.0 and 9.0. In Christ, Steven Watanabe _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG elizabeta petreska wrote:
Hello I am using Visual C++ 8.0 , and can't compile it :( Can someone else check if the above is compiling with Visual Studio 2008
Apparently the problem has been fixed in the trunk. http://svn.boost.org/trac/boost/changeset/56730 In Christ, Steven Watanabe
Thank you so much. I took the changeset directly from the trunk, and now it works. I am wondering why this change is not included in the latest version of boost 1.41.0. The change is made a month earlier , before boost 1.41.0 version is released. On Tue, Dec 8, 2009 at 5:49 PM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
elizabeta petreska wrote:
Hello I am using Visual C++ 8.0 , and can't compile it :( Can someone else check if the above is compiling with Visual Studio 2008
Apparently the problem has been fixed in the trunk.
http://svn.boost.org/trac/boost/changeset/56730
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
elizabeta petreska
-
John Emmas
-
Robert Ramey
-
Rune Lund Olesen
-
Steven Watanabe