-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathdefines.h
More file actions
303 lines (256 loc) · 12.1 KB
/
Copy pathdefines.h
File metadata and controls
303 lines (256 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**************************************************************************
**
** This file is part of Nut project.
** https://github.com/HamedMasafi/Nut
**
** Nut is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Nut is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************/
#ifndef SYNTAX_DEFINES_H
#define SYNTAX_DEFINES_H
#define NUT_NAMESPACE Nut
#include "defines_p.h"
#include "qglobal.h"
#include <QString>
#include <QStringList>
#include <QVariant>
#include <QMetaClassInfo>
#ifdef NUT_COMPILE_STATIC
# define NUT_EXPORT
#else
# define NUT_EXPORT Q_DECL_EXPORT
#endif
#define NUT_INFO(type, name, value) \
Q_CLASSINFO(__nut_NAME_PERFIX type #name #value, \
type "\n" #name "\n" #value)
#define NUT_INFO_STRING(type, name, value) \
Q_CLASSINFO(__nut_NAME_PERFIX type #name #value, \
type "\n" #name "\n" value)
#define NUT_FIELD_PERFIX
#define NUT_FIELD_POSTFIX Field
// Database
#define NUT_DB_VERSION(version) \
NUT_INFO(__nut_DB_VERSION, version, 0)
#define NUT_DECLARE_TABLE(type, name) \
NUT_INFO(__nut_TABLE, type, name) \
Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type>) name READ name) \
NUT_WRAP_NAMESPACE(TableSet<type>) *m_##name; \
public: \
static const type *_##name; \
NUT_WRAP_NAMESPACE(TableSet<type>) *name() const \
{ return m_##name; } \
private:
//Table
#define NUT_DECLARE_FIELD(type, name, read, write) \
Q_PROPERTY(type name READ read WRITE write) \
NUT_INFO(__nut_FIELD, name, 0) \
type m_##name; \
public: \
static NUT_WRAP_NAMESPACE(FieldPhrase<type>)& name ## Field(){ \
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) f = \
NUT_WRAP_NAMESPACE(FieldPhrase<type>) \
(staticMetaObject.className(), #name); \
return f; \
} \
type read() const{ \
return m_##name; \
} \
void write(type name){ \
m_##name = name; \
propertyChanged(#name); \
}
#define NUT_FOREIGN_KEY_DECLARE(type, keytype, name, read, write) \
NUT_INFO(__nut_FIELD, name##Id, 0) \
NUT_INFO(__nut_FOREIGN_KEY, name, type) \
Nut::Row<type> m_##name; \
keytype m_##name##Id; \
Q_PROPERTY(Nut::Row<type> name READ read WRITE write) \
Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \
Q_PROPERTY(Nut::Row<Table> _##name READ _##read WRITE _##write) \
public: \
Nut::Row<type> read() const; \
keytype read##Id() const; \
static NUT_WRAP_NAMESPACE(FieldPhrase<keytype>)& name##Id ## Field(){ \
static NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) f = \
NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) \
(staticMetaObject.className(), #name); \
return f; \
} \
void write(Nut::Row<type> name); \
void write##Id(keytype name##Id); \
private: \
Nut::Row<Table> _##read() const; \
void _##write(Nut::Row<Table> name);
#define NUT_FOREIGN_KEY_IMPLEMENT(class, type, keytype, name, read, write) \
\
Nut::Row<type> class::read() const { return m_##name ; } \
void class::write(Nut::Row<type> name){ \
propertyChanged(QT_STRINGIFY2(name##Id)); \
m_##name = name; \
m_##name##Id = name->primaryValue().value<keytype>(); \
} \
\
keytype class::read##Id() const{ \
if (m_##name) \
return m_##name->primaryValue().value<keytype>(); \
return m_##name##Id; \
} \
void class::write##Id(keytype name##Id){ \
propertyChanged(QT_STRINGIFY2(name##Id)); \
m_##name##Id = name##Id; \
m_##name = nullptr; \
propertyChanged(QT_STRINGIFY2(name##Id)); \
} \
Nut::Row<Table> class::_##read() const { return m_##name ; } \
\
void class::_##write(Nut::Row<Table> name) { \
propertyChanged(QT_STRINGIFY2(keyname)); \
m_##name = qSharedPointerCast< type >( name );\
m_##name##Id = m_##name->primaryValue().value<keytype>(); \
}
#define NUT_DECLARE_CHILD_TABLE(type, n) \
private: \
NUT_WRAP_NAMESPACE(TableSet)<type> *m_##n; \
public: \
static type *n##Table(); \
NUT_WRAP_NAMESPACE(TableSet)<type> *n();
#define NUT_IMPLEMENT_CHILD_TABLE(class, type, n) \
type *class::n##Table(){ \
static auto f = new type(); \
return f; \
} \
NUT_WRAP_NAMESPACE(TableSet)<type> *class::n(){ \
return m_##n; \
}
#define NUT_FIELD(name) NUT_INFO(__nut_FIELD, name, 0)
#define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0) \
public: \
QVariant primaryValue() const override { \
return property(#x); \
} \
void setPrimaryValue(const QVariant &value) override { \
setProperty(#x, value); \
} \
private:
#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0)
#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_INFO(__nut_PRIMARY_KEY_AI, x, 0)\
NUT_PRIMARY_KEY(X) NUT_AUTO_INCREMENT(X)
#define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name)
#define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0)
#define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len)
#define NUT_DEFAULT_VALUE(x, n) NUT_INFO(__nut_DEFAULT_VALUE, x, n)
#define NUT_NOT_NULL(x) NUT_INFO(__nut_NOT_NULL, x, 1)
#define NUT_INDEX(name, field, order)
NUT_BEGIN_NAMESPACE
inline bool nutClassInfo(const QMetaClassInfo &classInfo,
QString &type, QString &name, QVariant &value)
{
if (!QString(classInfo.name()).startsWith(__nut_NAME_PERFIX))
return false;
QStringList parts = QString(classInfo.value()).split("\n");
if (parts.count() != 3)
return false;
type = parts[0];
name = parts[1];
value = qVariantFromValue(parts[2]);
return true;
}
inline bool nutClassInfoString(const QMetaClassInfo &classInfo,
QString &type, QString &name, QString &value)
{
if (!QString(classInfo.name()).startsWith(__nut_NAME_PERFIX))
return false;
QStringList parts = QString(classInfo.value()).split("\n");
if (parts.count() != 3)
return false;
type = parts[0];
name = parts[1];
value = parts[2];
return true;
}
inline bool nutClassInfoBool(const QMetaClassInfo &classInfo,
QString &type, QString &name, bool &value)
{
if (!QString(classInfo.name()).startsWith(__nut_NAME_PERFIX))
return false;
QStringList parts = QString(classInfo.value()).split("\n");
if (parts.count() != 3)
return false;
QString buffer = parts[2].toLower();
if (buffer != "true" && buffer != "false")
return false;
type = parts[0];
name = parts[1];
value = (buffer == "true");
return true;
}
inline bool nutClassInfoInt(const QMetaClassInfo &classInfo,
QString &type, QString &name, bool &value)
{
if (!QString(classInfo.name()).startsWith(__nut_NAME_PERFIX))
return false;
QStringList parts = QString(classInfo.value()).split("\n");
if (parts.count() != 3)
return false;
bool ok;
type = parts[0];
name = parts[1];
value = parts[2].toInt(&ok);
return ok;
}
#ifdef NUT_SHARED_POINTER
template <class T>
using RowList = QList<QSharedPointer<T>>;
template <class T>
using RowSet = QSet<QSharedPointer<T>>;
template <typename T>
using Row = QSharedPointer<T>;
template<class T>
inline Row<T> create() {
return QSharedPointer<T>(new T);
}
template<class T>
inline Row<T> create(QObject *parent) {
return QSharedPointer<T>(new T(parent));
}
template<class T>
inline Row<T> createFrom(T *row) {
return QSharedPointer<T>(row);
}
template<class T>
inline Row<T> createFrom(const QSharedPointer<T> row) {
return row;
}
#else
template <typename T>
using RowList = QList<T*>;
template <typename T>
using RowSet = QSet<T*>;
template <typename T>
using Row = T*;
template<class T>
inline Row<T> create() {
return new T;
}
template<class T>
inline T *get(const Row<T> row) {
return row;
}
template<class T>
inline T *get(const QSharedPointer<T> row) {
return row.data();
}
#endif
NUT_END_NAMESPACE
#endif // SYNTAX_DEFINES_H