qtcollider: declare factories without the use of static initialization
[supercollider.git] / QtCollider / QcObjectFactory.h
blobdc6a7e339a9281d4a661cd0ad5252371d8c32781
1 /************************************************************************
3 * Copyright 2010-2012 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #ifndef QC_OBJECT_FACTORY_H
23 #define QC_OBJECT_FACTORY_H
25 #include "QObjectProxy.h"
26 #include "Common.h"
27 #include "Slot.h"
29 #include <PyrObject.h>
31 #include <QMetaObject>
32 #include <QObject>
33 #include <QWidget>
34 #include <QHash>
36 class QcAbstractFactory;
38 typedef QHash<QString,QcAbstractFactory*> QcFactoryHash;
40 namespace QtCollider {
41 QcFactoryHash & factories ();
44 class QcAbstractFactory
46 public:
47 virtual const QMetaObject *metaObject() = 0;
48 virtual QObjectProxy *newInstance( PyrObject *, QtCollider::Variant arg[10] ) = 0;
51 template <class QOBJECT> class QcObjectFactory : public QcAbstractFactory
53 public:
55 const QMetaObject *metaObject() {
56 return &QOBJECT::staticMetaObject;
59 virtual QObjectProxy *newInstance( PyrObject *scObject, QtCollider::Variant arg[10] ) {
60 QOBJECT *qObject;
62 if( arg[0].type() == QMetaType::Void ) {
63 qObject = new QOBJECT;
65 else {
66 QObject *obj = QOBJECT::staticMetaObject.newInstance(
67 arg[0].asGenericArgument(),
68 arg[1].asGenericArgument(),
69 arg[2].asGenericArgument(),
70 arg[3].asGenericArgument(),
71 arg[4].asGenericArgument(),
72 arg[5].asGenericArgument(),
73 arg[6].asGenericArgument(),
74 arg[7].asGenericArgument(),
75 arg[8].asGenericArgument(),
76 arg[9].asGenericArgument()
79 qObject = qobject_cast<QOBJECT*>(obj);
80 if( !qObject ) {
81 qcErrorMsg( QString("No appropriate constructor found for '%1'.")
82 .arg( QOBJECT::staticMetaObject.className() ) );
83 return 0;
87 return proxy( qObject, scObject );
90 protected:
92 virtual QObjectProxy *proxy( QOBJECT *obj, PyrObject *sc_obj ) {
93 QObjectProxy *prox( new QObjectProxy(obj, sc_obj) );
94 initialize( prox, obj );
95 return prox;
98 virtual void initialize( QObjectProxy *proxy, QOBJECT *obj ) {};
101 #define QC_DECLARE_FACTORY( QOBJECT, FACTORY ) \
102 namespace QtCollider { \
103 void add_factory_##QOBJECT () { \
104 QcAbstractFactory *factory = new FACTORY; \
105 factories().insert( factory->metaObject()->className(), factory ); \
109 #define QC_DECLARE_QOBJECT_FACTORY( QOBJECT ) QC_DECLARE_FACTORY( QOBJECT, QcObjectFactory<QOBJECT> )
111 #define QC_ADD_FACTORY( QOBJECT ) \
112 void add_factory_##QOBJECT(); \
113 add_factory_##QOBJECT()
117 #endif //QC_OBJECT_FACTORY_H