
Hi to all I am new to the boost library, I downloaded it and I am doing some tests. I was using MFC serialization. I want to do something like this : template<class _CReceiver, class _CMemberType> class CASetMemberCommand { friend class boost::serialization::access; public: typedef void (_CReceiver::* Action)(_CMemberType) ; protected: _CReceiver *m_pReceiver; _CMemberType m_Property; Action m_fnAction; public: CASetMemberCommand() { m_pReceiver = NULL; } CASetMemberCommand(_CReceiver *pReceiver, Action fnAction, _CMemberType memberProperty) { m_pReceiver = pReceiver; m_fnAction = fnAction; m_Property = memberProperty; } ~CASetMemberCommand() { } virtual void Execute() { if(m_pReceiver) (m_pReceiver->*m_fnAction)(m_Property); } template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & BOOST_SERIALIZATION_NVP(m_pReceiver) //& BOOST_SERIALIZATION_NVP(m_fnAction) & BOOST_SERIALIZATION_NVP(m_Property); } void Save(const char * filename) { std::ofstream ofs(filename); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(this); } void Load( const char * filename) { std::ifstream ifs(filename); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(*this); } }; int main(int argc, char *argv[]) { gps_position pos(32,52,25.0f); CASetMemberCommand<gps_position,int> cmd(&pos,&gps_position::SetMinutes,25); CASetMemberCommand<gps_position,int> cmd4(&pos,&gps_position::SetDegrees,25); cmd.Execute(); SaveCommand(cmd,"D:/test.xml"); cmd.Save("D:/Test2.xml"); CASetMemberCommand<gps_position,int> cmd2; cmd2.Load("D:/Test2.xml"); return 0; } the problem is that BOOST_SERIALIZATION_NVP(m_fnAction) is not working (compile errors); Is there any way to save a methode of w class, so it can be loaded and used later? Thank you

a) I don't see a serialize function for Action. Until this exists, it's not a serializble type. Hence it will not compile. b) The following is not symetrical and this is almost always a programmer error
oa << BOOST_SERIALIZATION_NVP(this); ... ia >> BOOST_SERIALIZATION_NVP(*this);
Robert Ramey

On 10/5/07, Robert Ramey <ramey@rrsd.com> wrote:
a) I don't see a serialize function for Action. Until this exists, it's not a serializble type. Hence it will not compile.
Ok thank you.
b) The following is not symetrical and this is almost always a programmer error
oa << BOOST_SERIALIZATION_NVP(this); ... ia >> BOOST_SERIALIZATION_NVP(*this);
I know that, but if I put oa << BOOST_SERIALIZATION_NVP(*this); I get a run time error, and when I put ia >> BOOST_SERIALIZATION_NVP(this) I get a run time error. If I do my save and load fucntion output my class, its ok and I can have symetrical Serialize. any Idea? _______________________________________________
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

at forum wrote:
On 10/5/07, Robert Ramey <ramey@rrsd.com> wrote:
a) I don't see a serialize function for Action. Until this exists, it's not a serializble type. Hence it will not compile.
Ok thank you.
b) The following is not symetrical and this is almost always a programmer error
oa << BOOST_SERIALIZATION_NVP(this); ... ia >> BOOST_SERIALIZATION_NVP(*this);
I know that, but if I put oa << BOOST_SERIALIZATION_NVP(*this); I get a run time error, and when I put ia >> BOOST_SERIALIZATION_NVP(this) I get a run time error.
If I do my save and load fucntion output my class, its ok and I can have symetrical Serialize.
any Idea?
BOOST_SERIALIZATION_NVP(this) is not suitable for "this" I believe you want something like: void Save(const char * filename, const CASetMemberCommand<gps_position,int> &c) { std::ofstream ofs(filename); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(c); } void Load( const char * filename, CASetMemberCommand<gps_position,int> & c) { std::ifstream ifs(filename); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(c); } int main(int argc, char *argv[]) { gps_position pos(32,52,25.0f); CASetMemberCommand<gps_position,int> cmd(&pos,&gps_position::SetMinutes,25); CASetMemberCommand<gps_position,int> cmd4(&pos,&gps_position::SetDegrees,25); cmd.Execute(); SaveCommand(cmd,"D:/test.xml"); Save("D:/Test2.xml", cmd); CASetMemberCommand<gps_position,int> cmd2; Load("D:/Test2.xml", cmd); return 0; } or maybe something simpler like: int main(int argc, char *argv[]) { gps_position pos(32,52,25.0f); const CASetMemberCommand<gps_position,int> cmd(&pos,&gps_position::SetMinutes,25); cmd.Execute(); { std::ofstream ofs("D:/test.xml");); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(cmd); } CASetMemberCommand<gps_position,int> cmd2; { std::ifstream ifs("D:/test.xml"); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(cmd2); } return 0; }
participants (2)
-
at forum
-
Robert Ramey