[boost::serialization] Convert enumeration to string and back again

Another "basic" question for the list again, apologies in advance. I have a class with a data member which is an enumeration, e.g.: typedef enum param_choice { PARAM_CHOICE_UINT8, PARAM_CHOICE_UINT16, PARAM_CHOICE_UINT32, PARAM_CHOICE_INT8, PARAM_CHOICE_BOOL } param_choice_e; I can quite happily user the Boost Serialization library to save and restore a class with a data member of this type: class Stuff { friend std::ostream& operator<<(std::ostream& os, const Stuff& stuff friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(choice); } param_choice choice; public: Stuff () {} Stuff (param_choice theChoice); }; This is using XML as the file-format. The data member appears in the file as an integer. However, what I would LIKE to do is output the value of "choice" as a string (e.g. instead of outputing <choice>2</choice>, the file contains <choice>UINT32</choice>). And do a conversion when I read it all back in again, of course. Can someone please provide some pointers to where I can find out how to do this? Or an example? Is the solution going to end up along the lines of: * Split the serialize into separate load and save routines * In the save, convert the data member into a string and output this to the archive * In the load, input from the archive into a string and then store this into the data member Best regards in anticipation, Graham.

Experiment with something like: class param_choice { enum { PARAM_CHOICE_UINT8, PARAM_CHOICE_UINT16, PARAM_CHOICE_UINT32, PARAM_CHOICE_INT8, PARAM_CHOICE_BOOL } m_current_value; template<class Archive> save(Archive &ar, unsigned int version); std::string svalue; switch(m_current_value){ case PARAM_CHOICE_UINT8: svalue = "PARAM_CHOICE_UINT8"; ... } ar << BOOST_SERIALIZATION_NVP(svalue); } template<class Archive> load(Archive &ar, unsigned int version){ std::string svalue; ar >> BOOST_SERIALIZATION_NVP(svalue); if(svalue == std::string("PARAM_CHOICE_UINT8") m_current_value = PARAM_CHOICE_UINT8; else if(... ... } }; Robert Ramey "Graham Hudspith" <graham.hudspith@ubiquisys.com> wrote in message news:000301c6d7fb$8e8a76c0$7701a8c0@ubiquisys.local... Another "basic" question for the list again, apologies in advance. I have a class with a data member which is an enumeration, e.g.: typedef enum param_choice { PARAM_CHOICE_UINT8, PARAM_CHOICE_UINT16, PARAM_CHOICE_UINT32, PARAM_CHOICE_INT8, PARAM_CHOICE_BOOL } param_choice_e; I can quite happily user the Boost Serialization library to save and restore a class with a data member of this type: class Stuff { friend std::ostream& operator<<(std::ostream& os, const Stuff& stuff friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(choice); } param_choice choice; public: Stuff () {} Stuff (param_choice theChoice); }; This is using XML as the file-format. The data member appears in the file as an integer. However, what I would LIKE to do is output the value of "choice" as a string (e.g. instead of outputing <choice>2</choice>, the file contains <choice>UINT32</choice>). And do a conversion when I read it all back in again, of course. Can someone please provide some pointers to where I can find out how to do this? Or an example? Is the solution going to end up along the lines of: a.. Split the serialize into separate load and save routines b.. In the save, convert the data member into a string and output this to the archive c.. In the load, input from the archive into a string and then store this into the data member Best regards in anticipation, Graham. ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Graham Hudspith
-
Robert Ramey