Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# KeosConsoleFunctor.inl

Go to the documentation of this file.

    00001 /*
    00002  * This source file is part of KEOS (Free 3D Engine)
    00003  * For the latest info, see http://www.keosengine.org/
    00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it under
    00007  * the terms of the GNU Lesser General Public License as published by the Free Software
    00008  * Foundation; either version 2 of the License, or (at your option) any later
    00009  * version.
    00010  *
    00011  * This program is distributed in the hope that it will be useful, but WITHOUT
    00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Lesser General Public License along with
    00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00018  * http://www.gnu.org/copyleft/lesser.txt.
    00019  *
    00020  */
    00021
    00022 //==========================================================
    00023 // ORIGINAL COPYRIGHT FOLLOWS
    00024 //==========================================================
    00025 // Yes::Engine - Free C++ 3D engine
    00026 //
    00027 // Copyright (C) 2004-2005 Laurent Gomila
    00028 //
    00029 // This program is free software; you can redistribute it and/or
    00030 // modify it under the terms of the GNU General Public License
    00031 // as published by the Free Software Foundation; either version 2
    00032 // of the License, or (at your option) any later version.
    00033 //
    00034 // This program is distributed in the hope that it will be useful,
    00035 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00036 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00037 // GNU General Public License for more details.
    00038 //
    00039 // You should have received a copy of the GNU General Public License
    00040 // along with this program; if not, write to the Free Software
    00041 // Foundation, Inc.,
    00042 // 59 Temple Place - Suite 330,
    00043 // Boston, MA  02111-1307, USA.
    00044 //
    00045 // E-mail : laurent.gom@gmail.com
    00046 //==========================================================
    00047
    00048
    00055 inline CFunctor::CFunctor(IFunction* Func) :
    00056 m_Function(Func)
    00057 {
    00058
    00059 }
    00060
    00061
    00070 inline std::string CFunctor::operator ()(const std::string& Params) const
    00071 {
    00072     if (!m_Function)
    00073         throw CException("Tentative d'effectuer un appel à une fonction nulle via un foncteur");
    00074
    00075     return m_Function->Execute(Params);
    00076 }
    00077
    00078
    00083 template <typename T> struct Base {typedef T Type;};
    00084 template <typename T> struct Base<T&> {typedef T Type;};
    00085 template <typename T> struct Base<const T> {typedef T Type;};
    00086 template <typename T> struct Base<const T&> {typedef T Type;};
    00087
    00088
    00095 template <typename Ret>
    00096 struct CallFun
    00097 {
    00098     template <typename FuncType>
    00099     static std::string Do(FuncType Func)
    00100     {
    00101         return CStringBuilder(Func());
    00102     }
    00103
    00104     template <typename FuncType, typename Arg1>
    00105     static std::string Do(FuncType Func, Arg1 a1)
    00106     {
    00107         return CStringBuilder(Func(a1));
    00108     }
    00109
    00110     template <typename FuncType, typename Arg1, typename Arg2>
    00111     static std::string Do(FuncType Func, Arg1 a1, Arg2 a2)
    00112     {
    00113         return CStringBuilder(Func(a1, a2));
    00114     }
    00115 };
    00116
    00120 template <>
    00121 struct CallFun<void>
    00122 {
    00123     template <typename FuncType>
    00124     static std::string Do(FuncType Func)
    00125     {
    00126         Func();
    00127         return "";
    00128     }
    00129
    00130     template <typename FuncType, typename Arg1>
    00131     static std::string Do(FuncType Func, Arg1 a1)
    00132     {
    00133         Func(a1);
    00134         return "";
    00135     }
    00136
    00137     template <typename FuncType, typename Arg1, typename Arg2>
    00138     static std::string Do(FuncType Func, Arg1 a1, Arg2 a2)
    00139     {
    00140         Func(a1, a2);
    00141         return "";
    00142     }
    00143 };
    00144
    00151 template <typename Ret, typename ParamType>
    00152 struct CallMemFun
    00153 {
    00154     template <typename FuncType>
    00155     static std::string Do(ParamType Obj, FuncType Func)
    00156     {
    00157         return CStringBuilder((Obj.*Func)());
    00158     }
    00159
    00160     template <typename FuncType, typename Arg1>
    00161     static std::string Do(ParamType Obj, FuncType Func, Arg1 a1)
    00162     {
    00163         return CStringBuilder((Obj.*Func)(a1));
    00164     }
    00165
    00166     template <typename FuncType, typename Arg1, typename Arg2>
    00167     static std::string Do(ParamType Obj, FuncType Func, Arg1 a1, Arg2 a2)
    00168     {
    00169         return CStringBuilder((Obj.*Func)(a1, a2));
    00170     }
    00171 };
    00172
    00173
    00177 template <typename ParamType>
    00178 struct CallMemFun<void, ParamType>
    00179 {
    00180     template <typename FuncType>
    00181     static std::string Do(ParamType Obj, FuncType Func)
    00182     {
    00183         (Obj.*Func)();
    00184         return "";
    00185     }
    00186
    00187     template <typename FuncType, typename Arg1>
    00188     static std::string Do(ParamType Obj, FuncType Func, Arg1 a1)
    00189     {
    00190         (Obj.*Func)(a1);
    00191         return "";
    00192     }
    00193
    00194     template <typename FuncType, typename Arg1, typename Arg2>
    00195     static std::string Do(ParamType Obj, FuncType Func, Arg1 a1, Arg2 a2)
    00196     {
    00197         (Obj.*Func)(a1, a2);
    00198         return "";
    00199     }
    00200 };
    00201
    00202
    00206 template <typename Ret>
    00207 class CFunction0 : public IFunction
    00208 {
    00209     virtual std::string Execute(const std::string& Params)
    00210     {
    00211         CStringExtractor(Params.c_str()).ThrowIfEOF();
    00212
    00213         return CallFun<Ret>::Do(m_Func);
    00214     }
    00215
    00216     typedef Ret (*TFuncType)();
    00217     TFuncType m_Func;
    00218
    00219 public :
    00220
    00221     CFunction0(TFuncType Func) : m_Func(Func) {}
    00222 };
    00223
    00224
    00228 template <typename Ret, typename Arg1>
    00229 class CFunction1 : public IFunction
    00230 {
    00231 private :
    00232
    00233     virtual std::string Execute(const std::string& Params)
    00234     {
    00235         typename Base<Arg1>::Type a1;
    00236         CStringExtractor(Params.c_str())(a1).ThrowIfEOF();
    00237
    00238         return CallFun<Ret>::Do(m_Func, a1);
    00239     }
    00240
    00241     typedef Ret (*TFuncType)(Arg1);
    00242     TFuncType m_Func;
    00243
    00244 public :
    00245
    00246     CFunction1(TFuncType Func) : m_Func(Func) {}
    00247 };
    00248
    00249
    00253 template <typename Ret, typename Arg1, typename Arg2>
    00254 class CFunction2 : public IFunction
    00255 {
    00256 private :
    00257
    00258     virtual std::string Execute(const std::string& Params)
    00259     {
    00260         typename Base<Arg1>::Type a1;
    00261         typename Base<Arg2>::Type a2;
    00262         CStringExtractor(Params.c_str())(a1)(a2).ThrowIfEOF();
    00263
    00264         return CallFun<Ret>::Do(m_Func, a1, a2);
    00265     }
    00266
    00267     typedef Ret (*TFuncType)(Arg1, Arg2);
    00268     TFuncType m_Func;
    00269
    00270 public :
    00271
    00272     CFunction2(TFuncType Func) : m_Func(Func) {}
    00273 };
    00274
    00275
    00279 template <typename ParamType, typename FuncType, typename Ret>
    00280 class CFunctionMem0 : public IFunction
    00281 {
    00282 private :
    00283
    00284     virtual std::string Execute(const std::string& Params)
    00285     {
    00286         CStringExtractor(Params.c_str()).ThrowIfEOF();
    00287
    00288         return CallMemFun<Ret, ParamType>::Do(m_Obj, m_Func);
    00289     }
    00290
    00291     FuncType  m_Func;
    00292     ParamType m_Obj;
    00293
    00294 public :
    00295
    00296     CFunctionMem0(FuncType Func, ParamType Obj) : m_Func(Func), m_Obj(Obj) {}
    00297 };
    00298
    00299
    00303 template <typename ParamType, typename FuncType, typename Ret, typename Arg1>
    00304 class CFunctionMem1 : public IFunction
    00305 {
    00306 private :
    00307
    00308     virtual std::string Execute(const std::string& Params)
    00309     {
    00310         typename Base<Arg1>::Type a1;
    00311         CStringExtractor(Params.c_str())(a1).ThrowIfEOF();
    00312
    00313         return CallMemFun<Ret, ParamType>::Do(m_Obj, m_Func, a1);
    00314     }
    00315
    00316     FuncType  m_Func;
    00317     ParamType m_Obj;
    00318
    00319 public :
    00320
    00321     CFunctionMem1(FuncType Func, ParamType Obj) : m_Func(Func), m_Obj(Obj) {}
    00322 };
    00323
    00324
    00328 template <typename ParamType, typename FuncType, typename Ret, typename Arg1, typename Arg2>
    00329 class CFunctionMem2 : public IFunction
    00330 {
    00331 private :
    00332
    00333     virtual std::string Execute(const std::string& Params)
    00334     {
    00335         typename Base<Arg1>::Type a1;
    00336         typename Base<Arg2>::Type a2;
    00337         CStringExtractor(Params.c_str())(a1)(a2).ThrowIfEOF();
    00338
    00339         return CallMemFun<Ret, ParamType>::Do(m_Obj, m_Func, a1, a2);
    00340     }
    00341
    00342     FuncType  m_Func;
    00343     ParamType m_Obj;
    00344
    00345 public :
    00346
    00347     CFunctionMem2(FuncType Func, ParamType Obj) : m_Func(Func), m_Obj(Obj) {}
    00348 };
    00349
    00350
    00361 template <typename Class, typename Ret>
    00362 inline CFunctor BindCopy(Ret (Class::*Func)(), Class Obj)
    00363 {
    00364     return new CFunctionMem0<Class, Ret (Class::*)(), Ret>(Func, Obj);
    00365 }
    00366
    00367
    00378 template <typename Class, typename Ret, typename Arg1>
    00379 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1), Class Obj)
    00380 {
    00381     return new CFunctionMem1<Class, Ret (Class::*)(Arg1), Ret, Arg1>(Func, Obj);
    00382 }
    00383
    00384
    00395 template <typename Class, typename Ret, typename Arg1, typename Arg2>
    00396 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1, Arg2), Class Obj)
    00397 {
    00398     return new CFunctionMem2<Class, Ret (Class::*)(Arg1, Arg2), Ret, Arg1, Arg2>(Func, Obj);
    00399 }
    00400
    00401
    00412 template <typename Class, typename Ret>
    00413 inline CFunctor BindCopy(Ret (Class::*Func)() const, Class Obj)
    00414 {
    00415     return new CFunctionMem0<Class, Ret (Class::*)() const, Ret>(Func, Obj);
    00416 }
    00417
    00418
    00429 template <typename Class, typename Ret, typename Arg1>
    00430 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1) const, Class Obj)
    00431 {
    00432     return new CFunctionMem1<Class, Ret (Class::*)(Arg1) const, Ret, Arg1>(Func, Obj);
    00433 }
    00434
    00435
    00446 template <typename Class, typename Ret, typename Arg1, typename Arg2>
    00447 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1, Arg2) const, Class Obj)
    00448 {
    00449     return new CFunctionMem2<Class, Ret (Class::*)(Arg1, Arg2) const, Ret, Arg1, Arg2>(Func, Obj);
    00450 }
    00451
    00452
    00461 template <typename T>
    00462 inline CFunctor Bind(T Obj)
    00463 {
    00464     return BindCopy(&T::operator (), Obj);
    00465 }
    00466
    00467
    00477 template <typename Ret>
    00478 inline CFunctor Bind(Ret (*Func)())
    00479 {
    00480     return new CFunction0<Ret>(Func);
    00481 }
    00482
    00483
    00493 template <typename Ret, typename Arg1>
    00494 inline CFunctor Bind(Ret (*Func)(Arg1))
    00495 {
    00496     return new CFunction1<Ret, Arg1>(Func);
    00497 }
    00498
    00499
    00509 template <typename Ret, typename Arg1, typename Arg2>
    00510 inline CFunctor Bind(Ret (*Func)(Arg1, Arg2))
    00511 {
    00512     return new CFunction2<Ret, Arg1, Arg2>(Func);
    00513 }
    00514
    00515
    00526 template <typename Class, typename ParamType, typename Ret>
    00527 inline CFunctor Bind(Ret (Class::*Func)(), ParamType& Obj)
    00528 {
    00529     return new CFunctionMem0<ParamType&, Ret (Class::*)(), Ret>(Func, Obj);
    00530 }
    00531
    00532
    00543 template <typename Class, typename ParamType, typename Ret, typename Arg1>
    00544 inline CFunctor Bind(Ret (Class::*Func)(Arg1), ParamType& Obj)
    00545 {
    00546     return new CFunctionMem1<ParamType&, Ret (Class::*)(Arg1), Ret, Arg1>(Func, Obj);
    00547 }
    00548
    00549
    00560 template <typename Class, typename ParamType, typename Ret, typename Arg1, typename Arg2>
    00561 inline CFunctor Bind(Ret (Class::*Func)(Arg1, Arg2), ParamType& Obj)
    00562 {
    00563     return new CFunctionMem2<ParamType&, Ret (Class::*)(Arg1, Arg2), Ret, Arg1, Arg2>(Func, Obj);
    00564 }
    00565
    00566
    00577 template <typename Class, typename ParamType, typename Ret>
    00578 inline CFunctor Bind(Ret (Class::*Func)() const, const ParamType& Obj)
    00579 {
    00580     return new CFunctionMem0<const ParamType&, Ret (Class::*)() const, Ret>(Func, Obj);
    00581 }
    00582
    00583
    00594 template <typename Class, typename ParamType, typename Ret, typename Arg1>
    00595 inline CFunctor Bind(Ret (Class::*Func)(Arg1) const, const ParamType& Obj)
    00596 {
    00597     return new CFunctionMem1<const ParamType&, Ret (Class::*)(Arg1) const, Ret, Arg1>(Func, Obj);
    00598 }
    00599
    00600
    00611 template <typename Class, typename ParamType, typename Ret, typename Arg1, typename Arg2>
    00612 inline CFunctor Bind(Ret (Class::*Func)(Arg1, Arg2) const, const ParamType& Obj)
    00613 {
    00614     return new CFunctionMem2<const ParamType&, Ret (Class::*)(Arg1, Arg2) const, Ret, Arg1, Arg2>(Func, Obj);
    00615 }