Type checking in dangerous pointer-using code
This was SVN commit r1113.
This commit is contained in:
parent
78934b50c1
commit
9b1127d82f
@ -367,3 +367,25 @@ void CInternalCGUIAccessorBase::HandleMessage(IGUIObject *pObject, const SGUIMes
|
||||
{
|
||||
pObject->HandleMessage(message);
|
||||
}
|
||||
|
||||
|
||||
//#ifndef NDEBUG
|
||||
#define TYPE(T) \
|
||||
template<> void CheckType<T>(const IGUIObject* obj, const CStr& setting) { \
|
||||
if (((IGUIObject*)obj)->m_Settings[setting].m_Type != GUIST_##T) \
|
||||
{ \
|
||||
debug_warn("EXCESSIVELY FATAL ERROR: Inconsistent types in GUI"); \
|
||||
throw "EXCESSIVELY FATAL ERROR: Inconsistent types in GUI"; /* TODO: better reporting */ \
|
||||
} \
|
||||
}
|
||||
TYPE(bool)
|
||||
TYPE(int)
|
||||
TYPE(float)
|
||||
TYPE(CClientArea)
|
||||
TYPE(CStr)
|
||||
TYPE(CColor)
|
||||
TYPE(CGUIString)
|
||||
TYPE(EAlign)
|
||||
TYPE(EVAlign)
|
||||
#undef TYPE
|
||||
//#endif // #ifndef NDEBUG
|
||||
|
@ -152,6 +152,12 @@ protected:
|
||||
};
|
||||
|
||||
|
||||
//#ifndef NDEBUG
|
||||
// Used to ensure type-safety, sort of
|
||||
template<typename T> void CheckType(const IGUIObject* obj, const CStr& setting);
|
||||
//#endif
|
||||
|
||||
|
||||
/**
|
||||
* @author Gustav Larsson
|
||||
*
|
||||
@ -188,7 +194,9 @@ public:
|
||||
if (!pObject->m_Settings.find(Setting)->second.m_pSetting)
|
||||
return PS_FAIL;
|
||||
|
||||
// Set value
|
||||
CheckType<T>(pObject, Setting);
|
||||
|
||||
// Get value
|
||||
Value = *(T*)pObject->m_Settings.find(Setting)->second.m_pSetting;
|
||||
|
||||
return PS_OK;
|
||||
@ -212,6 +220,8 @@ public:
|
||||
if (!pObject->SettingExists(Setting))
|
||||
return PS_SETTING_FAIL;
|
||||
|
||||
CheckType<T>(pObject, Setting);
|
||||
|
||||
// Set value
|
||||
*(T*)pObject->m_Settings[Setting].m_pSetting = Value;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user