metaobject.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
4 * copyright (C) 2005 by Tobi Krebs (tobi.krebs@gmail.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "metaobject.h"
21#include "metamethod.h"
22#include "variable.h"
23#include "exception.h"
24
25#include <tqguardedptr.h>
26#include <tqmetaobject.h>
27
28#include <kdebug.h>
29
30using namespace KoMacro;
31
32namespace KoMacro {
33
38 class MetaObject::Private
39 {
40 public:
41
45 TQGuardedPtr<TQObject> const object;
46
50 Private(TQObject* const object)
51 : object(object)
52 {
53 }
54 };
55
56}
57
58MetaObject::MetaObject(TQObject* const object)
59 : TDEShared()
60 , d( new Private(object) ) // create the private d-pointer instance.
61{
62}
63
65{
66 delete d;
67}
68
69TQObject* const MetaObject::object() const
70{
71 if(! d->object) {
72 throw Exception(TQString("Object is undefined."));
73 }
74 return d->object;
75}
76
77/*
78TQStrList MetaObject::signalNames() const
79{
80 return object()->metaObject()->signalNames();
81}
82
83TQStrList MetaObject::slotNames() const
84{
85 return object()->metaObject()->slotNames();
86}
87*/
88
89int MetaObject::indexOfSignal(const char* signal) const
90{
91 TQMetaObject* metaobject = object()->metaObject();
92 int signalid = metaobject->findSignal(signal, false);
93 if(signalid < 0) {
94 throw Exception(TQString("Invalid signal \"%1\"").arg(signal));
95 }
96 return signalid;
97}
98
99int MetaObject::indexOfSlot(const char* slot) const
100{
101 TQMetaObject* metaobject = object()->metaObject();
102 int slotid = metaobject->findSlot(slot, false);
103 if(slotid < 0) {
104 throw Exception(TQString("Invalid slot \"%1\"").arg(slot));
105 }
106 return slotid;
107}
108
109TDESharedPtr<MetaMethod> MetaObject::method(int index)
110{
111 TQObject* obj = object();
113 TQMetaObject* metaobject = obj->metaObject();
114
115 const TQMetaData* metadata = metaobject->slot(index, true);
116 if(! metadata) {
117 // Try to get a signal with that index iff we failed to determinate
118 // a matching slot.
119
120 metadata = metaobject->signal(index, true);
121 if(! metadata) {
122 throw Exception(TQString("Invalid method index \"%1\" in object \"%2\"").arg(index).arg(obj->name()));
123 }
124 type = MetaMethod::Signal;
125 }
126
127 if(metadata->access != TQMetaData::Public) {
128 throw Exception(TQString("Not allowed to access method \"%1\" in object \"%2\"").arg(metadata->name).arg(obj->name()));
129 }
130
131 return new MetaMethod(metadata->name, type, this);
132}
133
134TDESharedPtr<MetaMethod> MetaObject::signal(const char* signal)
135{
136 return method( indexOfSignal(signal) );
137}
138
139TDESharedPtr<MetaMethod> MetaObject::slot(const char* slot)
140{
141 return method( indexOfSlot(slot) );
142}
143
144TDESharedPtr<Variable> MetaObject::invokeMethod(int index, Variable::List arguments)
145{
146 // kdDebug() << "MetaObject::invokeMethod(int index, Variable::List arguments)" << endl;
147 TDESharedPtr<MetaMethod> m = method(index);
148 // kdDebug() << "MetaObject::invokeMethod(int index, Variable::List arguments) return" << endl;
149 return m->invoke(arguments);
150}
151
Base Exception class.
Definition exception.h:35
Class to provide abstract methods for the undocumented TQt3 TQUObject-API functionality.
Definition metamethod.h:47
Type
The type of method this MetaMethod provides access to.
Definition metamethod.h:54
@ Slot
The MetaMethod points to a TQt signal.
Definition metamethod.h:56
int indexOfSlot(const char *slot) const
TDESharedPtr< MetaMethod > slot(const char *slot)
TDESharedPtr< MetaMethod > signal(const char *signal)
TDESharedPtr< MetaMethod > method(int index)
TQObject *const object() const
MetaObject(TQObject *const object)
Constructor.
int indexOfSignal(const char *signal) const
TDESharedPtr< Variable > invokeMethod(int index, TQValueList< TDESharedPtr< Variable > > arguments)
Invoke the MetaMethod that has the index index .
~MetaObject()
Destructor.
TQValueList< TDESharedPtr< Variable > > List
Property to get and set a TQVariant as variable.
Definition variable.h:61