History

Fixes to most scripting-related warnings

ksuprynowicz committed 4 years ago

Commit 6f321731 parent a14c64435 files · +195 −149Browse files

Showing the changes to libraries/script-engine/src/ScriptEngineCast.h (1 of 35 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngineCast.h+34−8
@@ -66,8 +66,39 @@ inline QVariant scriptvalue_cast<QVariant>(const ScriptValue& value) { · 65 unmodified lines
6666 return value.toVariant();
6767 }
6868
69+//#define MARSHAL_MACRO(FUNCTION, TYPE) +[FUNCTION](ScriptEngine* engine, const void* p) -> ScriptValue { FUNCTION(engine, *(static_cast<const TYPE*>(p)) ); }
70+
71+template <typename T, ScriptValue (*f)(ScriptEngine*, const T&)>
72+ScriptValue toScriptValueWrapper(ScriptEngine* engine, const void *p) {
73+ Q_ASSERT(p != NULL);
74+ auto &src = *(reinterpret_cast<const T*>(p));
75+ return f(engine, src);
76+}
77+
78+template <typename T, bool (*f)(const ScriptValue&, T&)>
79+bool fromScriptValueWrapper(const ScriptValue& val, void* p) {
80+ Q_ASSERT(p != NULL);
81+ auto &dest = *(reinterpret_cast<T*>(p));
82+ return f(val, dest);
83+}
84+
85+template <typename T, ScriptValue (*toScriptValue)(ScriptEngine*, const T&), bool (*fromScriptValue)(const ScriptValue&, T&)>
86+int scriptRegisterMetaType(ScriptEngine* eng, const char* name = "",
87+ T* = 0)
88+{
89+ int id;
90+ if (strlen(name) > 0) { // make sure it's registered
91+ id = qRegisterMetaType<T>(name);
92+ } else {
93+ id = qRegisterMetaType<T>();
94+ }
95+ eng->registerCustomType(id, toScriptValueWrapper<T, toScriptValue>,
96+ fromScriptValueWrapper<T, fromScriptValue>);
97+ return id;
98+}
99+
69100 template <typename T>
70−int scriptRegisterMetaType(ScriptEngine* eng,
101+int scriptRegisterMetaTypeWithLambdas(ScriptEngine* eng,
71102 ScriptValue (*toScriptValue)(ScriptEngine*, const T& t),
72103 bool (*fromScriptValue)(const ScriptValue&, T& t), const char* name = "",
73104 T* = 0)
@@ -76,12 +107,7 @@ int scriptRegisterMetaType(ScriptEngine* eng, · 2 unmodified lines
76107 if (strlen(name) > 0) { // make sure it's registered
77108 id = qRegisterMetaType<T>(name);
78109 } else {
79− //if (!QMetaType::fromType<T>().name().isNull()) {
80− //qDebug() << "scriptRegisterMetaType: " << QMetaType::fromType<T>().name();
81− //id = qRegisterMetaType<T>(QMetaType::fromType<T>().name());
82− //}else{
83− id = qRegisterMetaType<T>();
84− //}
110+ id = qRegisterMetaType<T>();
85111 }
86112 eng->registerCustomType(id, reinterpret_cast<ScriptEngine::MarshalFunction>(toScriptValue),
87113 reinterpret_cast<ScriptEngine::DemarshalFunction>(fromScriptValue));
@@ -148,7 +174,7 @@ bool scriptValueToEnumClass(const ScriptValue& value, T& enumValue) { · 60 unmodified lines
148174 template <typename T>
149175 int scriptRegisterSequenceMetaType(ScriptEngine* engine,
150176 T* = 0) {
151− return scriptRegisterMetaType<T>(engine, scriptValueFromSequence, scriptValueToSequence);
177+ return scriptRegisterMetaType<T, scriptValueFromSequence, scriptValueToSequence>(engine);
152178 }
153179
154180 #endif // hifi_ScriptEngineCast_h