1
0
forked from 0ad/0ad

Reduced GUI's exception usage (=> shortened debug-mode start time by a couple of seconds)

This was SVN commit r1494.
This commit is contained in:
Ykkrosh 2004-12-13 12:07:12 +00:00
parent 114f3f1513
commit 80b1876b77
4 changed files with 172 additions and 184 deletions

View File

@ -578,7 +578,7 @@ void CGUI::AddObject(IGUIObject* pObject)
GUI<CGUI*>::RecurseObject(0, pObject, &IGUIObject::SetGUI, this);
// Add child to base object
m_BaseObject->AddChild(pObject);
m_BaseObject->AddChild(pObject); // can throw
// Cache tree
GUI<>::RecurseObject(0, pObject, &IGUIObject::UpdateCachedSize);
@ -1301,13 +1301,8 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
}
// Try setting the value
try
if (object->SetSetting(pFile->getAttributeString(attr.Name), (CStr)attr.Value) != PS_OK)
{
object->SetSetting(pFile->getAttributeString(attr.Name), (CStr)attr.Value);
}
catch (PS_RESULT e)
{
UNUSED(e);
ReportParseError("Can't set \"%s\" to \"%s\"", pFile->getAttributeString(attr.Name).c_str(), CStr(attr.Value).c_str());
// This is not a fatal error
@ -1328,15 +1323,10 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
CStrW caption (Element.getText());
if (caption.Length())
{
try
{
// Set the setting caption to this
object->SetSetting("caption", caption);
}
catch (PS_RESULT)
{
// There is no harm if the object didn't have a "caption"
}
// Set the setting caption to this
object->SetSetting("caption", caption);
// There is no harm if the object didn't have a "caption"
}

View File

@ -238,16 +238,16 @@ bool IGUIObject::SettingExists(const CStr& Setting) const
{ \
type _Value; \
if (!GUI<type>::ParseString(Value, _Value)) \
throw PS_FAIL; \
return PS_FAIL; \
\
GUI<type>::SetSetting(this, Setting, _Value); \
}
void IGUIObject::SetSetting(const CStr& Setting, const CStr& Value)
PS_RESULT IGUIObject::SetSetting(const CStr& Setting, const CStr& Value)
{
if (!SettingExists(Setting))
{
throw PS_FAIL;
return PS_FAIL;
}
// Get setting
@ -268,10 +268,14 @@ void IGUIObject::SetSetting(const CStr& Setting, const CStr& Value)
ADD_TYPE(EVAlign)
else
{
throw PS_FAIL;
return PS_FAIL;
}
return PS_OK;
}
#undef ADD_TYPE
PS_RESULT IGUIObject::GetSettingType(const CStr& Setting, EGUISettingType &Type) const
{
if (!SettingExists(Setting))
@ -357,19 +361,14 @@ void IGUIObject::LoadStyle(const SGUIStyle &Style)
for (cit = Style.m_SettingsDefaults.begin(); cit != Style.m_SettingsDefaults.end(); ++cit)
{
// Try set setting in object
try
{
SetSetting(cit->first, cit->second);
}
SetSetting(cit->first, cit->second);
// It doesn't matter if it fail, it's not suppose to be able to set every setting.
// since it's generic.
catch (PS_RESULT e)
{
UNUSED(e);
// The beauty with styles is that it can contain more settings
// than exists for the objects using it. So if the SetSetting
// fails, don't care.
}
// The beauty with styles is that it can contain more settings
// than exists for the objects using it. So if the SetSetting
// fails, don't care.
}
}

View File

@ -259,9 +259,9 @@ public:
* @param Setting Setting by name
* @param Value Value to set to
*
* @throws PS_RESULT
* @return PS_RESULT (PS_OK if successful)
*/
void SetSetting(const CStr& Setting, const CStr& Value);
PS_RESULT SetSetting(const CStr& Setting, const CStr& Value);
/**
* Retrieves the type of a named setting.

View File

@ -207,160 +207,159 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval
{
CStr propValue = JS_GetStringBytes(JS_ValueToString(cx, *vp));
e->SetName(propValue);
return JS_TRUE;
}
else
EGUISettingType Type;
if (e->GetSettingType(propName, Type) != PS_OK)
{
EGUISettingType Type;
if (e->GetSettingType(propName, Type) != PS_OK)
{
JS_ReportError(cx, "Invalid setting '%s'", propName.c_str());
return JS_TRUE;
}
try
{
switch (Type)
{
case GUIST_CStr:
{
CStr value = JS_GetStringBytes(JS_ValueToString(cx, *vp));
GUI<CStr>::SetSetting(e, propName, value);
break;
}
case GUIST_CStrW:
{
utf16string value (JS_GetStringChars(JS_ValueToString(cx, *vp)));
GUI<CStrW>::SetSetting(e, propName, value);
break;
}
case GUIST_CGUIString:
{
std::wstring value;
StringConvert::jsstring_to_wstring(JS_ValueToString(cx, *vp), value);
CGUIString str;
str.SetValue(value);
GUI<CGUIString>::SetSetting(e, propName, str);
break;
}
case GUIST_int:
{
int32 value;
if (JS_ValueToInt32(cx, *vp, &value) == JS_TRUE)
GUI<int>::SetSetting(e, propName, value);
else
{
JS_ReportError(cx, "Cannot convert value to int");
return JS_FALSE;
}
break;
}
case GUIST_float:
{
jsdouble value;
if (JS_ValueToNumber(cx, *vp, &value) == JS_TRUE)
GUI<float>::SetSetting(e, propName, (float)value);
else
{
JS_ReportError(cx, "Cannot convert value to float");
return JS_FALSE;
}
break;
}
case GUIST_bool:
{
JSBool value;
if (JS_ValueToBoolean(cx, *vp, &value) == JS_TRUE)
GUI<bool>::SetSetting(e, propName, value||0); // ||0 to avoid int-to-bool compiler warnings
else
{
JS_ReportError(cx, "Cannot convert value to bool");
return JS_FALSE;
}
break;
}
case GUIST_CClientArea:
{
if (JSVAL_IS_STRING(*vp))
{
e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp)) );
}
else if (JSVAL_IS_OBJECT(*vp) && JS_GetClass(JSVAL_TO_OBJECT(*vp)) == &JSI_GUISize::JSI_class)
{
CClientArea area;
GUI<CClientArea>::GetSetting(e, propName, area);
JSObject* obj = JSVAL_TO_OBJECT(*vp);
#define P(x, y, z) area.x.y = (float)g_ScriptingHost.GetObjectProperty_Double(obj, #z)
P(pixel, left, left);
P(pixel, top, top);
P(pixel, right, right);
P(pixel, bottom, bottom);
P(percent, left, rleft);
P(percent, top, rtop);
P(percent, right, rright);
P(percent, bottom, rbottom);
#undef P
GUI<CClientArea>::SetSetting(e, propName, area);
}
else
{
JS_ReportError(cx, "Size only accepts strings or GUISize objects");
return JS_FALSE;
}
break;
}
case GUIST_CColor:
{
if (JSVAL_IS_STRING(*vp))
{
e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp)) );
}
else if (JSVAL_IS_OBJECT(*vp) && JS_GetClass(JSVAL_TO_OBJECT(*vp)) == &JSI_GUIColor::JSI_class)
{
CColor colour;
JSObject* obj = JSVAL_TO_OBJECT(*vp);
jsval t; double s;
#define PROP(x) JS_GetProperty(cx, obj, #x, &t); \
JS_ValueToNumber(cx, t, &s); \
colour.x = (float)s
PROP(r); PROP(g); PROP(b); PROP(a);
#undef PROP
GUI<CColor>::SetSetting(e, propName, colour);
}
else
{
JS_ReportError(cx, "Color only accepts strings or GUIColor objects");
return JS_FALSE;
}
break;
}
// TODO Gee: (2004-09-01) EAlign and EVAlign too.
default:
JS_ReportError(cx, "Setting '%s' uses an unimplemented type", propName.c_str());
break;
}
}
// Catch failed calls to SetSetting (the string and templated versions)
catch (PS_RESULT)
{
JS_ReportError(cx, "Invalid value for setting '%s'", propName.c_str());
return JS_FALSE;
}
JS_ReportError(cx, "Invalid setting '%s'", propName.c_str());
return JS_TRUE;
}
switch (Type)
{
case GUIST_CStr:
{
CStr value = JS_GetStringBytes(JS_ValueToString(cx, *vp));
GUI<CStr>::SetSetting(e, propName, value);
break;
}
case GUIST_CStrW:
{
utf16string value (JS_GetStringChars(JS_ValueToString(cx, *vp)));
GUI<CStrW>::SetSetting(e, propName, value);
break;
}
case GUIST_CGUIString:
{
std::wstring value;
StringConvert::jsstring_to_wstring(JS_ValueToString(cx, *vp), value);
CGUIString str;
str.SetValue(value);
GUI<CGUIString>::SetSetting(e, propName, str);
break;
}
case GUIST_int:
{
int32 value;
if (JS_ValueToInt32(cx, *vp, &value) == JS_TRUE)
GUI<int>::SetSetting(e, propName, value);
else
{
JS_ReportError(cx, "Cannot convert value to int");
return JS_FALSE;
}
break;
}
case GUIST_float:
{
jsdouble value;
if (JS_ValueToNumber(cx, *vp, &value) == JS_TRUE)
GUI<float>::SetSetting(e, propName, (float)value);
else
{
JS_ReportError(cx, "Cannot convert value to float");
return JS_FALSE;
}
break;
}
case GUIST_bool:
{
JSBool value;
if (JS_ValueToBoolean(cx, *vp, &value) == JS_TRUE)
GUI<bool>::SetSetting(e, propName, value||0); // ||0 to avoid int-to-bool compiler warnings
else
{
JS_ReportError(cx, "Cannot convert value to bool");
return JS_FALSE;
}
break;
}
case GUIST_CClientArea:
{
if (JSVAL_IS_STRING(*vp))
{
if (e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp))) != PS_OK)
{
JS_ReportError(cx, "Invalid value for setting '%s'", propName.c_str());
return JS_FALSE;
}
}
else if (JSVAL_IS_OBJECT(*vp) && JS_GetClass(JSVAL_TO_OBJECT(*vp)) == &JSI_GUISize::JSI_class)
{
CClientArea area;
GUI<CClientArea>::GetSetting(e, propName, area);
JSObject* obj = JSVAL_TO_OBJECT(*vp);
#define P(x, y, z) area.x.y = (float)g_ScriptingHost.GetObjectProperty_Double(obj, #z)
P(pixel, left, left);
P(pixel, top, top);
P(pixel, right, right);
P(pixel, bottom, bottom);
P(percent, left, rleft);
P(percent, top, rtop);
P(percent, right, rright);
P(percent, bottom, rbottom);
#undef P
GUI<CClientArea>::SetSetting(e, propName, area);
}
else
{
JS_ReportError(cx, "Size only accepts strings or GUISize objects");
return JS_FALSE;
}
break;
}
case GUIST_CColor:
{
if (JSVAL_IS_STRING(*vp))
{
if (e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp))) != PS_OK)
{
JS_ReportError(cx, "Invalid value for setting '%s'", propName.c_str());
return JS_FALSE;
}
}
else if (JSVAL_IS_OBJECT(*vp) && JS_GetClass(JSVAL_TO_OBJECT(*vp)) == &JSI_GUIColor::JSI_class)
{
CColor colour;
JSObject* obj = JSVAL_TO_OBJECT(*vp);
jsval t; double s;
#define PROP(x) JS_GetProperty(cx, obj, #x, &t); \
JS_ValueToNumber(cx, t, &s); \
colour.x = (float)s
PROP(r); PROP(g); PROP(b); PROP(a);
#undef PROP
GUI<CColor>::SetSetting(e, propName, colour);
}
else
{
JS_ReportError(cx, "Color only accepts strings or GUIColor objects");
return JS_FALSE;
}
break;
}
// TODO Gee: (2004-09-01) EAlign and EVAlign too.
default:
JS_ReportError(cx, "Setting '%s' uses an unimplemented type", propName.c_str());
break;
}
return JS_TRUE;
}