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 
00068 inline std::string CFunctor::operator ()(const std::string& Params) const
00069 {
00070   if (!m_Function)
00071     throw CException("Tentative d'effectuer un appel à une fonction nulle via un foncteur");
00072 
00073   return m_Function->Execute(Params);
00074 }
00075 
00076 
00081 template <typename T> struct Base
00082 {
00083   typedef T Type;
00084 };
00085 template <typename T> struct Base<T&>
00086 {
00087   typedef T Type;
00088 };
00089 template <typename T> struct Base<const T>
00090 {
00091   typedef T Type;
00092 };
00093 template <typename T> struct Base<const T&>
00094 {
00095   typedef T Type;
00096 };
00097 
00098 
00105 template <typename Ret>
00106 struct CallFun
00107 {
00108   template <typename FuncType>
00109   static std::string Do(FuncType Func)
00110   {
00111     return CStringBuilder(Func());
00112   }
00113 
00114   template <typename FuncType, typename Arg1>
00115   static std::string Do(FuncType Func, Arg1 a1)
00116   {
00117     return CStringBuilder(Func(a1));
00118   }
00119 
00120   template <typename FuncType, typename Arg1, typename Arg2>
00121   static std::string Do(FuncType Func, Arg1 a1, Arg2 a2)
00122   {
00123     return CStringBuilder(Func(a1, a2));
00124   }
00125 };
00126 
00130 template <>
00131 struct CallFun<void>
00132 {
00133   template <typename FuncType>
00134   static std::string Do(FuncType Func)
00135   {
00136     Func();
00137     return "";
00138   }
00139 
00140   template <typename FuncType, typename Arg1>
00141   static std::string Do(FuncType Func, Arg1 a1)
00142   {
00143     Func(a1);
00144     return "";
00145   }
00146 
00147   template <typename FuncType, typename Arg1, typename Arg2>
00148   static std::string Do(FuncType Func, Arg1 a1, Arg2 a2)
00149   {
00150     Func(a1, a2);
00151     return "";
00152   }
00153 };
00154 
00161 template <typename Ret, typename ParamType>
00162 struct CallMemFun
00163 {
00164   template <typename FuncType>
00165   static std::string Do(ParamType Obj, FuncType Func)
00166   {
00167     return CStringBuilder((Obj.*Func)());
00168   }
00169 
00170   template <typename FuncType, typename Arg1>
00171   static std::string Do(ParamType Obj, FuncType Func, Arg1 a1)
00172   {
00173     return CStringBuilder((Obj.*Func)(a1));
00174   }
00175 
00176   template <typename FuncType, typename Arg1, typename Arg2>
00177   static std::string Do(ParamType Obj, FuncType Func, Arg1 a1, Arg2 a2)
00178   {
00179     return CStringBuilder((Obj.*Func)(a1, a2));
00180   }
00181 };
00182 
00183 
00187 template <typename ParamType>
00188 struct CallMemFun<void, ParamType>
00189 {
00190   template <typename FuncType>
00191   static std::string Do(ParamType Obj, FuncType Func)
00192   {
00193     (Obj.*Func)();
00194     return "";
00195   }
00196 
00197   template <typename FuncType, typename Arg1>
00198   static std::string Do(ParamType Obj, FuncType Func, Arg1 a1)
00199   {
00200     (Obj.*Func)(a1);
00201     return "";
00202   }
00203 
00204   template <typename FuncType, typename Arg1, typename Arg2>
00205   static std::string Do(ParamType Obj, FuncType Func, Arg1 a1, Arg2 a2)
00206   {
00207     (Obj.*Func)(a1, a2);
00208     return "";
00209   }
00210 };
00211 
00212 
00216 template <typename Ret>
00217 class CFunction0 : public IFunction
00218 {
00219   virtual std::string Execute(const std::string& Params)
00220   {
00221     CStringExtractor(Params.c_str()).ThrowIfEOF();
00222 
00223     return CallFun<Ret>::Do(m_Func);
00224   }
00225 
00226   typedef Ret (*TFuncType)();
00227   TFuncType m_Func;
00228 
00229 public :
00230 
00231   CFunction0(TFuncType Func) : m_Func(Func)
00232   {}
00233 };
00234 
00235 
00239 template <typename Ret, typename Arg1>
00240 class CFunction1 : public IFunction
00241 {
00242 private :
00243 
00244   virtual std::string Execute(const std::string& Params)
00245   {
00246     typename Base<Arg1>::Type a1;
00247     CStringExtractor(Params.c_str())(a1).ThrowIfEOF();
00248 
00249     return CallFun<Ret>::Do(m_Func, a1);
00250   }
00251 
00252   typedef Ret (*TFuncType)(Arg1);
00253   TFuncType m_Func;
00254 
00255 public :
00256 
00257   CFunction1(TFuncType Func) : m_Func(Func)
00258   {}
00259 };
00260 
00261 
00265 template <typename Ret, typename Arg1, typename Arg2>
00266 class CFunction2 : public IFunction
00267 {
00268 private :
00269 
00270   virtual std::string Execute(const std::string& Params)
00271   {
00272     typename Base<Arg1>::Type a1;
00273     typename Base<Arg2>::Type a2;
00274     CStringExtractor(Params.c_str())(a1)(a2).ThrowIfEOF();
00275 
00276     return CallFun<Ret>::Do(m_Func, a1, a2);
00277   }
00278 
00279   typedef Ret (*TFuncType)(Arg1, Arg2);
00280   TFuncType m_Func;
00281 
00282 public :
00283 
00284   CFunction2(TFuncType Func) : m_Func(Func)
00285   {}
00286 };
00287 
00288 
00292 template <typename ParamType, typename FuncType, typename Ret>
00293 class CFunctionMem0 : public IFunction
00294 {
00295 private :
00296 
00297   virtual std::string Execute(const std::string& Params)
00298   {
00299     CStringExtractor(Params.c_str()).ThrowIfEOF();
00300 
00301     return CallMemFun<Ret, ParamType>::Do(m_Obj, m_Func);
00302   }
00303 
00304   FuncType  m_Func;
00305   ParamType m_Obj;
00306 
00307 public :
00308 
00309   CFunctionMem0(FuncType Func, ParamType Obj) : m_Func(Func), m_Obj(Obj)
00310   {}
00311 };
00312 
00313 
00317 template <typename ParamType, typename FuncType, typename Ret, typename Arg1>
00318 class CFunctionMem1 : public IFunction
00319 {
00320 private :
00321 
00322   virtual std::string Execute(const std::string& Params)
00323   {
00324     typename Base<Arg1>::Type a1;
00325     CStringExtractor(Params.c_str())(a1).ThrowIfEOF();
00326 
00327     return CallMemFun<Ret, ParamType>::Do(m_Obj, m_Func, a1);
00328   }
00329 
00330   FuncType  m_Func;
00331   ParamType m_Obj;
00332 
00333 public :
00334 
00335   CFunctionMem1(FuncType Func, ParamType Obj) : m_Func(Func), m_Obj(Obj)
00336   {}
00337 };
00338 
00339 
00343 template <typename ParamType, typename FuncType, typename Ret, typename Arg1, typename Arg2>
00344 class CFunctionMem2 : public IFunction
00345 {
00346 private :
00347 
00348   virtual std::string Execute(const std::string& Params)
00349   {
00350     typename Base<Arg1>::Type a1;
00351     typename Base<Arg2>::Type a2;
00352     CStringExtractor(Params.c_str())(a1)(a2).ThrowIfEOF();
00353 
00354     return CallMemFun<Ret, ParamType>::Do(m_Obj, m_Func, a1, a2);
00355   }
00356 
00357   FuncType  m_Func;
00358   ParamType m_Obj;
00359 
00360 public :
00361 
00362   CFunctionMem2(FuncType Func, ParamType Obj) : m_Func(Func), m_Obj(Obj)
00363   {}
00364 };
00365 
00366 
00377 template <typename Class, typename Ret>
00378 inline CFunctor BindCopy(Ret (Class::*Func)(), Class Obj)
00379 {
00380   return new CFunctionMem0 < Class, Ret (Class::*)(), Ret > (Func, Obj);
00381 }
00382 
00383 
00394 template <typename Class, typename Ret, typename Arg1>
00395 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1), Class Obj)
00396 {
00397   return new CFunctionMem1 < Class, Ret (Class::*)(Arg1), Ret, Arg1 > (Func, Obj);
00398 }
00399 
00400 
00411 template <typename Class, typename Ret, typename Arg1, typename Arg2>
00412 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1, Arg2), Class Obj)
00413 {
00414   return new CFunctionMem2 < Class, Ret (Class::*)(Arg1, Arg2), Ret, Arg1, Arg2 > (Func, Obj);
00415 }
00416 
00417 
00428 template <typename Class, typename Ret>
00429 inline CFunctor BindCopy(Ret (Class::*Func)() const, Class Obj)
00430 {
00431   return new CFunctionMem0 < Class, Ret (Class::*)() const, Ret > (Func, Obj);
00432 }
00433 
00434 
00445 template <typename Class, typename Ret, typename Arg1>
00446 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1) const, Class Obj)
00447 {
00448   return new CFunctionMem1 < Class, Ret (Class::*)(Arg1) const, Ret, Arg1 > (Func, Obj);
00449 }
00450 
00451 
00462 template <typename Class, typename Ret, typename Arg1, typename Arg2>
00463 inline CFunctor BindCopy(Ret (Class::*Func)(Arg1, Arg2) const, Class Obj)
00464 {
00465   return new CFunctionMem2 < Class, Ret (Class::*)(Arg1, Arg2) const, Ret, Arg1, Arg2 > (Func, Obj);
00466 }
00467 
00468 
00477 template <typename T>
00478 inline CFunctor Bind(T Obj)
00479 {
00480   return BindCopy(&T::operator (), Obj);
00481 }
00482 
00483 
00493 template <typename Ret>
00494 inline CFunctor Bind(Ret (*Func)())
00495 {
00496   return new CFunction0<Ret>(Func);
00497 }
00498 
00499 
00509 template <typename Ret, typename Arg1>
00510 inline CFunctor Bind(Ret (*Func)(Arg1))
00511 {
00512   return new CFunction1<Ret, Arg1>(Func);
00513 }
00514 
00515 
00525 template <typename Ret, typename Arg1, typename Arg2>
00526 inline CFunctor Bind(Ret (*Func)(Arg1, Arg2))
00527 {
00528   return new CFunction2<Ret, Arg1, Arg2>(Func);
00529 }
00530 
00531 
00542 template <typename Class, typename ParamType, typename Ret>
00543 inline CFunctor Bind(Ret (Class::*Func)(), ParamType& Obj)
00544 {
00545   return new CFunctionMem0 < ParamType&, Ret (Class::*)(), Ret > (Func, Obj);
00546 }
00547 
00548 
00559 template <typename Class, typename ParamType, typename Ret, typename Arg1>
00560 inline CFunctor Bind(Ret (Class::*Func)(Arg1), ParamType& Obj)
00561 {
00562   return new CFunctionMem1 < ParamType&, Ret (Class::*)(Arg1), Ret, Arg1 > (Func, Obj);
00563 }
00564 
00565 
00576 template <typename Class, typename ParamType, typename Ret, typename Arg1, typename Arg2>
00577 inline CFunctor Bind(Ret (Class::*Func)(Arg1, Arg2), ParamType& Obj)
00578 {
00579   return new CFunctionMem2 < ParamType&, Ret (Class::*)(Arg1, Arg2), Ret, Arg1, Arg2 > (Func, Obj);
00580 }
00581 
00582 
00593 template <typename Class, typename ParamType, typename Ret>
00594 inline CFunctor Bind(Ret (Class::*Func)() const, const ParamType& Obj)
00595 {
00596   return new CFunctionMem0 < const ParamType&, Ret (Class::*)() const, Ret > (Func, Obj);
00597 }
00598 
00599 
00610 template <typename Class, typename ParamType, typename Ret, typename Arg1>
00611 inline CFunctor Bind(Ret (Class::*Func)(Arg1) const, const ParamType& Obj)
00612 {
00613   return new CFunctionMem1 < const ParamType&, Ret (Class::*)(Arg1) const, Ret, Arg1 > (Func, Obj);
00614 }
00615 
00616 
00627 template <typename Class, typename ParamType, typename Ret, typename Arg1, typename Arg2>
00628 inline CFunctor Bind(Ret (Class::*Func)(Arg1, Arg2) const, const ParamType& Obj)
00629 {
00630   return new CFunctionMem2 < const ParamType&, Ret (Class::*)(Arg1, Arg2) const, Ret, Arg1, Arg2 > (Func, Obj);
00631 }

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1