lib

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 "koffice_export.h"
24
25#include "object.h"
26#include "callable.h"
27#include "list.h"
28#include "exception.h"
29#include "function.h"
30#include "proxy.h"
31#include "variant.h"
32
33#include <tqstring.h>
34#include <tqvaluelist.h>
35#include <tqmap.h>
36
37namespace Kross { namespace Api {
38
45 template<class T>
46 class KOFFICE_EXPORT Event : public Callable
47 {
48 private:
49
53 typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
54
59 TQMap<TQString, Function* > m_functions;
60
61 public:
62
68 Event(const TQString& name)
69 : Callable(name)
70 {
71 }
72
76 virtual ~Event()
77 {
78 TQMapConstIterator<TQString, Function* > endit = m_functions.constEnd();
79 for(TQMapConstIterator<TQString, Function* > it = m_functions.constBegin(); it != endit; ++it)
80 delete it.data();
81 }
82
99 inline void addFunction(const TQString& name, FunctionPtr function)
100 {
101 m_functions.replace(name, new Function0<T>(static_cast<T*>(this), function));
102 }
103
116 inline void addFunction(const TQString& name, Function* function)
117 {
118 m_functions.replace(name, function);
119 }
120
125 template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class ARG4OBJ, class INSTANCE, typename METHOD>
126 inline void addFunction4(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0, ARG4OBJ* arg4 = 0)
127 {
128 m_functions.replace(name,
130 (instance, method, arg1, arg2, arg3, arg4)
131 );
132 }
133
135 template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ, class INSTANCE, typename METHOD>
136 inline void addFunction3(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0, ARG3OBJ* arg3 = 0)
137 {
138 m_functions.replace(name,
140 (instance, method, arg1, arg2, arg3)
141 );
142 }
143
145 template<class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class INSTANCE, typename METHOD>
146 inline void addFunction2(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0, ARG2OBJ* arg2 = 0)
147 {
148 m_functions.replace(name,
150 (instance, method, arg1, arg2)
151 );
152 }
153
155 template<class RETURNOBJ, class ARG1OBJ, class INSTANCE, typename METHOD>
156 inline void addFunction1(const TQString& name, INSTANCE* instance, METHOD method, ARG1OBJ* arg1 = 0)
157 {
158 m_functions.replace(name,
160 (instance, method, arg1)
161 );
162 }
163
165 template<class RETURNOBJ, class INSTANCE, typename METHOD>
166 inline void addFunction0(const TQString& name, INSTANCE* instance, METHOD method)
167 {
168 m_functions.replace(name,
170 (instance, method)
171 );
172 }
173
179 bool isAFunction(const TQString & name) const
180 {
181 return m_functions.contains(name);
182 }
183
202 virtual Object::Ptr call(const TQString& name, List::Ptr arguments)
203 {
204#ifdef KROSS_API_EVENT_CALL_DEBUG
205 krossdebug( TQString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) );
206#endif
207
208 Function* function = m_functions[name];
209 if(function) {
210#ifdef KROSS_API_EVENT_CALL_DEBUG
211 krossdebug( TQString("Event::call() name='%1' is a builtin function.").arg(name) );
212#endif
213 return function->call(arguments);
214 }
215
216 if(name.isNull()) {
217 // If no name is defined, we return a reference to our instance.
218 return this;
219 }
220
221 // Redirect the call to the Kross::Api::Callable we are inherited from.
222 return Callable::call(name, arguments);
223 }
224
225 };
226
227}}
228
229#endif
230
virtual Object::Ptr call(const TQString &name, List::Ptr arguments)
Call the object.
Definition callable.cpp:93
const TQString getName() const
Definition callable.cpp:37
Callable(const TQString &name)
Constructor.
Definition callable.cpp:27
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:126
virtual ~Event()
Destructor.
Definition event.h:76
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:136
void addFunction(const TQString &name, FunctionPtr function)
Add a Callable methodfunction to the list of functions this Object supports.
Definition event.h:99
virtual Object::Ptr call(const TQString &name, List::Ptr arguments)
Overloaded method to handle function-calls.
Definition event.h:202
bool isAFunction(const TQString &name) const
Check if a function is a member of this Callable.
Definition event.h:179
void addFunction(const TQString &name, Function *function)
Add a methodfunction to the list of functions this Object supports.
Definition event.h:116
void addFunction2(const TQString &name, INSTANCE *instance, METHOD method, ARG1OBJ *arg1=0, ARG2OBJ *arg2=0)
Same as above with two arguments.
Definition event.h:146
Event(const TQString &name)
Constructor.
Definition event.h:68
void addFunction1(const TQString &name, INSTANCE *instance, METHOD method, ARG1OBJ *arg1=0)
Same as above, but with one argument.
Definition event.h:156
void addFunction0(const TQString &name, INSTANCE *instance, METHOD method)
Same as above with no arguments.
Definition event.h:166
This class implements the most abstract way to work with functions.
Definition function.h:58
The base class for functions.
Definition function.h:35
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:46
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:56
The ProxyFunction template-class is used to publish any C/C++ method (not only slots) of a struct or ...
Definition proxy.h:103
The common Kross API used as common codebase.
Definition callable.h:33
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:33
KOFFICE_EXPORT void krossdebug(const TQString &s)
Debugging function.