b3e66eaf9e8987a9e29231811636509864e7b9ac
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativelist.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVELIST_H
43 #define QDECLARATIVELIST_H
44
45 #include <QtCore/qglobal.h>
46 #include <QtCore/qlist.h>
47 #include <QtCore/qvariant.h>
48
49 QT_BEGIN_HEADER
50
51 QT_BEGIN_NAMESPACE
52
53
54 class QObject;
55 struct QMetaObject;
56
57 #ifndef QDECLARATIVELISTPROPERTY
58 #define QDECLARATIVELISTPROPERTY
59 template<typename T>
60 class QDeclarativeListProperty {
61 public:
62     typedef void (*AppendFunction)(QDeclarativeListProperty<T> *, T*);
63     typedef int (*CountFunction)(QDeclarativeListProperty<T> *);
64     typedef T *(*AtFunction)(QDeclarativeListProperty<T> *, int);
65     typedef void (*ClearFunction)(QDeclarativeListProperty<T> *);
66
67     QDeclarativeListProperty()
68         : object(0), data(0), append(0), count(0), at(0), clear(0), dummy1(0), dummy2(0) {}
69     QDeclarativeListProperty(QObject *o, QList<T *> &list)
70         : object(o), data(&list), append(qlist_append), count(qlist_count), at(qlist_at),
71           clear(qlist_clear), dummy1(0), dummy2(0) {}
72     QDeclarativeListProperty(QObject *o, void *d, AppendFunction a, CountFunction c = 0, AtFunction t = 0,
73                     ClearFunction r = 0)
74         : object(o), data(d), append(a), count(c), at(t), clear(r), dummy1(0), dummy2(0) {}
75
76     bool operator==(const QDeclarativeListProperty &o) const {
77         return object == o.object &&
78                data == o.data &&
79                append == o.append &&
80                count == o.count &&
81                at == o.at &&
82                clear == o.clear;
83     }
84
85     QObject *object;
86     void *data;
87
88     AppendFunction append;
89
90     CountFunction count;
91     AtFunction at;
92
93     ClearFunction clear;
94
95     void *dummy1;
96     void *dummy2;
97
98 private:
99     static void qlist_append(QDeclarativeListProperty *p, T *v) {
100         reinterpret_cast<QList<T *> *>(p->data)->append(v);
101     }
102     static int qlist_count(QDeclarativeListProperty *p) {
103         return reinterpret_cast<QList<T *> *>(p->data)->count();
104     }
105     static T *qlist_at(QDeclarativeListProperty *p, int idx) {
106         return reinterpret_cast<QList<T *> *>(p->data)->at(idx);
107     }
108     static void qlist_clear(QDeclarativeListProperty *p) {
109         return reinterpret_cast<QList<T *> *>(p->data)->clear();
110     }
111 };
112 #endif
113
114 class QDeclarativeEngine;
115 class QDeclarativeListReferencePrivate;
116 class Q_DECLARATIVE_EXPORT QDeclarativeListReference
117 {
118 public:
119     QDeclarativeListReference();
120     QDeclarativeListReference(QObject *, const char *property, QDeclarativeEngine * = 0);
121     QDeclarativeListReference(const QDeclarativeListReference &);
122     QDeclarativeListReference &operator=(const QDeclarativeListReference &);
123     ~QDeclarativeListReference();
124
125     bool isValid() const;
126
127     QObject *object() const;
128     const QMetaObject *listElementType() const;
129
130     bool canAppend() const;
131     bool canAt() const;
132     bool canClear() const;
133     bool canCount() const;
134
135     bool append(QObject *) const;
136     QObject *at(int) const;
137     bool clear() const;
138     int count() const;
139
140 private:
141     friend class QDeclarativeListReferencePrivate;
142     QDeclarativeListReferencePrivate* d;
143 };
144
145 QT_END_NAMESPACE
146
147 Q_DECLARE_METATYPE(QDeclarativeListReference)
148
149 QT_END_HEADER
150
151 #endif // QDECLARATIVELIST_H