#include <string_conversion.h>
// An enumerated type enum TMyType { Foo = 0, Bar, FooBar, Unknown };
// The conversion table static const CStringConversion<TMyType>::CPair stringTable [] = { { "Foo", Foo }, { "Bar", Bar }, { "FooBar", FooBar } };
// The helper object for conversion (instance of this class) static const CStringConversion conversion(stringTable, sizeof(stringTable) / sizeof(stringTable[0]), Unknown);
// Some conversions request TMyType value1 = conversion.fromString("foo"); // returns 'foo' value1 = conversion.fromString("Foo"); // returns 'foo' (this is case unsensitive by default) std::string str = conversion.toString(Bar) // returns "Bar"
NB : Please note that that helpers macros are provided to build such a table in an easy way
Nevrax France
Definition at line 83 of file string_conversion.h.
Public Types | |
typedef DestType | TDestType |
typedef Pred | TPred |
Public Member Functions | |
CStringConversion (const CPair *pairs, uint numPairs, const DestType ¬FoundValue) | |
const TDestType & | fromString (const std::string &str) const |
uint16 | getNbPairs () const |
void | insert (const char *str, TDestType value) |
const std::string & | toString (const TDestType &value) const |
Private Types | |
typedef std::map< TDestType, std::string > | TDestType2String |
typedef std::map< std::string, TDestType, TPred > | TString2DestType |
Private Attributes | |
TDestType2String | _DestType2String |
TDestType | _NotFoundValue |
TString2DestType | _String2DestType |
|
Definition at line 86 of file string_conversion.h. Referenced by NLMISC::CStringConversion< DestType, Pred >::insert(). |
|
Definition at line 111 of file string_conversion.h. |
|
Definition at line 87 of file string_conversion.h. |
|
Definition at line 110 of file string_conversion.h. |
|
CStringConversion ctor Definition at line 159 of file string_conversion.h. References NLMISC::CStringConversion< DestType, Pred >::_DestType2String, NLMISC::CStringConversion< DestType, Pred >::_NotFoundValue, NLMISC::CStringConversion< DestType, Pred >::_String2DestType, NLMISC::CStringConversion< DestType, Pred >::CPair::Str, and uint.
00160 { 00161 for(uint k = 0; k < numPairs; ++k) 00162 { 00163 _String2DestType[pairs[k].Str] = pairs[k].Value; 00164 _DestType2String[pairs[k].Value] = pairs[k].Str; 00165 } 00166 _NotFoundValue = notFoundValue; 00167 } |
|
Definition at line 180 of file string_conversion.h. References NLMISC::CStringConversion< DestType, Pred >::_NotFoundValue, and NLMISC::CStringConversion< DestType, Pred >::_String2DestType.
00181 { 00182 typename TString2DestType::const_iterator it = _String2DestType.find(str); 00183 if (it == _String2DestType.end()) 00184 { 00185 return _NotFoundValue; 00186 } 00187 else 00188 { 00189 return it->second; 00190 } 00191 } |
|
Definition at line 107 of file string_conversion.h. References NLMISC::CStringConversion< DestType, Pred >::_String2DestType, and uint16.
00107 { return _String2DestType.size(); } |
|
Definition at line 171 of file string_conversion.h. References NLMISC::CStringConversion< DestType, Pred >::_DestType2String, NLMISC::CStringConversion< DestType, Pred >::_String2DestType, NLMISC::CStringConversion< DestType, Pred >::TDestType, and value.
00172 { 00173 _String2DestType[str] = value; 00174 _DestType2String[value] = str; 00175 } |
|
|
|
Definition at line 114 of file string_conversion.h. Referenced by NLMISC::CStringConversion< DestType, Pred >::CStringConversion(), and NLMISC::CStringConversion< DestType, Pred >::insert(). |
|
Definition at line 115 of file string_conversion.h. Referenced by NLMISC::CStringConversion< DestType, Pred >::CStringConversion(), and NLMISC::CStringConversion< DestType, Pred >::fromString(). |
|