Properties and boost::any / boost::program_options

Dear group, I have a text file which defines several properties. For example: // Properties.txt "Position" "1.0 2.0 3.0" "Color" "1.0 0.0 0.0" "Intensity" "500" I wonder if it is possible to use the Property structure like defined in the boost::any example: // Property from boost::any help file struct Property { Property(); Property( const char* ) // Convinence c'tor for easy use with std::find Property( const std::string&, const boost::any& ); std::string Key; boost::any Value; } typedef std::vector<Property> Properties_t; // Operators for sorting and finding bool operator==( const Property& lhs, const Property& rhs ) { return lhs. Key == rhs.Key; } bool operator<( const Property& lhs, const Property& rhs ) { return lhs. Key < rhs.Key; } I want to do two things. First I like to parse through my text file and simply push_back() the parsed properties as pairs of strings: // The parse function - basically void ParseProperties( std::ifstream& is, Properties_t& properties ) { std::string key, value; while( is ) { is >> key >> value; properties.push_back( Property( key, value ) ); } std::sort( properties.begin(), properties.end() ); } Then later I would like to be able to process the properties like this: // Process the parsed properties... void ProcessProperties( const Properties_t& properties ) { typedef Properties_t::iterator PIter; PIter pos = find( properties.begin(), properties.end(), "Position" ); // Here we need the const char* c'tor if ( pos != properties.end() ) Vector3 position = any_cast<Vector3>( pos->Value ); } 1. Is it possible to use boost::any in this way? If yes - where and how do I have to implement the conversions? 2. If this is not possible could I use boost::programm_options instead for the value type? How and where do I have to implement the conversion here. 3. Would you suggest to define a simple Value class like this when I can not use any boost classes: class Value { public: Value(); Value( const std::string& ) template<typename T> T As(void ) const; private: std::string m_Value; } Are there any rules how to implement such a class? I have very, very few experience with template code. So do I need a traits class to look up the conversions in the As() function or how should a simple class like this be implemented in order to be nicely extendable by a client? I wish you all a nice christmas and a happy new year! Regards, -Dirk

On Sunday 26 December 2004 16:35, Dirk Gregorius wrote:
I have a text file which defines several properties. For example:
// Properties.txt "Position" "1.0 2.0 3.0" "Color" "1.0 0.0 0.0" "Intensity" "500" ..... properties.push_back( Property( key, value ) ); ..... Vector3 position = any_cast<Vector3>( pos->Value );
1. Is it possible to use boost::any in this way? If yes - where and how do I have to implement the conversions?
To the best of my knowledge -- no. boost::any does not support lexical casting of content that you seem to want. You can use lexical_cast<Vector3>*any_cast<string>(&pos->Value) or something like that.
2. If this is not possible could I use boost::programm_options instead for the value type? How and where do I have to implement the conversion here.
You'd overload the 'validate' function for your Vector3, I think. The idea is that you'll have to specify the types of all properties when declaring the options.
3. Would you suggest to define a simple Value class like this when I can not use any boost classes:
class Value { public: Value(); Value( const std::string& )
template<typename T> T As(void ) const;
private: std::string m_Value; }
Seems like lexical_cast does what you want. - Volodya
participants (2)
-
Dirk Gregorius
-
Vladimir Prus