diff --git a/stepmania/src/EnumHelper.h b/stepmania/src/EnumHelper.h index a35c5eed5f..8a3af3fd8c 100644 --- a/stepmania/src/EnumHelper.h +++ b/stepmania/src/EnumHelper.h @@ -98,7 +98,8 @@ const RString &EnumToString( int iVal, int iMax, const char **szNameArray, auto_ { \ static auto_ptr as_##X##Name[NUM_##X+2]; \ return EnumToString( x, NUM_##X, X##Names, as_##X##Name ); \ - } + } \ + template<> RString ToString( 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( const RString &sValue, X &out ) { out = StringTo##X(sValue); return out != X##_Invalid; } // currently unused #define LuaDeclareType(X) diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 28de52e2dd..58998114b2 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -1884,7 +1884,7 @@ void CollapsePath( RString &sPath, bool bRemoveLeadingDot ) } -bool FromString( const RString &sValue, int &out ) +template<> bool FromString( 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( 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( 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( 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( const int &value ) { return ssprintf( "%i", value ); } -RString ToString( unsigned value ) +template<> RString ToString( const unsigned &value ) { return ssprintf( "%u", value ); } -RString ToString( float value ) +template<> RString ToString( const float &value ) { return ssprintf( "%f", value ); } -RString ToString( bool value ) +template<> RString ToString( const bool &value ) { return ssprintf( "%i", value ); } diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index d75dcf53d1..e9272e5f40 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -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 +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( const RString &sValue, int &out ); +template<> bool FromString( const RString &sValue, unsigned &out ); +template<> bool FromString( const RString &sValue, float &out ); +template<> bool FromString( const RString &sValue, bool &out ); +template<> inline bool FromString( const RString &sValue, RString &out ) { out = sValue; return true; } + +template +RString ToString( const T &value ); +template<> RString ToString( const int &value ); +template<> RString ToString( const unsigned &value ); +template<> RString ToString( const float &value ); +template<> RString ToString( const bool &value ); +template<> inline RString ToString( const RString &value ) { return value; } // helper file functions used by Bookkeeper and ProfileManager class RageFileBasic;