diff options
author | Lars Knoll <lars.knoll@digia.com> | 2013-09-13 14:11:55 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-18 13:14:03 +0200 |
commit | 49aead7f236f8b8a6ab85adc4b5eace1c1e4dde6 (patch) | |
tree | 21b01c30b568bd79176a5dfe4ec2437e2545660e /src/qml/jsruntime/qv4scopedvalue_p.h | |
parent | 2a43ec129a544d80c9cc3266b5eccce0f6ba66ef (diff) |
Extend the ReturnedValue mechanism to pointers to Managed objects
Add a Returned<T> that we can return instead of raw pointers
to Managed objects.
Start using the Returned<T> for a few methods.
Also clean up all our classes to use the Q_MANAGED macro instead
of manually defining their vtable.
Change-Id: I0a2962e47f3de955cd2cd8474f8f3fcc9e36d084
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src/qml/jsruntime/qv4scopedvalue_p.h')
-rw-r--r-- | src/qml/jsruntime/qv4scopedvalue_p.h | 91 |
1 files changed, 88 insertions, 3 deletions
diff --git a/src/qml/jsruntime/qv4scopedvalue_p.h b/src/qml/jsruntime/qv4scopedvalue_p.h index 68cc2848b3..e192cf5477 100644 --- a/src/qml/jsruntime/qv4scopedvalue_p.h +++ b/src/qml/jsruntime/qv4scopedvalue_p.h @@ -138,6 +138,16 @@ struct ScopedValue #endif } + template<typename T> + ScopedValue(const Scope &scope, Returned<T> *t) + { + ptr = scope.engine->jsStackTop++; + ptr->val = T::toValue(t->getPointer()); +#ifndef QT_NO_DEBUG + ++scope.size; +#endif + } + ScopedValue &operator=(const Value &v) { *ptr = v; return *this; @@ -148,6 +158,12 @@ struct ScopedValue return *this; } + template<typename T> + ScopedValue &operator=(const Returned<T> *t) { + ptr->val = T::toValue(t->getPointer()); + return *this; + } + ScopedValue &operator=(const ScopedValue &other) { *ptr = *other.ptr; return *this; @@ -193,6 +209,27 @@ struct Scoped #endif } + Scoped(const Scope &scope, const ValueRef &v); + + Scoped(const Scope &scope, T *t) + { + ptr = scope.engine->jsStackTop++; + *ptr = T::toValue(t); +#ifndef QT_NO_DEBUG + ++scope.size; +#endif + } + template<typename X> + Scoped(const Scope &scope, Returned<X> *x) + { + ptr = scope.engine->jsStackTop++; + T *t = Returned<T>::getPointer(x); + *ptr = T::toValue(t); +#ifndef QT_NO_DEBUG + ++scope.size; +#endif + } + Scoped(const Scope &scope, const ReturnedValue &v) { ptr = scope.engine->jsStackTop++; @@ -213,6 +250,8 @@ struct Scoped return *this; } + Scoped<T> &operator=(const ValueRef &v); + Scoped<T> &operator=(const ReturnedValue &v) { if (T::cast(QV4::Value::fromReturnedValue(v))) ptr->val = v; @@ -226,17 +265,32 @@ struct Scoped return *this; } + Scoped<T> &operator=(T *t) { + *ptr = T::toValue(t); + return *this; + } + + template<typename X> + Scoped<T> &operator=(Returned<X> *x) { + *ptr = T::toValue(Returned<T>::getPointer(x)); + return *this; + } + + T *operator->() { return static_cast<T *>(ptr->managed()); } -// const Value *operator->() const { -// return T::cast(*ptr); -// } + const Value *operator->() const { + return T::cast(*ptr); + } bool operator!() const { return !ptr->managed(); } + operator bool() const { + return ptr->managed(); + } T *getPointer() { return static_cast<T *>(ptr->managed()); @@ -325,6 +379,29 @@ private: Value *ptr; }; +template<typename T> +Scoped<T>::Scoped(const Scope &scope, const ValueRef &v) +{ + ptr = scope.engine->jsStackTop++; + if (T::cast(*v.operator ->())) + *ptr = *v.operator ->(); + else + *ptr = QV4::Value::undefinedValue(); +#ifndef QT_NO_DEBUG + ++scope.size; +#endif +} + +template<typename T> +Scoped<T> &Scoped<T>::operator=(const ValueRef &v) +{ + if (T::cast(*v.operator ->())) + *ptr = *v.operator ->(); + else + *ptr = QV4::Value::undefinedValue(); + return *this; +} + struct CallDataRef { CallDataRef(const ScopedCallData &c) @@ -384,6 +461,14 @@ struct Encode : private Value { dbl = i; } } + Encode(ReturnedValue v) { + val = v; + } + + template<typename T> + Encode(Returned<T> *t) { + val = T::toValue(t->getPointer()).val; + } operator ReturnedValue() const { return val; |