make ToString and FromString templates

This commit is contained in:
Glenn Maynard
2006-10-07 06:21:39 +00:00
parent 0493bc75a9
commit d882f21348
3 changed files with 36 additions and 21 deletions
+4 -2
View File
@@ -98,7 +98,8 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_
{ \ { \
static auto_ptr<RString> as_##X##Name[NUM_##X+2]; \ static auto_ptr<RString> as_##X##Name[NUM_##X+2]; \
return EnumToString( x, NUM_##X, X##Names, as_##X##Name ); \ return EnumToString( x, NUM_##X, X##Names, as_##X##Name ); \
} } \
template<> RString ToString<X>( const X &value ) { return X##ToString(value); }
#define XToString(X, CNT) XToString2(X) #define XToString(X, CNT) XToString2(X)
#define XToLocalizedString(X) \ #define XToLocalizedString(X) \
@@ -125,7 +126,8 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_
if( !s2.CompareNoCase(X##Names[i]) ) \ if( !s2.CompareNoCase(X##Names[i]) ) \
return (X)i; \ return (X)i; \
return (X)(i+1); /*invalid*/ \ return (X)(i+1); /*invalid*/ \
} } \
template<> bool FromString<X>( const RString &sValue, X &out ) { out = StringTo##X(sValue); return out != X##_Invalid; }
// currently unused // currently unused
#define LuaDeclareType(X) #define LuaDeclareType(X)
+12 -9
View File
@@ -1884,7 +1884,7 @@ void CollapsePath( RString &sPath, bool bRemoveLeadingDot )
} }
bool FromString( const RString &sValue, int &out ) template<> bool FromString<int>( const RString &sValue, int &out )
{ {
if( sscanf( sValue.c_str(), "%d", &out ) == 1 ) if( sscanf( sValue.c_str(), "%d", &out ) == 1 )
return true; return true;
@@ -1893,7 +1893,7 @@ bool FromString( const RString &sValue, int &out )
return false; return false;
} }
bool FromString( const RString &sValue, unsigned &out ) template<> bool FromString<unsigned>( const RString &sValue, unsigned &out )
{ {
if( sscanf( sValue.c_str(), "%u", &out ) == 1 ) if( sscanf( sValue.c_str(), "%u", &out ) == 1 )
return true; return true;
@@ -1902,14 +1902,17 @@ bool FromString( const RString &sValue, unsigned &out )
return false; return false;
} }
bool FromString( const RString &sValue, float &out ) template<> bool FromString<float>( const RString &sValue, float &out )
{ {
const char *endptr = sValue.data() + sValue.size(); const char *endptr = sValue.data() + sValue.size();
out = strtof( sValue, (char **) &endptr ); out = strtof( sValue, (char **) &endptr );
return endptr != sValue.data() && isfinite( out ); if( endptr != sValue.data() && isfinite( out ) )
return true;
out = 0;
return false;
} }
bool FromString( const RString &sValue, bool &out ) template<> bool FromString<bool>( const RString &sValue, bool &out )
{ {
if( sValue.size() == 0 ) if( sValue.size() == 0 )
return false; return false;
@@ -1918,22 +1921,22 @@ bool FromString( const RString &sValue, bool &out )
return true; return true;
} }
RString ToString( int value ) template<> RString ToString<int>( const int &value )
{ {
return ssprintf( "%i", value ); return ssprintf( "%i", value );
} }
RString ToString( unsigned value ) template<> RString ToString<unsigned>( const unsigned &value )
{ {
return ssprintf( "%u", value ); return ssprintf( "%u", value );
} }
RString ToString( float value ) template<> RString ToString<float>( const float &value )
{ {
return ssprintf( "%f", value ); return ssprintf( "%f", value );
} }
RString ToString( bool value ) template<> RString ToString<bool>( const bool &value )
{ {
return ssprintf( "%i", value ); return ssprintf( "%i", value );
} }
+20 -10
View File
@@ -483,17 +483,27 @@ void FlushDirCache();
void FixSlashesInPlace( RString &sPath ); void FixSlashesInPlace( RString &sPath );
void CollapsePath( RString &sPath, bool bRemoveLeadingDot=false ); void CollapsePath( RString &sPath, bool bRemoveLeadingDot=false );
bool FromString( const RString &sValue, int &out ); /* If the specified value is not a valid value for this type, and the type
bool FromString( const RString &sValue, unsigned &out ); * has a corresponding "invalid" value, out will be assigned the invalid
bool FromString( const RString &sValue, float &out ); * value if bAllowInvalid is true, or a sensible default if false. (An example
bool FromString( const RString &sValue, bool &out ); * "invalid" sentinels is NULL for pointers.) In either case, false will
inline bool FromString( const RString &sValue, RString &out ) { out = sValue; return true; } * be returned. */
template<typename T>
bool FromString( const RString &sValue, T &out );
RString ToString( int value ); template<> bool FromString<int>( const RString &sValue, int &out );
RString ToString( unsigned value ); template<> bool FromString<unsigned>( const RString &sValue, unsigned &out );
RString ToString( float value ); template<> bool FromString<float>( const RString &sValue, float &out );
RString ToString( bool value ); template<> bool FromString<bool>( const RString &sValue, bool &out );
inline RString ToString( const RString &value ) { return value; } template<> inline bool FromString<RString>( const RString &sValue, RString &out ) { out = sValue; return true; }
template<typename T>
RString ToString( const T &value );
template<> RString ToString<int>( const int &value );
template<> RString ToString<unsigned>( const unsigned &value );
template<> RString ToString<float>( const float &value );
template<> RString ToString<bool>( const bool &value );
template<> inline RString ToString<RString>( const RString &value ) { return value; }
// helper file functions used by Bookkeeper and ProfileManager // helper file functions used by Bookkeeper and ProfileManager
class RageFileBasic; class RageFileBasic;