From 6c0264620eb0b38bc6e1c072e230592bed0b7c5e Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 12 Oct 2006 02:03:47 +0000 Subject: [PATCH] probably safer: ConvertValue --- stepmania/src/LightsManager.cpp | 2 +- stepmania/src/Profile.cpp | 2 +- stepmania/src/RageUtil.h | 36 ++++++++++++++++++++++++++++++- stepmania/src/ScreenTextEntry.cpp | 2 +- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/stepmania/src/LightsManager.cpp b/stepmania/src/LightsManager.cpp index 53c083cd88..e8b5ef1236 100644 --- a/stepmania/src/LightsManager.cpp +++ b/stepmania/src/LightsManager.cpp @@ -426,7 +426,7 @@ void LightsManager::ChangeTestCabinetLight( int iDir ) m_iControllerTestManualCycleCurrent = -1; m_clTestManualCycleCurrent = (CabinetLight)(m_clTestManualCycleCurrent+iDir); - wrap( UnionCast(m_clTestManualCycleCurrent), NUM_CabinetLight ); + wrap( *ConvertValue(&m_clTestManualCycleCurrent), NUM_CabinetLight ); } void LightsManager::ChangeTestGameButtonLight( int iDir ) diff --git a/stepmania/src/Profile.cpp b/stepmania/src/Profile.cpp index 8c07fd242f..38d0f460bc 100644 --- a/stepmania/src/Profile.cpp +++ b/stepmania/src/Profile.cpp @@ -1161,7 +1161,7 @@ void Profile::LoadGeneralDataFromNode( const XNode* pNode ) pNode->GetChildValue( "TotalGameplaySeconds", m_iTotalGameplaySeconds ); pNode->GetChildValue( "CurrentCombo", m_iCurrentCombo ); pNode->GetChildValue( "TotalCaloriesBurned", m_fTotalCaloriesBurned ); - pNode->GetChildValue( "GoalType", UnionCast(m_GoalType) ); + pNode->GetChildValue( "GoalType", *ConvertValue(&m_GoalType) ); pNode->GetChildValue( "GoalCalories", m_iGoalCalories ); pNode->GetChildValue( "GoalSeconds", m_iGoalSeconds ); pNode->GetChildValue( "LastPlayedMachineGuid", m_sLastPlayedMachineGuid ); diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index 4afa55b560..a712f9dfd9 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -135,6 +135,40 @@ int &UnionCast( T &e ) return CastSimilarTypes(e); } +/* Helper for ConvertValue(). */ +template +struct ConvertValueHelper +{ + explicit ConvertValueHelper( FROM *pVal ): m_pFromValue(pVal) + { + m_ToValue = static_cast( *m_pFromValue ); + } + + ~ConvertValueHelper() + { + *m_pFromValue = static_cast( m_ToValue ); + } + + TO &operator *() { return m_ToValue; } + operator TO *() { return &m_ToValue; } + +private: + FROM *m_pFromValue; + TO m_ToValue; +}; + +/* + * Safely temporarily convert between types. For example, + * + * float f = 10.5; + * *ConvertValue(&f) = 12; + */ +template +ConvertValueHelper ConvertValue( FROM *pValue ) +{ + return ConvertValueHelper( pValue ); +} + /* * Safely add an integer to an enum. * @@ -154,7 +188,7 @@ int &UnionCast( T &e ) template static inline void enum_add( T &val, int iAmt ) { - UnionCast( val ) += iAmt; + *ConvertValue(&val) += iAmt; } template diff --git a/stepmania/src/ScreenTextEntry.cpp b/stepmania/src/ScreenTextEntry.cpp index cdafaf5b23..baffeafbab 100644 --- a/stepmania/src/ScreenTextEntry.cpp +++ b/stepmania/src/ScreenTextEntry.cpp @@ -406,7 +406,7 @@ void ScreenTextEntryVisual::MoveY( int iDir ) do { m_iFocusY = enum_add2( m_iFocusY, +iDir ); - wrap( UnionCast(m_iFocusY), NUM_KeyboardRow ); + wrap( *ConvertValue(&m_iFocusY), NUM_KeyboardRow ); // HACK: Round to nearest option so that we always stop // on KEYBOARD_ROW_SPECIAL.