event.h
1/***************************************************************************
2 * event.h
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
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#ifndef KROSS_API_EVENT_H
21#define KROSS_API_EVENT_H
22
23#include "../main/krossconfig.h"
24#include "object.h"
25#include "callable.h"
26#include "list.h"
27#include "exception.h"
28#include "function.h"
29#include "proxy.h"
30#include "variant.h"
31
32#include <tqstring.h>
33#include <tqvaluelist.h>
34#include <tqmap.h>
35
36namespace Kross { namespace Api {
37
44 template<class T>
45 class Event : public Callable
46 {
47 private:
48
52 typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
53
58 TQMap<TQString, Function* > m_functions;
59
60 public:
61
67 Event(const TQString& name)
68 : Callable(name)
69 {
70 }
71
75 virtual ~Event()
76 {
77 TQMapConstIterator<TQString, Function* > endit = m_functions.constEnd();
78 for(TQMapConstIterator<TQString, Function* > it = m_functions.constBegin(); it != endit; ++it)
79 delete it.data();
80 }
81
98 inline void addFunction(const TQString& name, FunctionPtr function)
99 {
100 m_functions.replace(name, new Function0<T>(static_cast<T*>(this), function));
101 }
102
115 inline void addFunction(const TQString& name, Function* function)
116 {
117 m_functions.replace(name, function);
118 }
119
124 template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class ARG4OBJ, class INSTANCE, typename METHOD>
125 inline void addFunction4(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0, ARG4OBJ* arg4 = 0)
126 {
127 m_functions.replace(name,
129 (instance, method, arg1, arg2, arg3, arg4)
130 );
131 }
132
134 template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class INSTANCE, typename METHOD>
135 inline void addFunction3(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0)
136 {
137 m_functions.replace(name,
139 (instance, method, arg1, arg2, arg3)
140 );
141 }
142
144 template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class INSTANCE, typename METHOD>
145 inline void addFunction2(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0)
146 {
147 m_functions.replace(name,
149 (instance, method, arg1, arg2)
150 );
151 }
152
154 template<class RETURNOBJ, class ARG1OBJ, class INSTANCE, typename METHOD>
155 inline void addFunction1(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0)
156 {
157 m_functions.replace(name,
159 (instance, method, arg1)
160 );
161 }
162
164 template<class RETURNOBJ, class INSTANCE, typename METHOD>
165 inline void addFunction0(const TQString& name, INSTANCE* instance, METHOD method)
166 {
167 m_functions.replace(name,
169 (instance, method)
170 );
171 }
172
178 bool isAFunction(const TQString & name) const
179 {
180 return m_functions.contains(name);
181 }
182
201 virtual Object::Ptr call(const TQString& name, List::Ptr arguments)
202 {
203#ifdef KROSS_API_EVENT_CALL_DEBUG
204 krossdebug( TQString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) );
205#endif
206
207 Function* function = m_functions[name];
208 if(function) {
209#ifdef KROSS_API_EVENT_CALL_DEBUG
210 krossdebug( TQString("Event::call() name='%1' is a builtin function.").arg(name) );
211#endif
212 return function->call(arguments);
213 }
214
215 if(name.isNull()) {
216 // If no name is defined, we return a reference to our instance.
217 return this;
218 }
219
220 // Redirect the call to the Kross::Api::Callable we are inherited from.
221 return Callable::call(name, arguments);
222 }
223
224 };
225
226}}
227
228#endif
229
Base class for callable objects.
Definition callable.h:40
virtual Object::Ptr call(const TQString &name, List::Ptr arguments)
Call the object.
Definition callable.cpp:94
const TQString getName() const
Definition callable.cpp:38
Template class for all kinds of callable events.
Definition event.h:46
void addFunction4(const TQString &name, INSTANCE *instance, METHOD method, ARG1OBJ *arg1=0, ARG2OBJ *arg2=0, ARG3OBJ *arg3=0, ARG4OBJ *arg4=0)
Template function to add a ProxyFunction as builtin-function to this Event instance.
Definition event.h:125
virtual ~Event()
Destructor.
Definition event.h:75
void addFunction3(const TQString &name, INSTANCE *instance, METHOD method, ARG1OBJ *arg1=0, ARG2OBJ *arg2=0, ARG3OBJ *arg3=0)
Same as above with three arguments.
Definition event.h:135
void addFunction(const TQString &name, FunctionPtr function)
Add a Callable methodfunction to the list of functions this Object supports.
Definition event.h:98
virtual Object::Ptr call(const TQString &name, List::Ptr arguments)
Overloaded method to handle function-calls.
Definition event.h:201
bool isAFunction(const TQString &name) const
Check if a function is a member of this Callable.
Definition event.h:178
void addFunction(const TQString &name, Function *function)
Add a methodfunction to the list of functions this Object supports.
Definition event.h:115
void addFunction2(const TQString &name, INSTANCE *instance, METHOD method, ARG1OBJ *arg1=0, ARG2OBJ *arg2=0)
Same as above with two arguments.
Definition event.h:145
Event(const TQString &name)
Constructor.
Definition event.h:67
void addFunction1(const TQString &name, INSTANCE *instance, METHOD method, ARG1OBJ *arg1=0)
Same as above, but with one argument.
Definition event.h:155
void addFunction0(const TQString &name, INSTANCE *instance, METHOD method)
Same as above with no arguments.
Definition event.h:165
This class implements the most abstract way to work with functions.
Definition function.h:59
The base class for functions.
Definition function.h:36
virtual Object::Ptr call(List::Ptr)=0
Each function needs to implement the call-method which will be executed if the function itself should...
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:44
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:57
The ProxyFunction template-class is used to publish any C/C++ method (not only slots) of a struct or ...
Definition proxy.h:104
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:31
void krossdebug(const TQString &s)
Debugging function.