make ToString and FromString templates
This commit is contained in:
@@ -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]; \
|
||||
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 XToLocalizedString(X) \
|
||||
@@ -125,7 +126,8 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_
|
||||
if( !s2.CompareNoCase(X##Names[i]) ) \
|
||||
return (X)i; \
|
||||
return (X)(i+1); /*invalid*/ \
|
||||
}
|
||||
} \
|
||||
template<> bool FromString<X>( const RString &sValue, X &out ) { out = StringTo##X(sValue); return out != X##_Invalid; }
|
||||
|
||||
// currently unused
|
||||
#define LuaDeclareType(X)
|
||||
|
||||
@@ -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 )
|
||||
return true;
|
||||
@@ -1893,7 +1893,7 @@ bool FromString( const RString &sValue, int &out )
|
||||
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 )
|
||||
return true;
|
||||
@@ -1902,14 +1902,17 @@ bool FromString( const RString &sValue, unsigned &out )
|
||||
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();
|
||||
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 )
|
||||
return false;
|
||||
@@ -1918,22 +1921,22 @@ bool FromString( const RString &sValue, bool &out )
|
||||
return true;
|
||||
}
|
||||
|
||||
RString ToString( int value )
|
||||
template<> RString ToString<int>( const int &value )
|
||||
{
|
||||
return ssprintf( "%i", value );
|
||||
}
|
||||
|
||||
RString ToString( unsigned value )
|
||||
template<> RString ToString<unsigned>( const unsigned &value )
|
||||
{
|
||||
return ssprintf( "%u", value );
|
||||
}
|
||||
|
||||
RString ToString( float value )
|
||||
template<> RString ToString<float>( const float &value )
|
||||
{
|
||||
return ssprintf( "%f", value );
|
||||
}
|
||||
|
||||
RString ToString( bool value )
|
||||
template<> RString ToString<bool>( const bool &value )
|
||||
{
|
||||
return ssprintf( "%i", value );
|
||||
}
|
||||
|
||||
+20
-10
@@ -483,17 +483,27 @@ void FlushDirCache();
|
||||
void FixSlashesInPlace( RString &sPath );
|
||||
void CollapsePath( RString &sPath, bool bRemoveLeadingDot=false );
|
||||
|
||||
bool FromString( const RString &sValue, int &out );
|
||||
bool FromString( const RString &sValue, unsigned &out );
|
||||
bool FromString( const RString &sValue, float &out );
|
||||
bool FromString( const RString &sValue, bool &out );
|
||||
inline bool FromString( const RString &sValue, RString &out ) { out = sValue; return true; }
|
||||
/* If the specified value is not a valid value for this type, and the type
|
||||
* has a corresponding "invalid" value, out will be assigned the invalid
|
||||
* value if bAllowInvalid is true, or a sensible default if false. (An example
|
||||
* "invalid" sentinels is NULL for pointers.) In either case, false will
|
||||
* be returned. */
|
||||
template<typename T>
|
||||
bool FromString( const RString &sValue, T &out );
|
||||
|
||||
RString ToString( int value );
|
||||
RString ToString( unsigned value );
|
||||
RString ToString( float value );
|
||||
RString ToString( bool value );
|
||||
inline RString ToString( const RString &value ) { return value; }
|
||||
template<> bool FromString<int>( const RString &sValue, int &out );
|
||||
template<> bool FromString<unsigned>( const RString &sValue, unsigned &out );
|
||||
template<> bool FromString<float>( const RString &sValue, float &out );
|
||||
template<> bool FromString<bool>( const RString &sValue, bool &out );
|
||||
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
|
||||
class RageFileBasic;
|
||||
|
||||
Reference in New Issue
Block a user