1
1
forked from 0ad/0ad

Remove both copies of the 15-fold copies of the JSI_IGUIObject::getProperty and setProperty setting case handlers following the FromJSVal / ToJSVal template specializations implemented in

* JSI_GUIColor in 9be8a560a9
* bool in eec179a9a8
* CGUISeries and CGUIList in 26ae9d430a
* primitives in 636c719110
* EAlign and EVAlign in 0834d07462
* ClientArea in b9f3c8557b
* CColor in 415939b59b
* CGUISpriteInstance 0a7d0ecdde
* CPos in d6b93b3be6

The removed JS_ReportError calls were redundant.

This was SVN commit r22574.
This commit is contained in:
elexis 2019-07-29 14:46:29 +00:00
parent d6b93b3be6
commit 82f1d2718b
3 changed files with 46 additions and 318 deletions

View File

@ -27,6 +27,24 @@
#include "ps/Profile.h"
#include "scriptinterface/ScriptInterface.h"
template<typename T>
void SGUISetting::Init(IGUIObject& pObject, const CStr& Name)
{
m_pSetting = new T();
m_FromJSVal = [Name, &pObject](JSContext* cx, JS::HandleValue v) {
T value;
if (!ScriptInterface::FromJSVal<T>(cx, v, value))
return false;
GUI<T>::SetSetting(&pObject, Name, value);
return true;
};
m_ToJSVal = [Name, this](JSContext* cx, JS::MutableHandleValue v) {
ScriptInterface::ToJSVal<T>(cx, v, *static_cast<T*>(m_pSetting));
};
}
IGUIObject::IGUIObject()
: m_pGUI(NULL), m_pParent(NULL), m_MouseHovering(false), m_LastClickTime()
@ -151,7 +169,7 @@ void IGUIObject::AddSetting(const EGUISettingType& Type, const CStr& Name)
{
#define TYPE(type) \
case GUIST_##type: \
m_Settings[Name].m_pSetting = new type(); \
m_Settings[Name].Init<type>(*this, Name);\
break;
// Construct the setting.

View File

@ -32,6 +32,7 @@
#include "lib/input.h" // just for IN_PASS
#include "ps/XML/Xeromyces.h"
#include <functional>
#include <string>
#include <vector>
@ -68,8 +69,25 @@ struct SGUISetting
{
SGUISetting() : m_pSetting(NULL) {}
/**
* Stores the instance of the setting type holding the setting data. Can be set from XML and JS.
*/
void *m_pSetting;
EGUISettingType m_Type;
template<typename T>
void Init(IGUIObject& pObject, const CStr& Name);
/**
* Parses the given JS::Value using ScriptInterface::FromJSVal and assigns it to the setting data.
*/
std::function<bool(JSContext* cx, JS::HandleValue v)> m_FromJSVal;
/**
* Converts the setting data to a JS::Value using ScriptInterface::ToJSVal.
*/
std::function<void(JSContext* cx, JS::MutableHandleValue v)> m_ToJSVal;
};
/**

View File

@ -114,147 +114,14 @@ bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::Handle
ScriptInterface::ToJSVal(cx, vp, e->GetName());
return true;
}
else
else if (e->SettingExists(propName))
{
// Retrieve the setting's type (and make sure it actually exists)
EGUISettingType Type;
if (e->GetSettingType(propName, Type) != PSRETURN_OK)
{
JS_ReportError(cx, "Invalid GUIObject property '%s'", propName.c_str());
return false;
}
// (All the cases are in {...} to avoid scoping problems)
switch (Type)
{
case GUIST_bool:
{
bool value;
GUI<bool>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_int:
{
i32 value;
GUI<i32>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_uint:
{
u32 value;
GUI<u32>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_float:
{
float value;
GUI<float>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CGUIColor:
{
CGUIColor value;
GUI<CGUIColor>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CPos:
{
CPos value;
GUI<CPos>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CClientArea:
{
CClientArea value;
GUI<CClientArea>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CGUIString:
{
CGUIString value;
GUI<CGUIString>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CStr:
{
CStr value;
GUI<CStr>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CStrW:
{
CStrW value;
GUI<CStrW>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CGUISpriteInstance:
{
CGUISpriteInstance* value;
GUI<CGUISpriteInstance>::GetSettingPointer(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, *value);
break;
}
case GUIST_EAlign:
{
EAlign value;
GUI<EAlign>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_EVAlign:
{
EVAlign value;
GUI<EVAlign>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CGUIList:
{
CGUIList value;
GUI<CGUIList>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
case GUIST_CGUISeries:
{
CGUISeries value;
GUI<CGUISeries>::GetSetting(e, propName, value);
ScriptInterface::ToJSVal(cx, vp, value);
break;
}
default:
JS_ReportError(cx, "Setting '%s' uses an unimplemented type", propName.c_str());
DEBUG_WARN_ERR(ERR::LOGIC);
return false;
}
e->m_Settings[propName].m_ToJSVal(cx, vp);
return true;
}
JS_ReportError(cx, "Property '%s' does not exist!", propName.c_str());
return false;
}
bool JSI_IGUIObject::setProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, bool UNUSED(strict), JS::MutableHandleValue vp)
@ -300,186 +167,11 @@ bool JSI_IGUIObject::setProperty(JSContext* cx, JS::HandleObject obj, JS::Handle
return true;
}
// Retrieve the setting's type (and make sure it actually exists)
EGUISettingType Type;
if (e->GetSettingType(propName, Type) != PSRETURN_OK)
{
JS_ReportError(cx, "Invalid setting '%s'", propName.c_str());
return true;
}
if (e->SettingExists(propName))
return e->m_Settings[propName].m_FromJSVal(cx, vp);
switch (Type)
{
case GUIST_CStr:
{
CStr value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CStr>::SetSetting(e, propName, value);
break;
}
case GUIST_CStrW:
{
CStrW value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CStrW>::SetSetting(e, propName, value);
break;
}
case GUIST_CGUISpriteInstance:
{
CGUISpriteInstance value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CGUISpriteInstance>::SetSetting(e, propName, value);
break;
}
case GUIST_CGUIString:
{
CGUIString value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CGUIString>::SetSetting(e, propName, value);
break;
}
case GUIST_EAlign:
{
EAlign a;
if (!ScriptInterface::FromJSVal(cx, vp, a))
return false;
GUI<EAlign>::SetSetting(e, propName, a);
break;
}
case GUIST_EVAlign:
{
EVAlign a;
if (!ScriptInterface::FromJSVal(cx, vp, a))
return false;
GUI<EVAlign>::SetSetting(e, propName, a);
break;
}
case GUIST_int:
{
i32 value;
if (ScriptInterface::FromJSVal(cx, vp, value))
GUI<i32>::SetSetting(e, propName, value);
else
{
JS_ReportError(cx, "Cannot convert value to i32");
return false;
}
break;
}
case GUIST_uint:
{
u32 value;
if (ScriptInterface::FromJSVal(cx, vp, value))
GUI<u32>::SetSetting(e, propName, value);
else
{
JS_ReportError(cx, "Cannot convert value to u32");
return false;
}
break;
}
case GUIST_float:
{
float value;
if (ScriptInterface::FromJSVal(cx, vp, value))
GUI<float>::SetSetting(e, propName, value);
else
{
JS_ReportError(cx, "Cannot convert value to float");
return false;
}
break;
}
case GUIST_bool:
{
bool value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<bool>::SetSetting(e, propName, value);
break;
}
case GUIST_CClientArea:
{
CClientArea value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CClientArea>::SetSetting(e, propName, value);
break;
}
case GUIST_CPos:
{
CPos value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CPos>::SetSetting(e, propName, value);
break;
}
case GUIST_CGUIColor:
{
CGUIColor value;
if (!ScriptInterface::FromJSVal(cx, vp, value))
return false;
GUI<CGUIColor>::SetSetting(e, propName, value);
break;
}
case GUIST_CGUIList:
{
CGUIList list;
if (ScriptInterface::FromJSVal(cx, vp, list))
GUI<CGUIList>::SetSetting(e, propName, list);
else
{
JS_ReportError(cx, "Failed to get list '%s'", propName.c_str());
return false;
}
break;
}
case GUIST_CGUISeries:
{
CGUISeries series;
if (ScriptInterface::FromJSVal(cx, vp, series))
GUI<CGUISeries>::SetSetting(e, propName, series);
else
{
JS_ReportError(cx, "Invalid value for chart series '%s'", propName.c_str());
return false;
}
break;
}
default:
JS_ReportError(cx, "Setting '%s' uses an unimplemented type", propName.c_str());
break;
}
return !JS_IsExceptionPending(cx);
JS_ReportError(cx, "Property '%s' does not exist!", propName.c_str());
return false;
}
void JSI_IGUIObject::init(ScriptInterface& scriptInterface)