00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KROSS_API_EVENT_H
00021 #define KROSS_API_EVENT_H
00022
00023 #include "../main/krossconfig.h"
00024 #include "object.h"
00025 #include "argument.h"
00026 #include "callable.h"
00027 #include "list.h"
00028 #include "exception.h"
00029 #include "function.h"
00030 #include "proxy.h"
00031 #include "variant.h"
00032
00033 #include <qstring.h>
00034 #include <qvaluelist.h>
00035 #include <qmap.h>
00036 #include <kdebug.h>
00037
00038 namespace Kross { namespace Api {
00039
00046 template<class T>
00047 class Event : public Callable
00048 {
00049 private:
00050
00054 typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
00055
00060 QMap<QString, Function* > m_functions;
00061
00062 public:
00063
00071 Event(const QString& name, Object::Ptr parent)
00072 : Callable(name, parent, ArgumentList())
00073 {
00074 }
00075
00079 virtual ~Event()
00080 {
00081 for(QMapIterator<QString, Function* > it = m_functions.begin(); it != m_functions.end(); ++it)
00082 delete it.data();
00083 }
00084
00102
00103 void addFunction(const QString& name, FunctionPtr function, const ArgumentList& arglist = ArgumentList())
00104 {
00105 m_functions.replace(name, new VarFunction0<T>(static_cast<T*>(this), function));
00106 Q_UNUSED(arglist);
00107 }
00108
00121 void addFunction(const QString& name, Function* function)
00122 {
00123 m_functions.replace(name, function);
00124 }
00125
00130 template<class RET, class ARG1, class ARG2, class ARG3, class ARG4, class INSTANCE, typename METHOD>
00131 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00132 {
00133 m_functions.replace(name,
00134 new Kross::Api::ProxyFunction <
00135 INSTANCE, METHOD,
00136 RET, ARG1, ARG2, ARG3, ARG4
00137 > ( instance, method ) );
00138 }
00139
00141 template<class RET, class ARG1, class ARG2, class ARG3, class INSTANCE, typename METHOD>
00142 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00143 {
00144 m_functions.replace(name,
00145 new Kross::Api::ProxyFunction <
00146 INSTANCE, METHOD,
00147 RET, ARG1, ARG2, ARG3
00148 > ( instance, method ) );
00149 }
00150
00152 template<class RET, class ARG1, class ARG2, class INSTANCE, typename METHOD>
00153 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00154 {
00155 m_functions.replace(name,
00156 new Kross::Api::ProxyFunction <
00157 INSTANCE, METHOD,
00158 RET, ARG1, ARG2
00159 > ( instance, method ) );
00160 }
00161
00163 template<class RET, class ARG1, class INSTANCE, typename METHOD>
00164 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00165 {
00166 m_functions.replace(name,
00167 new Kross::Api::ProxyFunction <
00168 INSTANCE, METHOD,
00169 RET, ARG1
00170 > ( instance, method ) );
00171 }
00172
00174 template<class RET, class INSTANCE, typename METHOD>
00175 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00176 {
00177 m_functions.replace(name,
00178 new Kross::Api::ProxyFunction <
00179 INSTANCE, METHOD,
00180 RET
00181 > ( instance, method ) );
00182 }
00183
00189 bool isAFunction(const QString & name) const
00190 {
00191 return m_functions.contains(name);
00192 }
00193
00212 virtual Object::Ptr call(const QString& name, List::Ptr arguments)
00213 {
00214 #ifdef KROSS_API_EVENT_CALL_DEBUG
00215 kdDebug() << QString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) << endl;
00216 #endif
00217
00218 Function* function = m_functions[name];
00219 if(function) {
00220 #ifdef KROSS_API_EVENT_CALL_DEBUG
00221 kdDebug() << QString("Event::call() name='%1' is a builtin function.").arg(name) << endl;
00222 #endif
00223
00224
00225 return function->call(arguments);
00226 }
00227
00228 if(name.isNull()) {
00229
00230 return this;
00231 }
00232
00233
00234 return Callable::call(name, arguments);
00235 }
00236
00237 };
00238
00239 }}
00240
00241 #endif
00242