probably safer: ConvertValue

This commit is contained in:
Glenn Maynard
2006-10-12 02:03:47 +00:00
parent ccfa2f16dd
commit 6c0264620e
4 changed files with 38 additions and 4 deletions
+1 -1
View File
@@ -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<int>(&m_clTestManualCycleCurrent), NUM_CabinetLight );
}
void LightsManager::ChangeTestGameButtonLight( int iDir )
+1 -1
View File
@@ -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<int>(&m_GoalType) );
pNode->GetChildValue( "GoalCalories", m_iGoalCalories );
pNode->GetChildValue( "GoalSeconds", m_iGoalSeconds );
pNode->GetChildValue( "LastPlayedMachineGuid", m_sLastPlayedMachineGuid );
+35 -1
View File
@@ -135,6 +135,40 @@ int &UnionCast( T &e )
return CastSimilarTypes<int,T>(e);
}
/* Helper for ConvertValue(). */
template<typename TO, typename FROM>
struct ConvertValueHelper
{
explicit ConvertValueHelper( FROM *pVal ): m_pFromValue(pVal)
{
m_ToValue = static_cast<TO>( *m_pFromValue );
}
~ConvertValueHelper()
{
*m_pFromValue = static_cast<FROM>( 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<int>(&f) = 12;
*/
template<typename TO, typename FROM>
ConvertValueHelper<TO, FROM> ConvertValue( FROM *pValue )
{
return ConvertValueHelper<TO, FROM>( pValue );
}
/*
* Safely add an integer to an enum.
*
@@ -154,7 +188,7 @@ int &UnionCast( T &e )
template<typename T>
static inline void enum_add( T &val, int iAmt )
{
UnionCast( val ) += iAmt;
*ConvertValue<int>(&val) += iAmt;
}
template<typename T>
+1 -1
View File
@@ -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<int>(&m_iFocusY), NUM_KeyboardRow );
// HACK: Round to nearest option so that we always stop
// on KEYBOARD_ROW_SPECIAL.