lib

proxy.h

00001 /***************************************************************************
00002  * proxy.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_PROXY_H
00021 #define KROSS_API_PROXY_H
00022 
00023 #include "../main/krossconfig.h"
00024 #include "object.h"
00025 #include "list.h"
00026 
00027 #include <qstring.h>
00028 
00029 namespace Kross { namespace Api {
00030 
00035     template< class OBJECT, typename TYPE >
00036     class ProxyValue
00037     {
00038         public:
00039 
00044             typedef TYPE type;
00045 
00051             typedef OBJECT object;
00052     };
00053 
00064     template< class INSTANCE, // the objectinstance
00065               typename METHOD, // the method-signature
00066               class RET  = ProxyValue<Kross::Api::Object,void>, // returnvalue
00067               class ARG1 = ProxyValue<Kross::Api::Object,void>, // first argument
00068               class ARG2 = ProxyValue<Kross::Api::Object,void>, // second argument
00069               class ARG3 = ProxyValue<Kross::Api::Object,void>, // forth argument
00070               class ARG4 = ProxyValue<Kross::Api::Object,void> > // fifth argument
00071     class ProxyFunction : public Function
00072     {
00073         private:
00075             INSTANCE* m_instance;
00077             const METHOD m_method;
00078 
00082             template<class PROXYFUNC, typename RETURNRYPE>
00083             struct ProxyFunctionCaller {
00084                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1, typename ARG2::type arg2, typename ARG3::type arg3, typename ARG4::type arg4) {
00085                     return new typename RET::object( ( (self->m_instance)->*(self->m_method) )(arg1,arg2,arg3,arg4) );
00086                 }
00087             };
00088 
00094             template<class PROXYFUNC>
00095             struct ProxyFunctionCaller<PROXYFUNC, void> {
00096                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1, typename ARG2::type arg2, typename ARG3::type arg3, typename ARG4::type arg4) {
00097                     ( (self->m_instance)->*(self->m_method) )(arg1,arg2,arg3,arg4);
00098                     return 0;
00099                 }
00100             };
00101 
00102         public:
00103 
00111             ProxyFunction(INSTANCE* instance, const METHOD& method)
00112                 : m_instance(instance), m_method(method) {}
00113 
00123             Object::Ptr call(List::Ptr args) {
00124                 return ProxyFunctionCaller<ProxyFunction, typename RET::type>::exec(this,
00125                     Kross::Api::Object::fromObject<typename ARG1::object>(args->item(0))->operator typename ARG1::type(),
00126                     Kross::Api::Object::fromObject<typename ARG2::object>(args->item(1))->operator typename ARG2::type(),
00127                     Kross::Api::Object::fromObject<typename ARG3::object>(args->item(2))->operator typename ARG3::type(),
00128                     Kross::Api::Object::fromObject<typename ARG4::object>(args->item(3))->operator typename ARG4::type()
00129                 );
00130             }
00131 
00132             template<class PROXYFUNC, typename RETURNRYPE>
00133             friend struct ProxyFunctionCaller;
00134     };
00135 
00139     template<class INSTANCE, typename METHOD, class RET, class ARG1, class ARG2, class ARG3>
00140     class ProxyFunction<INSTANCE, METHOD, RET, ARG1, ARG2, ARG3 > : public Function
00141     {
00142         private:
00143             INSTANCE* m_instance;
00144             const METHOD m_method;
00145 
00146             template<class PROXYFUNC, typename RETURNRYPE>
00147             struct ProxyFunctionCaller {
00148                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1, typename ARG2::type arg2, typename ARG3::type arg3) {
00149                     return new typename RET::object( ( (self->m_instance)->*(self->m_method) )(arg1,arg2,arg3) );
00150                 }
00151             };
00152 
00153             template<class PROXYFUNC>
00154             struct ProxyFunctionCaller<PROXYFUNC, void> {
00155                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1, typename ARG2::type arg2, typename ARG3::type arg3) {
00156                     ( (self->m_instance)->*(self->m_method) )(arg1,arg2,arg3);
00157                     return 0;
00158                 }
00159             };
00160 
00161         public:
00162             ProxyFunction(INSTANCE* instance, const METHOD& method)
00163                 : m_instance(instance), m_method(method) {}
00164             Object::Ptr call(List::Ptr args) {
00165                 return ProxyFunctionCaller<ProxyFunction, typename RET::type>::exec(this,
00166                     Kross::Api::Object::fromObject<typename ARG1::object>(args->item(0))->operator typename ARG1::type(),
00167                     Kross::Api::Object::fromObject<typename ARG2::object>(args->item(1))->operator typename ARG2::type(),
00168                     Kross::Api::Object::fromObject<typename ARG3::object>(args->item(2))->operator typename ARG3::type()
00169                 );
00170             }
00171 
00172             template<class PROXYFUNC, typename RETURNRYPE>
00173             friend struct ProxyFunctionCaller;
00174     };
00175 
00179     template<class INSTANCE, typename METHOD, class RET, class ARG1, class ARG2>
00180     class ProxyFunction<INSTANCE, METHOD, RET, ARG1, ARG2 > : public Function
00181     {
00182         private:
00183             INSTANCE* m_instance;
00184             const METHOD m_method;
00185 
00186             template<class PROXYFUNC, typename RETURNRYPE>
00187             struct ProxyFunctionCaller {
00188                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1, typename ARG2::type arg2) {
00189                     return new typename RET::object( ( (self->m_instance)->*(self->m_method) )(arg1,arg2) );
00190                 }
00191             };
00192 
00193             template<class PROXYFUNC>
00194             struct ProxyFunctionCaller<PROXYFUNC, void> {
00195                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1, typename ARG2::type arg2) {
00196                     ( (self->m_instance)->*(self->m_method) )(arg1,arg2);
00197                     return 0;
00198                 }
00199             };
00200 
00201         public:
00202             ProxyFunction(INSTANCE* instance, const METHOD& method)
00203                 : m_instance(instance), m_method(method) {}
00204             Object::Ptr call(List::Ptr args) {
00205                 return ProxyFunctionCaller<ProxyFunction, typename RET::type>::exec(this,
00206                     Kross::Api::Object::fromObject<typename ARG1::object>(args->item(0))->operator typename ARG1::type(),
00207                     Kross::Api::Object::fromObject<typename ARG2::object>(args->item(1))->operator typename ARG2::type()
00208                 );
00209             }
00210 
00211             template<class PROXYFUNC, typename RETURNRYPE>
00212             friend struct ProxyFunctionCaller;
00213     };
00214 
00218     template<class INSTANCE, typename METHOD, class RET, class ARG1>
00219     class ProxyFunction<INSTANCE, METHOD, RET, ARG1 > : public Function
00220     {
00221         private:
00222             INSTANCE* m_instance;
00223             const METHOD m_method;
00224 
00225             template<class PROXYFUNC, typename RETURNRYPE>
00226             struct ProxyFunctionCaller {
00227                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1) {
00228                     return new typename RET::object( ( (self->m_instance)->*(self->m_method) )(arg1) );
00229                 }
00230             };
00231 
00232             template<class PROXYFUNC>
00233             struct ProxyFunctionCaller<PROXYFUNC, void> {
00234                 inline static Object::Ptr exec(PROXYFUNC* self, typename ARG1::type arg1) {
00235                     ( (self->m_instance)->*(self->m_method) )(arg1);
00236                     return 0;
00237                 }
00238             };
00239 
00240         public:
00241             ProxyFunction(INSTANCE* instance, const METHOD& method)
00242                 : m_instance(instance), m_method(method) {}
00243             Object::Ptr call(List::Ptr args) {
00244                 return ProxyFunctionCaller<ProxyFunction, typename RET::type>::exec(this,
00245                     Kross::Api::Object::fromObject<typename ARG1::object>(args->item(0))->operator typename ARG1::type()
00246                 );
00247             }
00248 
00249             template<class PROXYFUNC, typename RETURNRYPE>
00250         friend struct ProxyFunctionCaller;
00251     };
00252 
00256     template<class INSTANCE, typename METHOD, class RET>
00257     class ProxyFunction<INSTANCE, METHOD, RET > : public Function
00258     {
00259         private:
00260             INSTANCE* m_instance;
00261             const METHOD m_method;
00262 
00263             template<class PROXYFUNC, typename RETURNRYPE>
00264             struct ProxyFunctionCaller {
00265                 inline static Object::Ptr exec(PROXYFUNC* self) {
00266                     return new typename RET::object( ( (self->m_instance)->*(self->m_method) )() );
00267                 }
00268             };
00269 
00270             template<class PROXYFUNC>
00271             struct ProxyFunctionCaller<PROXYFUNC, void> {
00272                 inline static Object::Ptr exec(PROXYFUNC* self) {
00273                     ( (self->m_instance)->*(self->m_method) )();
00274                     return 0;
00275                 }
00276             };
00277 
00278         public:
00279             ProxyFunction(INSTANCE* instance, const METHOD& method)
00280                 : m_instance(instance), m_method(method) {}
00281             Object::Ptr call(List::Ptr) {
00282                 return ProxyFunctionCaller<ProxyFunction, typename RET::type>::exec(this);
00283             }
00284 
00285             template<class PROXYFUNC, typename RETURNRYPE>
00286             friend struct ProxyFunctionCaller;
00287     };
00288 
00289 }}
00290 
00291 #endif
00292 
KDE Home | KDE Accessibility Home | Description of Access Keys