|
Boost : |
From: Dirk Gregorius (dirk_at_[hidden])
Date: 2004-12-26 08:35:11
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk