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
00040
00041
00042
00043
00044
00045
00046 template<class T>
00047 class Event : public Callable
00048 {
00049 private:
00050
00051
00052
00053
00054 typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
00055
00056
00057
00058
00059
00060 QMap<QString, Function* > m_functions;
00061
00062 public:
00063
00064
00065
00066
00067
00068
00069
00070
00071 Event(const QString& name, Object::Ptr parent)
00072 : Callable(name, parent, ArgumentList())
00073 {
00074 }
00075
00076
00077
00078
00079 virtual ~Event()
00080 {
00081 for(QMapIterator<QString, Function* > it = m_functions.begin(); it != m_functions.end(); ++it)
00082 delete it.data();
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void addFunction(const QString& name, FunctionPtr function, const ArgumentList& = ArgumentList())
00102 {
00103 m_functions.replace(name, new VarFunction0<T>(static_cast<T*>(this), function));
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 void addFunction(const QString& name, Function* function)
00120 {
00121 m_functions.replace(name, function);
00122 }
00123
00124
00125
00126
00127
00128 template<class RET, class ARG1, class ARG2, class ARG3, class ARG4, class INSTANCE, typename METHOD>
00129 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00130 {
00131 m_functions.replace(name,
00132 new Kross::Api::ProxyFunction <
00133 INSTANCE, METHOD,
00134 RET, ARG1, ARG2, ARG3, ARG4
00135 > ( instance, method ) );
00136 }
00137
00138
00139 template<class RET, class ARG1, class ARG2, class ARG3, class INSTANCE, typename METHOD>
00140 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00141 {
00142 m_functions.replace(name,
00143 new Kross::Api::ProxyFunction <
00144 INSTANCE, METHOD,
00145 RET, ARG1, ARG2, ARG3
00146 > ( instance, method ) );
00147 }
00148
00149
00150 template<class RET, class ARG1, class ARG2, class INSTANCE, typename METHOD>
00151 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00152 {
00153 m_functions.replace(name,
00154 new Kross::Api::ProxyFunction <
00155 INSTANCE, METHOD,
00156 RET, ARG1, ARG2
00157 > ( instance, method ) );
00158 }
00159
00160
00161 template<class RET, class ARG1, class INSTANCE, typename METHOD>
00162 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00163 {
00164 m_functions.replace(name,
00165 new Kross::Api::ProxyFunction <
00166 INSTANCE, METHOD,
00167 RET, ARG1
00168 > ( instance, method ) );
00169 }
00170
00171
00172 template<class RET, class INSTANCE, typename METHOD>
00173 inline void addProxyFunction(const QString& name, INSTANCE* instance, METHOD method)
00174 {
00175 m_functions.replace(name,
00176 new Kross::Api::ProxyFunction <
00177 INSTANCE, METHOD,
00178 RET
00179 > ( instance, method ) );
00180 }
00181
00182
00183
00184
00185
00186
00187 bool isAFunction(const QString & name) const
00188 {
00189 return m_functions.contains(name);
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 virtual Object::Ptr call(const QString& name, List::Ptr arguments)
00211 {
00212 #ifdef KROSS_API_EVENT_CALL_DEBUG
00213 kdDebug() << QString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) << endl;
00214 #endif
00215
00216 Function* function = m_functions[name];
00217 if(function) {
00218 #ifdef KROSS_API_EVENT_CALL_DEBUG
00219 kdDebug() << QString("Event::call() name='%1' is a builtin function.").arg(name) << endl;
00220 #endif
00221
00222
00223 return function->call(arguments);
00224 }
00225
00226 if(name.isNull()) {
00227
00228 return this;
00229 }
00230
00231
00232 return Callable::call(name, arguments);
00233 }
00234
00235 };
00236
00237 }}
00238
00239 #endif
00240