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 static void qcNoConstructorMsg( const QMetaObject
*metaObject
, int argc
, QtCollider::Variant
*argv
)
53 QString str
= QString("No appropriate constructor found for %1 (")
54 .arg( metaObject
->className() );
56 for (int i
= 0; i
< argc
; ++i
) {
57 int t_id
= argv
[i
].type();
59 if (t_id
!= QMetaType::Void
)
61 if (i
> 0) str
+= ", ";
62 str
+= QMetaType::typeName(t_id
);
73 template <class QOBJECT
> class QcObjectFactory
: public QcAbstractFactory
77 const QMetaObject
*metaObject() {
78 return &QOBJECT::staticMetaObject
;
81 virtual QObjectProxy
*newInstance( PyrObject
*scObject
, QtCollider::Variant arg
[10] ) {
84 if( arg
[0].type() == QMetaType::Void
) {
85 qObject
= new QOBJECT
;
88 QObject
*obj
= QOBJECT::staticMetaObject
.newInstance(
89 arg
[0].asGenericArgument(),
90 arg
[1].asGenericArgument(),
91 arg
[2].asGenericArgument(),
92 arg
[3].asGenericArgument(),
93 arg
[4].asGenericArgument(),
94 arg
[5].asGenericArgument(),
95 arg
[6].asGenericArgument(),
96 arg
[7].asGenericArgument(),
97 arg
[8].asGenericArgument(),
98 arg
[9].asGenericArgument()
101 qObject
= qobject_cast
<QOBJECT
*>(obj
);
103 qcNoConstructorMsg( metaObject(), 10, arg
);
108 return proxy( qObject
, scObject
);
113 virtual QObjectProxy
*proxy( QOBJECT
*obj
, PyrObject
*sc_obj
) {
114 QObjectProxy
*prox( new QObjectProxy(obj
, sc_obj
) );
115 initialize( prox
, obj
);
119 virtual void initialize( QObjectProxy
*proxy
, QOBJECT
*obj
) {};
122 #define QC_DECLARE_FACTORY( QOBJECT, FACTORY ) \
123 namespace QtCollider { \
124 void add_factory_##QOBJECT () { \
125 QcAbstractFactory *factory = new FACTORY; \
126 factories().insert( factory->metaObject()->className(), factory ); \
130 #define QC_DECLARE_QOBJECT_FACTORY( QOBJECT ) QC_DECLARE_FACTORY( QOBJECT, QcObjectFactory<QOBJECT> )
132 #define QC_ADD_FACTORY( QOBJECT ) \
133 void add_factory_##QOBJECT(); \
134 add_factory_##QOBJECT()
138 #endif //QC_OBJECT_FACTORY_H