Improve documentation.
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlaccessors_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLACCESSORS_P_H
43 #define QQMLACCESSORS_P_H
44
45 #include <private/qtqmlglobal_p.h>
46 #include <QtCore/qbytearray.h>
47 #include <QtCore/qvector.h>
48 #include <QtCore/qhash.h>
49 #include <QtCore/QReadWriteLock>
50
51 #if defined(Q_OS_QNX) || defined(Q_OS_LINUX)
52 #include <stdint.h>
53 #endif
54
55 QT_BEGIN_HEADER
56
57 QT_BEGIN_NAMESPACE
58
59 class QObject;
60 struct QMetaObject;
61 class QQmlNotifier;
62
63 // QML "accessor properties" allow V4 and V8 to bypass Qt's meta system to read and, more
64 // importantly, subscribe to properties directly.  Any property that is primarily read
65 // from bindings is a candidate for inclusion as an accessor property.
66 //
67 // To define accessor properties, use the QML_DECLARE_PROPERTIES() and QML_DEFINE_PROPERTIES()
68 // macros.  The QML_DECLARE_PROPERTIES() macro is used to specify the properties, and the
69 // QML_DEFINE_PROPERTIES() macro to register the properties with the
70 // QQmlAccessorProperties singleton.
71 //
72 // A class with accessor properties must also add the Q_CLASSINFO("qt_HasQmlAccessors", "true")
73 // tag to its declaration.  This is essential for QML to maintain internal consistency,
74 // and forgetting to do so will probably cause your application to qFatal() with a
75 // helpful reminder of this requirement.
76 //
77 // It is important that QML_DEFINE_PROPERTIES() has been called before QML ever sees
78 // the type with the accessor properties.  As QML_DEFINE_PROPERTIES() is idempotent, it is
79 // recommended to call it in the type's constructor as well as when the type is registered
80 // as a QML element (if it ever is).  QML_DEFINE_PROPERTIES() is a very cheap operation
81 // if registration has already occurred.
82
83 #define QML_DECLARE_PROPERTIES(type) \
84     static volatile bool qqml_accessor_properties_isregistered_ ## type = false; \
85     static QQmlAccessorProperties::Property qqml_accessor_properties_ ## type[] =
86
87 #define QML_DEFINE_PROPERTIES(type) \
88     do { \
89         if (!qqml_accessor_properties_isregistered_ ## type) { \
90             int count = sizeof(qqml_accessor_properties_ ## type) / \
91                         sizeof(QQmlAccessorProperties::Property); \
92             QQmlAccessorProperties::registerProperties(&type::staticMetaObject, count, \
93                     qqml_accessor_properties_ ## type);\
94             qqml_accessor_properties_isregistered_ ## type = true; \
95         } \
96     } while (false);
97
98 #define QML_PRIVATE_ACCESSOR(clazz, cpptype, name, variable) \
99     static void clazz ## _ ## name ## Read(QObject *o, intptr_t, void *rv) \
100     { \
101         clazz ## Private *d = clazz ## Private::get(static_cast<clazz *>(o)); \
102         *static_cast<cpptype *>(rv) = d->variable; \
103     }
104
105 #define QML_PROPERTY_NAME(name) #name, sizeof #name - 1
106
107 class QQmlAccessors
108 {
109 public:
110     void (*read)(QObject *object, intptr_t property, void *output);
111     void (*notifier)(QObject *object, intptr_t property, QQmlNotifier **notifier);
112 };
113
114 namespace QQmlAccessorProperties {
115     struct Property {
116         const char *name;
117         unsigned int nameLength;
118         intptr_t data;
119         QQmlAccessors *accessors;
120     };
121
122     struct Properties {
123         inline Properties();
124         Properties(Property *, int);
125
126         bool operator==(const Properties &o) const {
127             return count == o.count && properties == o.properties;
128         }
129
130         inline Property *property(const char *name);
131
132         int count;
133         Property *properties;
134         quint32 nameMask;
135     };
136
137     Properties properties(const QMetaObject *);
138     void Q_QML_PRIVATE_EXPORT registerProperties(const QMetaObject *, int, Property *);
139 };
140
141 QQmlAccessorProperties::Property *
142 QQmlAccessorProperties::Properties::property(const char *name)
143 {
144     if (count == 0)
145         return 0;
146
147     const unsigned int length = (unsigned int)strlen(name);
148
149     Q_ASSERT(length);
150
151     if (nameMask & (1 << qMin(31U, length - 1))) {
152
153         for (int ii = 0; ii < count; ++ii) {
154             if (properties[ii].nameLength == length && 0 == qstrcmp(name, properties[ii].name))
155                 return &properties[ii];
156         }
157
158     }
159
160     return 0;
161 }
162
163 QQmlAccessorProperties::Properties::Properties()
164 : count(0), properties(0), nameMask(0)
165 {
166 }
167
168 QT_END_NAMESPACE
169
170 QT_END_HEADER
171
172 #endif // QQMLACCESSORS_P_H