From 80b1876b7780c8e571d23badf335902c097fc5a7 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Mon, 13 Dec 2004 12:07:12 +0000 Subject: [PATCH] Reduced GUI's exception usage (=> shortened debug-mode start time by a couple of seconds) This was SVN commit r1494. --- source/gui/CGUI.cpp | 22 +- source/gui/IGUIObject.cpp | 29 +- source/gui/IGUIObject.h | 4 +- .../gui/scripting/JSInterface_IGUIObject.cpp | 301 +++++++++--------- 4 files changed, 172 insertions(+), 184 deletions(-) diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index ea30d0dab9..5f8d1e365a 100755 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -578,7 +578,7 @@ void CGUI::AddObject(IGUIObject* pObject) GUI::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" } diff --git a/source/gui/IGUIObject.cpp b/source/gui/IGUIObject.cpp index 224f7268f9..b0996154af 100755 --- a/source/gui/IGUIObject.cpp +++ b/source/gui/IGUIObject.cpp @@ -238,16 +238,16 @@ bool IGUIObject::SettingExists(const CStr& Setting) const { \ type _Value; \ if (!GUI::ParseString(Value, _Value)) \ - throw PS_FAIL; \ + return PS_FAIL; \ \ GUI::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. } } diff --git a/source/gui/IGUIObject.h b/source/gui/IGUIObject.h index a4ba84d41f..d02dd6b305 100755 --- a/source/gui/IGUIObject.h +++ b/source/gui/IGUIObject.h @@ -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. diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index fbebd30ce4..d5134e76bc 100755 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -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::SetSetting(e, propName, value); - break; - } - - case GUIST_CStrW: - { - utf16string value (JS_GetStringChars(JS_ValueToString(cx, *vp))); - GUI::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::SetSetting(e, propName, str); - break; - } - - case GUIST_int: - { - int32 value; - if (JS_ValueToInt32(cx, *vp, &value) == JS_TRUE) - GUI::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::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::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::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::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::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::SetSetting(e, propName, value); + break; + } + + case GUIST_CStrW: + { + utf16string value (JS_GetStringChars(JS_ValueToString(cx, *vp))); + GUI::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::SetSetting(e, propName, str); + break; + } + + case GUIST_int: + { + int32 value; + if (JS_ValueToInt32(cx, *vp, &value) == JS_TRUE) + GUI::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::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::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::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::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::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; }