[Boost serialization] non intrusive & derivated class
Hello. I'm currently trying to integrate the boost::serialization lib into my project and I encounter some problems : - With the non intrusive version I just cannot get my derivated class to serialize properly. I'm getting the "Invalid XML tag name" error (see http://codepad.org/EYHfWxqN http://codepad.org/EYHfWxqN for a short code sample ) - Still with the non intrusive version, I really don't want to break my encapsulation with public attributes. So I wonder if I could use friend instead, but friend of what ?? (friend class boost::serialization::access doesnt seems to work) Thx for your help. -- View this message in context: http://old.nabble.com/-Boost-serialization--non-intrusive---derivated-class-... Sent from the Boost - Users mailing list archive at Nabble.com.
I actually found a way to use friendship in my sample code. It looks like class Father{ protected : string who; template<class Archive> friend void serialize(Archive& , Father&, const unsigned int); public : Father(){ who = "father";}; virtual void whoAreYou(){ cout << "Hello, I am the " << this->who << endl; }; }; Seems to work but is it the cleanest way to make it ? -- View this message in context: http://old.nabble.com/-Boost-serialization--non-intrusive---derivated-class-... Sent from the Boost - Users mailing list archive at Nabble.com.
Zitat von agrosjea
I actually found a way to use friendship in my sample code. It looks like
class Father{ protected : string who; template<class Archive> friend void serialize(Archive& , Father&, const unsigned int); public : Father(){ who = "father";}; virtual void whoAreYou(){ cout << "Hello, I am the " << this->who << endl; }; };
Seems to work but is it the cleanest way to make it ?
nothing wrong with that. class A{ template<class Archive> friend void serialize(Archive &,A &,unsigned int){ } }; should be equivalent to class A{ friend class serialization::access; template<class Archive> void serialize(Archive &,unsigned int){ } };
The intended way to deal with is is use class Father { friend class boost::serialization::acess; ... }; agrosjea wrote:
I actually found a way to use friendship in my sample code. It looks like
class Father{ protected : string who; template<class Archive> friend void serialize(Archive& , Father&, const unsigned int); public : Father(){ who = "father";}; virtual void whoAreYou(){ cout << "Hello, I am the " << this->who << endl; }; };
Seems to work but is it the cleanest way to make it ?
But of course this isn't non-intrusive anymore. Robert Ramey agrosjea wrote:
I actually found a way to use friendship in my sample code. It looks like
class Father{ protected : string who; template<class Archive> friend void serialize(Archive& , Father&, const unsigned int); public : Father(){ who = "father";}; virtual void whoAreYou(){ cout << "Hello, I am the " << this->who << endl; }; };
Seems to work but is it the cleanest way to make it ?
Ok Thx, And do you have an idea concerning the invalid XML tag error ? -- View this message in context: http://old.nabble.com/-Boost-serialization--non-intrusive---derivated-class-... Sent from the Boost - Users mailing list archive at Nabble.com.
agrosjea wrote:
Hello.
I'm currently trying to integrate the boost::serialization lib into my project and I encounter some problems :
- With the non intrusive version I just cannot get my derivated class to serialize properly. I'm getting the "Invalid XML tag name" error (see http://codepad.org/EYHfWxqN http://codepad.org/EYHfWxqN for a short code sample )
- Still with the non intrusive version, I really don't want to break my encapsulation with public attributes. So I wonder if I could use friend instead, but friend of what ?? (friend class boost::serialization::access doesnt seems to work)
Thx for your help.
I found the problem in my code, the BOOST_SERIALIZATION_NVP macro doesnt seems to support the derivated classes. so I used boost::serialization::make_nvp(....) instead it replaces ar << BOOST_SERIALIZATION_NVP(boost::serialization::base_object<Father>(g)); by ar << boost::serialization::make_nvp("son", boost::serialization::base_object<Father>(g)); Hope this is gonna help someone... -- View this message in context: http://old.nabble.com/-Boost-serialization--non-intrusive---derivated-class-... Sent from the Boost - Users mailing list archive at Nabble.com.
AMDG agrosjea wrote:
I found the problem in my code, the BOOST_SERIALIZATION_NVP macro doesnt seems to support the derivated classes. so I used boost::serialization::make_nvp(....) instead
it replaces ar << BOOST_SERIALIZATION_NVP(boost::serialization::base_object<Father>(g)); by ar << boost::serialization::make_nvp("son", boost::serialization::base_object<Father>(g));
You could also use BOOST_SERIALIZATION_BASE_OBJECT_NVP In Christ, Steven Watanabe
BOOST_SERIALIZATION_BASE_OBJECT_NVP isnt working in a non intrusive case, since the macro uses this. Steven Watanabe-4 wrote:
AMDG
agrosjea wrote:
I found the problem in my code, the BOOST_SERIALIZATION_NVP macro doesnt seems to support the derivated classes. so I used boost::serialization::make_nvp(....) instead
it replaces ar << BOOST_SERIALIZATION_NVP(boost::serialization::base_object<Father>(g)); by ar << boost::serialization::make_nvp("son", boost::serialization::base_object<Father>(g));
You could also use BOOST_SERIALIZATION_BASE_OBJECT_NVP
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://old.nabble.com/-Boost-serialization--non-intrusive---derivated-class-... Sent from the Boost - Users mailing list archive at Nabble.com.
agrosjea wrote:
BOOST_SERIALIZATION_BASE_OBJECT_NVP isnt working in a non intrusive case, since the macro uses this.
Hmmm - this never came up before. But look likes your correct. The
definition
of BOOST_SERIALIZATION_BASE_OBJECT_NVP is
#define BOOST_SERIALIZATION_BASE_OBJECT_NVP(name) \
boost::serialization::make_nvp( \
BOOST_PP_STRINGIZE(name), \
boost::serialization::base_object<name >(*this) \
)
So you might try:
template<class Archive>
void save(Archive &ar, const my_class & t, const unsigned version){
ar << boost::serialization::make_nvp(
"base_class_name",
boost::serialization::base_object
participants (4)
-
agrosjea
-
Robert Ramey
-
Stefan Strasser
-
Steven Watanabe