Showing the changes to libraries/script-engine/src/ScriptEngineCast.h (1 of 35 files). Show all
Unified · Split
| @@ -66,8 +66,39 @@ inline QVariant scriptvalue_cast<QVariant>(const ScriptValue& value) { · 65 unmodified lines | ||
| 66 | 66 | return value.toVariant(); |
| 67 | 67 | } |
| 68 | 68 | |
| 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 | + | |
| 69 | 100 | template <typename T> |
| 70 | −int scriptRegisterMetaType(ScriptEngine* eng, | |
| 101 | +int scriptRegisterMetaTypeWithLambdas(ScriptEngine* eng, | |
| 71 | 102 | ScriptValue (*toScriptValue)(ScriptEngine*, const T& t), |
| 72 | 103 | bool (*fromScriptValue)(const ScriptValue&, T& t), const char* name = "", |
| 73 | 104 | T* = 0) |
| @@ -76,12 +107,7 @@ int scriptRegisterMetaType(ScriptEngine* eng, · 2 unmodified lines | ||
| 76 | 107 | if (strlen(name) > 0) { // make sure it's registered |
| 77 | 108 | id = qRegisterMetaType<T>(name); |
| 78 | 109 | } 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>(); | |
| 85 | 111 | } |
| 86 | 112 | eng->registerCustomType(id, reinterpret_cast<ScriptEngine::MarshalFunction>(toScriptValue), |
| 87 | 113 | reinterpret_cast<ScriptEngine::DemarshalFunction>(fromScriptValue)); |
| @@ -148,7 +174,7 @@ bool scriptValueToEnumClass(const ScriptValue& value, T& enumValue) { · 60 unmodified lines | ||
| 148 | 174 | template <typename T> |
| 149 | 175 | int scriptRegisterSequenceMetaType(ScriptEngine* engine, |
| 150 | 176 | T* = 0) { |
| 151 | − return scriptRegisterMetaType<T>(engine, scriptValueFromSequence, scriptValueToSequence); | |
| 177 | + return scriptRegisterMetaType<T, scriptValueFromSequence, scriptValueToSequence>(engine); | |
| 152 | 178 | } |
| 153 | 179 | |
| 154 | 180 | #endif // hifi_ScriptEngineCast_h |