event.h

00001 /***************************************************************************
00002  * event.h
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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      * Template class for all kinds of callable events. An
00042      * event is the abstract base for callable objects like
00043      * methodfunctions in \a Class instances or \a EventSlot
00044      * and \a EventSignal to access Qt signals and slots.
00045      */
00046     template<class T>
00047     class Event : public Callable
00048     {
00049         private:
00050 
00051             /**
00052              * Definition of function-pointers.
00053              */
00054             typedef Object::Ptr(T::*FunctionPtr)(List::Ptr);
00055 
00056             /**
00057              * List of memberfunctions. Each function is accessible
00058              * by the functionname.
00059              */
00060             QMap<QString, Function* > m_functions;
00061 
00062         public:
00063 
00064             /**
00065              * Constructor.
00066              *
00067              * \param name The name this \a Event has.
00068              * \param parent The \a Object that this \a Event is
00069              *        child of.
00070              */
00071             Event(const QString& name, Object::Ptr parent)
00072                 : Callable(name, parent, ArgumentList())
00073             {
00074             }
00075 
00076             /**
00077              * Destructor.
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              * Add a \a Callable methodfunction to the list of functions
00087              * this Object supports.
00088              *
00089              * The FunctionPtr points to the concret
00090              * Object::Ptr myfuncname(List::Ptr)
00091              * method in the class defined with template T.
00092              *
00093              * \param name The functionname. Each function this object
00094              *        holds should have an unique name to be
00095              *        still accessable.
00096              * \param function A pointer to the methodfunction that
00097              *        should handle calls.
00098              * \param arglist A list of arguments for the function.
00099              */
00100 //TODO remove this method as soon as there is no code using it any longer.
00101             void addFunction(const QString& name, FunctionPtr function, const ArgumentList& /*arglist*/ = ArgumentList())
00102             {
00103                 m_functions.replace(name, new VarFunction0<T>(static_cast<T*>(this), function));
00104             }
00105 
00106             /**
00107              * Add a methodfunction to the list of functions this Object
00108              * supports.
00109              *
00110              * \param name The functionname. Each function this object
00111              *        holds should have an unique name to be
00112              *        still accessable.
00113              * \param function A \a Function instance which defines
00114              *        the methodfunction. This \a Event will be the
00115              *        owner of the \a Function instance and will take
00116              *        care of deleting it if this \a Event got deleted.
00117              * \param arglist A list of arguments for the function.
00118              */
00119             void addFunction(const QString& name, Function* function)
00120             {
00121                 m_functions.replace(name, function);
00122             }
00123 
00124             /**
00125              * Template function to add a \a Kross::Api::ProxyFunction as
00126              * builtin-function to this \a Event instance.
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             /// Same as above, but with three arguments.
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             /// Same as above, but with two arguments.
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             /// Same as above, but with one argument.
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             /// Same as above, but with no arguments.
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             * Check if a function is a member of this \a Callable
00184             * \param name the function name
00185             * \return true if the function is available in this \a Callable
00186             */
00187             bool isAFunction(const QString & name) const
00188             {
00189                 return m_functions.contains(name);
00190             }
00191 
00192             /**
00193              * Overloaded method to handle function-calls.
00194              *
00195              * \throw AttributeException if argumentparameters
00196              *        arn't valid.
00197              * \throw RuntimeException if the functionname isn't
00198              *        valid.
00199              * \param name The functionname. Each function this
00200              *        Object holds should have a different
00201              *        name cause they are access by they name.
00202              *        If name is QString::null or empty, a
00203              *        self-reference to this instance is
00204              *        returned.
00205              * \param arguments The list of arguments.
00206              * \return An Object representing the call result
00207              *         or NULL if there doesn't exists such a
00208              *         function with defined name.
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                     //FIXME checkArguments(arguments);
00223                     return function->call(arguments);
00224                 }
00225 
00226                 if(name.isNull()) {
00227                     // If no name is defined, we return a reference to our instance.
00228                     return this;
00229                 }
00230 
00231                 // Redirect the call to the Kross::Api::Callable we are inheritated from.
00232                 return Callable::call(name, arguments);
00233             }
00234 
00235     };
00236 
00237 }}
00238 
00239 #endif
00240 

Generated on Thu Feb 9 17:59:09 2006 for Kross by  doxygen 1.4.6