summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/macros/lib/metamethod.cpp
blob: 6642c7069617771b64c4177ff529a866eba3a21c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/***************************************************************************
 * This file is part of the KDE project
 * copyright (C) 2005 by Sebastian Sauer ([email protected])
 * copyright (C) 2005 by Tobi Krebs ([email protected])
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "metamethod.h"
#include "metaobject.h"
#include "metaparameter.h"
#include "variable.h"
#include "exception.h"

#include <tqobject.h>
#include <tqmetaobject.h>

// to access the TQt3 TQUObject API.
#include <private/tqucom_p.h>
#include <private/tqucomextra_p.h>

#include <kdebug.h>

using namespace KoMacro;

namespace KoMacro {

	/**
	* @internal d-pointer class to be more flexible on future extension of the
	* functionality without to much risk to break the binary compatibility.
	*/
	class MetaMethod::Private
	{
		public:

			/**
			* The signature this @a MetaMethod has.
			*/
			TQString signature;

			/**
			* The signature tagname this @a MetaMethod has.
			*/
			TQString signaturetag;

			/**
			* The signature arguments this @a MetaMethod has.
			*/
			TQString signaturearguments;

			/**
			* Cached signature arguments parsed into a list
			* of @a MetaParameter instances.
			*/
			MetaParameter::List arguments;

			/**
			* The @a MetaObject this @a MetaMethod belongs to or is NULL
			* if this @a MetaMethod doesn't belong to any @a MetaObject
			* yet.
			*/
			TDESharedPtr<MetaObject> object;

			/**
			* The @a MetaMethod::Type this method provides access
			* to.
			*/
			MetaMethod::Type type;
	};

}

MetaMethod::MetaMethod(const TQString& signature, Type type, TDESharedPtr<MetaObject> object)
	: TDEShared()
	, d( new Private() ) // create the private d-pointer instance.
{
	d->signature = signature;
	d->object = object;
	d->type = type;

	int startpos = d->signature.find("(");
	int endpos = d->signature.findRev(")");
	if(startpos < 0 || startpos > endpos) {
		throw Exception(TQString("Invalid signature \"%1\"").arg(d->signature));
	}

	d->signaturetag = d->signature.left(startpos).stripWhiteSpace();
	if(d->signaturetag.isEmpty()) {
		throw Exception(TQString("Invalid tagname in signature \"%1\"").arg(d->signature));
	}

	d->signaturearguments = d->signature.mid(startpos + 1, endpos - startpos - 1).stripWhiteSpace();

	do {
		int commapos = d->signaturearguments.find(",");
		int starttemplatepos = d->signaturearguments.find("<");
		if(starttemplatepos >= 0 && (commapos < 0 || starttemplatepos < commapos)) {
			int endtemplatepos = d->signaturearguments.find(">", starttemplatepos);
			if(endtemplatepos <= 0) {
				throw Exception(TQString("No closing template-definiton in signature \"%1\"").arg(d->signature));
			}
			commapos = d->signaturearguments.find(",", endtemplatepos);
		}

		if(commapos > 0) {
			TQString s = d->signaturearguments.left(commapos).stripWhiteSpace();
			if(! s.isEmpty()) {
				d->arguments.append( new MetaParameter(s) );
			}
			d->signaturearguments = d->signaturearguments.right(d->signaturearguments.length() - commapos - 1);
		}
		else {
			TQString s = d->signaturearguments.stripWhiteSpace();
			if(! s.isEmpty()) {
				d->arguments.append( new MetaParameter(s) );
			}
			break;
		}
	} while(true);
}

MetaMethod::~MetaMethod()
{
	delete d;
}

TDESharedPtr<MetaObject> const MetaMethod::object() const
{
	return d->object;
}

const TQString MetaMethod::signature() const
{
	return d->signature;
}

const TQString MetaMethod::signatureTag() const
{
	return d->signaturetag;
}

const TQString MetaMethod::signatureArguments() const
{
	return d->signaturearguments;
}

MetaMethod::Type MetaMethod::type() const
{
	return d->type;
}

MetaParameter::List MetaMethod::arguments() const
{
	return d->arguments;
}

TQUObject* MetaMethod::toTQUObject(Variable::List arguments)
{
	uint argsize = d->arguments.size();

	if(arguments.size() <= argsize) {
		throw Exception(TQString("To less arguments for slot with siganture \"%1\"").arg(d->signature));
	}

	// The first item in the TQUObject-array is for the returnvalue
	// while everything >=1 are the passed parameters.
	TQUObject* uo = new TQUObject[ argsize + 1 ];

	uo[0] = TQUObject(); // empty placeholder for the returnvalue.

	for(uint i = 0; i < argsize; i++) {
		TDESharedPtr<MetaParameter> metaargument = d->arguments[i];
		TDESharedPtr<Variable> variable = arguments[i + 1];

		if ( !variable ) {
	 		throw Exception(TQString("Variable is undefined !"));
		}
	
		if(metaargument->type() != variable->type()) {
			throw Exception(TQString("Wrong variable type in method \"%1\". Expected \"%2\" but got \"%3\"").arg(d->signature).arg(metaargument->type()).arg(variable->type()));
		}

		switch(metaargument->type()) {

			case Variable::TypeNone: {
				kdDebug() << "Variable::TypeNone" << endl;
				uo[i + 1] = TQUObject();
			} break;

			case Variable::TypeVariant:  {
				kdDebug() << "Variable::TypeVariant" << endl;

				const TQVariant variant = variable->variant();
				switch(metaargument->variantType()) {
					case TQVariant::String: {
						const TQString s = variant.toString();
						static_TQUType_TQString.set( &(uo[i + 1]), s );
					} break;
					case TQVariant::Int: {
						const int j = variant.toInt();
						static_TQUType_int.set( &(uo[i + 1]), j );
					} break;
					case TQVariant::Bool: {
						const bool b = variant.toBool();
						static_TQUType_bool.set( &(uo[i + 1]), b );
					} break;
					case TQVariant::Double: {
						const double d = variant.toDouble();
						static_TQUType_double.set( &(uo[i + 1]), d );
					} break;
					case TQVariant::Invalid: {
						static_TQUType_TQVariant.set( &(uo[i + 1]), variant );
					}

					/*FIXME
					static_TQUType_charstar
					static_TQUType_ptr.get(uo); TQObject *qobj = (TQObject *)(ptr);
					*/

					default: {
						throw Exception(TQString("Invalid parameter !!!!!!!!!!!!!!!!!!!!!!!"));
					} break;
				}
			} break;

			case Variable::TypeObject:  {
				kdDebug() << "Variable::TypeObject" << endl;

				const TQObject* obj = arguments[i + 1]->object();
				if(! obj) { //FIXME: move check to MetaParameter?!
					throw Exception(TQString("No TQObject !"));
				}
				static_TQUType_ptr.set( &(uo[i + 1]), obj );
			} break;

			default:  {
				throw Exception(TQString("Invalid variable type"));
			} break;
		}

	}

	return uo;
}

TDESharedPtr<Variable> MetaMethod::toVariable(TQUObject* uo)
{
	const TQString desc( uo->type->desc() );

	if(desc == "null") {
		return new Variable();
	}

	if(desc == TQSTRING_OBJECT_NAME_STRING) {
		const TQString s = static_TQUType_TQString.get(uo);
		return new Variable(s);
	}

	if(desc == "int") {
		const int j = static_TQUType_int.get(uo);
		return new Variable(j);
	}

	if(desc == "bool") {
		const bool b = static_TQUType_bool.get(uo);
		return new Variable(b);
	}

	if(desc == "double") {
		const double d = static_TQUType_double.get(uo);
		return new Variable(d);
	}

	if(desc == "TQVariant") {
		TQVariant v = static_TQUType_TQVariant.get(uo);
		return new Variable(v);
	}

	throw Exception(TQString("Invalid parameter '%1'").arg(desc));
}

Variable::List MetaMethod::toVariableList(TQUObject* uo)
{
	Variable::List list;

	MetaParameter::List::ConstIterator it, end( d->arguments.constEnd() );
	for( it = d->arguments.constBegin(); it != end; ++it) {
		list.append( toVariable(uo) );
		uo++;
	}

	return list;
}

TDESharedPtr<Variable> MetaMethod::invoke(Variable::List arguments)
{
	kdDebug() << "TDESharedPtr<Variable> MetaMethod::invoke(Variable::List arguments)" << endl; 

	if(! d->object) {
		throw Exception("MetaObject is undefined.");
	}

	TQObject* obj = d->object->object();
	TDESharedPtr<Variable> returnvalue;
	TQUObject* qu = 0;

	try {
		qu = toTQUObject(arguments);

		switch( d->type ) {
			case Signal: {
				int index = d->object->indexOfSignal( d->signature.latin1() );
				obj->tqt_emit(index, qu);
			} break;
			case Slot: {
				int index = d->object->indexOfSlot( d->signature.latin1() );
				obj->tqt_invoke(index, qu);
			} break;
			default: {
				throw Exception("Unknown type.");
			} break;
		}
		returnvalue = toVariable( &qu[0] );
	}
	catch(Exception& e) {
		delete [] qu; // free the TQUObject array and
		kdDebug() << "EXCEPTION in KoMacro::MetaMethod::invoke(Variable::List)" << endl;
		throw Exception(e); // re-throw exception
	}

	delete [] qu;
	return returnvalue;
}