| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QV4VARIANTASSOCIATIONOBJECT_P_H_ |
| 5 | #define QV4VARIANTASSOCIATIONOBJECT_P_H_ |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <private/qv4object_p.h> |
| 19 | #include <private/qv4referenceobject_p.h> |
| 20 | #include <private/qv4value_p.h> |
| 21 | |
| 22 | #include <QtCore/QVariantMap> |
| 23 | #include <QtCore/QVariantHash> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | namespace QV4 { |
| 28 | |
| 29 | struct Q_QML_EXPORT VariantAssociationPrototype : public QV4::Object |
| 30 | { |
| 31 | V4_PROTOTYPE(objectPrototype); |
| 32 | |
| 33 | static ReturnedValue fromQVariantMap( |
| 34 | ExecutionEngine *engine, |
| 35 | const QVariantMap& variantMap, |
| 36 | QV4::Heap::Object* container, |
| 37 | int property, Heap::ReferenceObject::Flags flags); |
| 38 | |
| 39 | static ReturnedValue fromQVariantHash( |
| 40 | ExecutionEngine *engine, |
| 41 | const QVariantHash& variantHash, |
| 42 | QV4::Heap::Object* container, |
| 43 | int property, Heap::ReferenceObject::Flags flags); |
| 44 | }; |
| 45 | |
| 46 | namespace Heap { |
| 47 | |
| 48 | #define VariantAssociationObjectMembers(class, Member) \ |
| 49 | Member(class, Pointer, ArrayObject *, propertyIndexMapping); |
| 50 | |
| 51 | DECLARE_HEAP_OBJECT(VariantAssociationObject, ReferenceObject) |
| 52 | { |
| 53 | DECLARE_MARKOBJECTS(VariantAssociationObject) |
| 54 | |
| 55 | enum class AssociationType: quint8 { |
| 56 | VariantMap, |
| 57 | VariantHash |
| 58 | }; |
| 59 | |
| 60 | void init( |
| 61 | const QVariantMap& variantMap, |
| 62 | QV4::Heap::Object* container, |
| 63 | int property, Heap::ReferenceObject::Flags flags); |
| 64 | |
| 65 | void init( |
| 66 | const QVariantHash& variantHash, |
| 67 | QV4::Heap::Object* container, |
| 68 | int property, Heap::ReferenceObject::Flags flags); |
| 69 | |
| 70 | template<typename Association> |
| 71 | void createElementWrappers(const Association &association) |
| 72 | { |
| 73 | Q_ASSERT(!object()); |
| 74 | |
| 75 | QV4::Scope scope(internalClass->engine); |
| 76 | QV4::ScopedObject self(scope, this); |
| 77 | for (auto it = association.begin(), end = association.end(); it != end; ++it) { |
| 78 | QV4::ScopedString key(scope, scope.engine->newString(it.key())); |
| 79 | QV4::ScopedValue val(scope, scope.engine->fromVariant(*it)); |
| 80 | self->put(name: key, v: val); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | QV4::ReturnedValue getElement(const QString &id, bool *hasProperty); |
| 85 | |
| 86 | void destroy(); |
| 87 | |
| 88 | void *storagePointer() { return &m_variantAssociation; } |
| 89 | |
| 90 | QVariant toVariant(); |
| 91 | bool setVariant(const QVariant &variant); |
| 92 | |
| 93 | VariantAssociationObject *detached(); |
| 94 | |
| 95 | // The alignment calculation needs to be out of the |
| 96 | // `alignas` due to a GCC 8.3 bug (that at the time of |
| 97 | // writing is used on the QNX 7.1 platform). |
| 98 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94929 |
| 99 | static constexpr auto alignment = |
| 100 | std::max(a: alignof(QVariantMap), b: alignof(QVariantHash)); |
| 101 | alignas(alignment) |
| 102 | std::byte m_variantAssociation[std::max(a: sizeof(QVariantMap), b: sizeof(QVariantHash))]; |
| 103 | |
| 104 | AssociationType m_type; |
| 105 | }; |
| 106 | |
| 107 | } // namespace Heap |
| 108 | |
| 109 | struct Q_QML_EXPORT VariantAssociationObject : public QV4::ReferenceObject |
| 110 | { |
| 111 | V4_OBJECT2(VariantAssociationObject, QV4::ReferenceObject); |
| 112 | V4_PROTOTYPE(variantAssociationPrototype); |
| 113 | V4_NEEDS_DESTROY |
| 114 | |
| 115 | static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, |
| 116 | Value *receiver); |
| 117 | static QV4::ReturnedValue virtualGet(const QV4::Managed *that, PropertyKey id, |
| 118 | const Value *receiver, bool *hasProperty); |
| 119 | |
| 120 | static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id); |
| 121 | |
| 122 | static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target); |
| 123 | |
| 124 | static PropertyAttributes virtualGetOwnProperty(const Managed *m, PropertyKey id, |
| 125 | Property *p); |
| 126 | |
| 127 | static int virtualMetacall(Object *object, QMetaObject::Call call, int index, void **a); |
| 128 | }; |
| 129 | |
| 130 | } // namespace QV4 |
| 131 | |
| 132 | QT_END_NAMESPACE |
| 133 | |
| 134 | #endif // QV4VARIANTASSOCIATIONOBJECT_P_H_ |
| 135 | |