bump version to 3.5beta1
[supercollider.git] / QtCollider / QcObjectFactory.h
blob84aa86ebfa729dd5ef5c5bb24328a228a7fbbbb2
1 /************************************************************************
3 * Copyright 2010 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 QcAbstractFactory( const char *className ) {
48 qcDebugMsg( 2, QString("Declaring class '%1'").arg(className) );
49 QtCollider::factories().insert( className, this );
51 virtual const QMetaObject *metaObject() = 0;
52 virtual QObjectProxy *newInstance( PyrObject *, QtCollider::Variant arg[10] ) = 0;
56 template <class QOBJECT> class QcObjectFactory : public QcAbstractFactory
58 public:
59 QcObjectFactory() : QcAbstractFactory( QOBJECT::staticMetaObject.className() ) {}
61 const QMetaObject *metaObject() {
62 return &QOBJECT::staticMetaObject;
65 virtual QObjectProxy *newInstance( PyrObject *scObject, QtCollider::Variant arg[10] ) {
66 QOBJECT *qObject;
68 if( arg[0].type() == QMetaType::Void ) {
69 qObject = new QOBJECT;
71 else {
72 QObject *obj = QOBJECT::staticMetaObject.newInstance(
73 arg[0].asGenericArgument(),
74 arg[1].asGenericArgument(),
75 arg[2].asGenericArgument(),
76 arg[3].asGenericArgument(),
77 arg[4].asGenericArgument(),
78 arg[5].asGenericArgument(),
79 arg[6].asGenericArgument(),
80 arg[7].asGenericArgument(),
81 arg[8].asGenericArgument(),
82 arg[9].asGenericArgument()
85 qObject = qobject_cast<QOBJECT*>(obj);
86 if( !qObject ) {
87 qcErrorMsg( QString("No appropriate constructor found for '%1'.")
88 .arg( QOBJECT::staticMetaObject.className() ) );
89 return 0;
93 return proxy( qObject, scObject );
96 protected:
98 virtual QObjectProxy *proxy( QOBJECT *obj, PyrObject *sc_obj ) {
99 QObjectProxy *prox( new QObjectProxy(obj, sc_obj) );
100 initialize( prox, obj );
101 return prox;
104 virtual void initialize( QObjectProxy *proxy, QOBJECT *obj ) {};
109 #endif //QC_OBJECT_FACTORY_H