NLMISC::CStringConversion< DestType, Pred > Class Template Reference

#include <string_conversion.h>


Detailed Description

template<class DestType, class Pred = CUnsensitiveStrLessPred>
class NLMISC::CStringConversion< DestType, Pred >

This class allow simple mapping between string and other type (such as integral types or enum) In fact this primarily intended to make a string / enum correspondance Example of use :

// 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

See also:
NL_BEGIN_STRING_CONVERSION_TABLE

NL_END_STRING_CONVERSION_TABLE

NB: by default this class behaves in a case unsensitive way. To change this, just change the 'Pred' template parameter to std::less<std::string>

Author:
Nicolas Vizerie

Nevrax France

Date:
2003

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 &notFoundValue)
const TDestTypefromString (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


Member Typedef Documentation

template<class DestType, class Pred = CUnsensitiveStrLessPred>
typedef DestType NLMISC::CStringConversion< DestType, Pred >::TDestType
 

Definition at line 86 of file string_conversion.h.

Referenced by NLMISC::CStringConversion< DestType, Pred >::insert().

template<class DestType, class Pred = CUnsensitiveStrLessPred>
typedef std::map<TDestType, std::string> NLMISC::CStringConversion< DestType, Pred >::TDestType2String [private]
 

Definition at line 111 of file string_conversion.h.

template<class DestType, class Pred = CUnsensitiveStrLessPred>
typedef Pred NLMISC::CStringConversion< DestType, Pred >::TPred
 

Definition at line 87 of file string_conversion.h.

template<class DestType, class Pred = CUnsensitiveStrLessPred>
typedef std::map<std::string, TDestType, TPred> NLMISC::CStringConversion< DestType, Pred >::TString2DestType [private]
 

Definition at line 110 of file string_conversion.h.


Constructor & Destructor Documentation

template<class DestType, class Pred>
NLMISC::CStringConversion< DestType, Pred >::CStringConversion const CPair pairs,
uint  numPairs,
const DestType &  notFoundValue
 

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 }


Member Function Documentation

template<class DestType, class Pred>
const DestType & NLMISC::CStringConversion< DestType, Pred >::fromString const std::string &  str  )  const
 

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 }

template<class DestType, class Pred = CUnsensitiveStrLessPred>
uint16 NLMISC::CStringConversion< DestType, Pred >::getNbPairs  )  const [inline]
 

Definition at line 107 of file string_conversion.h.

References NLMISC::CStringConversion< DestType, Pred >::_String2DestType, and uint16.

00107 { return _String2DestType.size(); }

template<class DestType, class Pred>
void NLMISC::CStringConversion< DestType, Pred >::insert const char *  str,
TDestType  value
 

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 }

template<class DestType, class Pred = CUnsensitiveStrLessPred>
const std::string& NLMISC::CStringConversion< DestType, Pred >::toString const TDestType value  )  const
 


Field Documentation

template<class DestType, class Pred = CUnsensitiveStrLessPred>
TDestType2String NLMISC::CStringConversion< DestType, Pred >::_DestType2String [private]
 

Definition at line 114 of file string_conversion.h.

Referenced by NLMISC::CStringConversion< DestType, Pred >::CStringConversion(), and NLMISC::CStringConversion< DestType, Pred >::insert().

template<class DestType, class Pred = CUnsensitiveStrLessPred>
TDestType NLMISC::CStringConversion< DestType, Pred >::_NotFoundValue [private]
 

Definition at line 115 of file string_conversion.h.

Referenced by NLMISC::CStringConversion< DestType, Pred >::CStringConversion(), and NLMISC::CStringConversion< DestType, Pred >::fromString().

template<class DestType, class Pred = CUnsensitiveStrLessPred>
TString2DestType NLMISC::CStringConversion< DestType, Pred >::_String2DestType [private]
 

Definition at line 113 of file string_conversion.h.

Referenced by NLMISC::CStringConversion< DestType, Pred >::CStringConversion(), NLMISC::CStringConversion< DestType, Pred >::fromString(), NLMISC::CStringConversion< DestType, Pred >::getNbPairs(), and NLMISC::CStringConversion< DestType, Pred >::insert().


The documentation for this class was generated from the following file:
Generated on Tue Mar 16 13:35:24 2004 for NeL by doxygen 1.3.6