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