rev: b82360a98f49ea6cefbd6c08ede6f16231bd6522 dataquay/dataquay/objectmapper/ObjectBuilder.h -rw-r--r-- 12.5 KiB View raw Log this file
b82360a98f49 — Chris Cannam Fix compiler warning 9 months ago
                                                                                
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Dataquay

    A C++/Qt library for simple RDF datastore management.
    Copyright 2009-2012 Chris Cannam.
  
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Except as contained in this notice, the name of Chris Cannam
    shall not be used in advertising or otherwise to promote the sale,
    use or other dealings in this Software without prior written
    authorization.
*/

#ifndef _DATAQUAY_OBJECT_BUILDER_H_
#define _DATAQUAY_OBJECT_BUILDER_H_

#include <QHash>
#include <QString>
#include <QVariant>

namespace Dataquay {

/**
 * \class ObjectBuilder ObjectBuilder.h <dataquay/objectmapper/ObjectBuilder.h>
 *
 * ObjectBuilder is a singleton object factory capable of constructing
 * new objects of classes that are subclassed from QObject.  Given the
 * class name as a string, and optionally a parent object, it will
 * return a new instance of the class.  To be capable of construction
 * using ObjectBuilder, a class must be declared using Q_OBJECT as
 * well as subclassed from QObject.
 *
 * All candidate object classes need to be registered with the builder
 * before they can be constructed.  The only class that ObjectBuilder
 * is able to construct without registration is QObject itself.
 *
 * This class permits code to construct new objects dynamically,
 * without needing to know anything about them except for their class
 * names, and without needing their definitions to be visible.  (The
 * definitions must be visible when the object classes are registered,
 * but not when the objects are constructed.)
 */
class ObjectBuilder
{
public:
    /**
     * Retrieve the single global instance of ObjectBuilder.
     */
    static ObjectBuilder *getInstance();

    /**
     * Register type T, a subclass of QObject, as a class that can be
     * constructed by calling a zero-argument constructor.
     *
     * For example, registerClass<QAction>() declares that QAction is
     * a subclass of QObject that may be built by calling
     * QAction::QAction().
     * 
     * A subsequent call to ObjectBuilder::build("QAction") would
     * return a new QAction built with that constructor (since
     * "QAction" is the class name of QAction returned by its meta
     * object).
     */
    template <typename T>
    void registerClass() {
        m_builders[T::staticMetaObject.className()] = new Builder0<T>();
    }

    /**
     * Register type T, a subclass of QObject, as a class that can be
     * constructed by calling a single-argument constructor whose
     * argument is of pointer-to-Parent type, where Parent is also a
     * subclass of QObject.
     *
     * For example, registerClass<QWidget, QWidget>() declares that
     * QWidget is a subclass of QObject that may be built by calling
     * QWidget::QWidget(QWidget *parent).
     * 
     * A subsequent call to ObjectBuilder::build("QWidget", parent)
     * would return a new QWidget built with that constructor (since
     * "QWidget" is the class name of QWidget returned by its meta
     * object).
     */
    template <typename T, typename Parent>
    void registerClass() {
        m_builders[T::staticMetaObject.className()] = new Builder1<T, Parent>();
    }

    /**
     * Register type T, a subclass of QObject, as a class that can be
     * constructed by calling a zero-argument constructor.  Also
     * declare pointerName to be the meta type name for pointers to
     * type T, such that QVariant can be used to store such pointers.
     *
     * For example, registerClass<QAction>("QAction*") declares that
     * QAction is a subclass of QObject that may be built by calling
     * QAction::QAction(), and that "QAction*" has been registered
     * (using qRegisterMetaType) as the meta type name for
     * pointer-to-QAction.
     * 
     * A subsequent call to ObjectBuilder::build("QAction") would
     * return a new QAction built with that constructor (since
     * "QAction" is the class name of QAction returned by its meta
     * object).
     */
    template <typename T>
    void registerClass(QString pointerName) {
        QString className = T::staticMetaObject.className();
        m_cpmap[className] = pointerName;
        m_pcmap[pointerName] = className;
        m_builders[className] = new Builder0<T>();
        registerExtractor<T>(pointerName);
    }

    /**
     * Register type T, a subclass of QObject, as a class that can be
     * constructed by calling a single-argument constructor whose
     * argument is of pointer-to-Parent type, where Parent is also a
     * subclass of QObject.  Also declare pointerName to be the meta
     * type name for pointers to type T, such that QVariant can be
     * used to store such pointers.
     *
     * For example, registerClass<QWidget, QWidget>("QWidget*")
     * declares that QWidget is a subclass of QObject that may be
     * built by calling QWidget::QWidget(QWidget *parent), and that
     * "QWidget*" has been registered (using qRegisterMetaType) as the
     * meta type name for pointer-to-QWidget.
     * 
     * A subsequent call to ObjectBuilder::build("QWidget", parent)
     * would return a new QWidget built with that constructor (since
     * "QWidget" is the class name of QWidget returned by its meta
     * object).
     */
    template <typename T, typename Parent>
    void registerClass(QString pointerName) {
        QString className = T::staticMetaObject.className();
        m_cpmap[className] = pointerName;
        m_pcmap[pointerName] = className;
        m_builders[className] = new Builder1<T, Parent>();
        registerExtractor<T>(pointerName);
    }

    /**
     * Register type T, a subclass of QObject, as an interface (a pure
     * virtual class) and pointerName to be the meta type name for
     * pointers to type T, such that QVariant can be used to store
     * such pointers.
     *
     * For example, registerClass<Command>("Command*") declares that
     * Command is a subclass of QObject that may not be built directly
     * but that "Command*" has been registered (using
     * qRegisterMetaType) as the meta type name for
     * pointer-to-QAction.
     *
     * A subsequent call to ObjectBuilder::extract("Command*", v)
     * would extract a pointer of type Command* from the QVariant v.
     */
    template <typename T>
    void registerInterface(QString pointerName) {
        QString className = T::staticMetaObject.className();
        m_cpmap[className] = pointerName;
        m_pcmap[pointerName] = className;
        registerExtractor<T>(pointerName);
    }

    /**
     * Return true if the class whose class name (according to its
     * meta object) is className has been registered for building.
     */
    bool knows(QString className) {
        return m_builders.contains(className);
    }

    /**
     * Return a new object whose class name (according to its meta
     * object) is className, with the given parent (cast
     * appropriately) passed to its single argument constructor.
     */
    QObject *build(QString className, QObject *parent) {
        if (!knows(className)) return 0;
        return m_builders[className]->build(parent);
    }

    /**
     * Return a new object whose class name (according to its meta
     * object) is className, constructed with no parent.
     */
    QObject *build(QString className) {
        if (!knows(className)) return 0;
        return m_builders[className]->build(0);
    }

    /**
     * Return true if the class whose pointer has meta-type name
     * pointerName has been registered with that pointer name
     * (i.e. using one of the registerClass(pointerName) methods or
     * registerInterface).
     */
    bool canExtract(QString pointerName) {
        return m_extractors.contains(pointerName);
    }

    /**
     * Return true if the class whose pointer has meta-type name
     * pointerName has been registered with that pointer name
     * (i.e. using one of the registerClass(pointerName) methods or
     * registerInterface).
     */
    bool canInject(QString pointerName) {
        return m_extractors.contains(pointerName);
    }

    /**
     * Provided the given pointerName has been registered using one of
     * the registerClass(pointerName) methods or registerInterface,
     * take the given variant containing that pointer type and extract
     * and return the pointer.
     */
    QObject *extract(QString pointerName, QVariant &v) {
        if (!canExtract(pointerName)) return 0;
        return m_extractors[pointerName]->extract(v);
    }

    /**
     * Provided the given pointerName has been registered using one of
     * the registerClass(pointerName) methods or registerInterface,
     * take the given pointer and stuff it into a variant, returning
     * the result.
     */
    QVariant inject(QString pointerName, QObject *p) {
        if (!canInject(pointerName)) return QVariant();
        return m_extractors[pointerName]->inject(p);
    }

    /**
     * Provided the given pointerName has been registered using one of
     * the registerClass(pointerName) methods, return the name of the
     * class that was used as the template argument for that method.
     */
    QString getClassNameForPointerName(QString pointerName) const {
        if (m_pcmap.contains(pointerName)) return m_pcmap[pointerName];
        return "";
    }

    /**
     * If the class whose class name (according to its meta object) is
     * className has been registered using one of the
     * registerClass(pointerName) methods, return the pointerName that
     * was passed to that method.
     */
    QString getPointerNameForClassName(QString className) const {
        if (m_cpmap.contains(className)) return m_cpmap[className];
        return "";
    }

private:
    ObjectBuilder() {
        registerClass<QObject, QObject>("QObject*");
    }
    ~ObjectBuilder() {
        for (BuilderMap::iterator i = m_builders.begin();
             i != m_builders.end(); ++i) {
            delete *i;
        }
        for (ExtractorMap::iterator i = m_extractors.begin();
             i != m_extractors.end(); ++i) {
            delete *i;
        }
    }

    template <typename T>
    void
    registerExtractor(QString pointerName) {
        m_extractors[pointerName] = new Extractor<T *>();
    }

    template <typename T>
    void
    registerExtractor(QString pointerName, QString /* listName */) {
        m_extractors[pointerName] = new Extractor<T *>();
    }

    struct BuilderBase {
        virtual ~BuilderBase() { }
        virtual QObject *build(QObject *) = 0;
    };

    template <typename T> struct Builder0 : public BuilderBase {
        virtual QObject *build(QObject *) {
            return new T();
        }
    };

    template <typename T, typename Parent> struct Builder1 : public BuilderBase {
        virtual QObject *build(QObject *p) {
            return new T(qobject_cast<Parent *>(p));
        }
    };

    typedef QHash<QString, BuilderBase *> BuilderMap;
    BuilderMap m_builders;

    struct ExtractorBase {
        virtual ~ExtractorBase() { }
        virtual QObject *extract(const QVariant &v) = 0;
        virtual QVariant inject(QObject *) = 0;
    };

    template <typename Pointer> struct Extractor : public ExtractorBase {
        virtual QObject *extract(const QVariant &v) {
            return v.value<Pointer>();
        }
        virtual QVariant inject(QObject *o) {
            Pointer p = qobject_cast<Pointer>(o);
            if (p) return QVariant::fromValue<Pointer>(p);
            else return QVariant();
        }
    };

    typedef QHash<QString, ExtractorBase *> ExtractorMap;
    ExtractorMap m_extractors;

    QHash<QString, QString> m_cpmap;
    QHash<QString, QString> m_pcmap;
};

}

#endif