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"
29 #include <PyrObject.h>
31 #include <QMetaObject>
36 class QcAbstractFactory
;
38 typedef QHash
<QString
,QcAbstractFactory
*> QcFactoryHash
;
40 namespace QtCollider
{
41 QcFactoryHash
& factories ();
44 class QcAbstractFactory
47 virtual const QMetaObject
*metaObject() = 0;
48 virtual QObjectProxy
*newInstance( PyrObject
*, QtCollider::Variant arg
[10] ) = 0;
51 template <class QOBJECT
> class QcObjectFactory
: public QcAbstractFactory
55 const QMetaObject
*metaObject() {
56 return &QOBJECT::staticMetaObject
;
59 virtual QObjectProxy
*newInstance( PyrObject
*scObject
, QtCollider::Variant arg
[10] ) {
62 if( arg
[0].type() == QMetaType::Void
) {
63 qObject
= new QOBJECT
;
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
);
81 qcErrorMsg( QString("No appropriate constructor found for '%1'.")
82 .arg( QOBJECT::staticMetaObject
.className() ) );
87 return proxy( qObject
, scObject
);
92 virtual QObjectProxy
*proxy( QOBJECT
*obj
, PyrObject
*sc_obj
) {
93 QObjectProxy
*prox( new QObjectProxy(obj
, sc_obj
) );
94 initialize( prox
, obj
);
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