ConsoleRuleMethodIAdapter.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #if !defined(__INCLUDE_ConsoleRuleMethodIAdapterh_INCLUDE__)
00022 #define __INCLUDE_ConsoleRuleMethodIAdapterh_INCLUDE__
00023 
00024 #include <console/Console.h>
00025 
00026 // Q. Hmm what happens when you have a long winded naming scheme?
00027 // A. name -> inf
00028 
00029 // Usage pattern :-
00030 //      new ConsoleRuleMethodIAdapter<InfoMap>(
00031 //              this, showHeightBands, "InfoHeightBands");
00032 // Calls InfoMap::showHeightBands when InfoHeightBands is selected 
00033 // in the console
00034 template<class T>
00035 class ConsoleRuleMethodIAdapter : public ConsoleRule
00036 {
00037 public:
00038         ConsoleRuleMethodIAdapter(T *inst, 
00039                 void (T::*call)(), 
00040                 const char *name) : 
00041                 ConsoleRule(name, std::vector<ConsoleRuleParam>()), inst_(inst), call_(call)
00042         {
00043                 Console::instance()->addRule(this);
00044         };
00045 
00046         ConsoleRuleMethodIAdapter(T *inst, 
00047                 void (T::*call)(), 
00048                 const char *name, const std::vector<ConsoleRuleParam> &params) : 
00049                 ConsoleRule(name, params), inst_(inst), call_(call)
00050         {
00051                 Console::instance()->addRule(this);
00052         };
00053         virtual ~ConsoleRuleMethodIAdapter()
00054         {
00055                 Console::instance()->removeRule(this);
00056         };
00057 
00058         virtual void runRule(
00059                 Console *console,
00060                 const char *wholeLine,
00061                 std::vector<ConsoleRuleValue> &values)
00062         {
00063                 (inst_->*call_)();
00064         };
00065 
00066 protected:
00067         std::string name_;
00068         T *inst_;
00069         void (T::*call_)();
00070 };
00071 
00072 // Same as above but passed params to method
00073 template<class T>
00074 class ConsoleRuleMethodIAdapterEx : public ConsoleRule
00075 {
00076 public:
00077         ConsoleRuleMethodIAdapterEx(T *inst, 
00078                 void (T::*call)(std::vector<ConsoleRuleValue>&), 
00079                 const char *name, const std::vector<ConsoleRuleParam> &params) :
00080                 ConsoleRule(name, params), inst_(inst), call_(call)
00081         {
00082                 Console::instance()->addRule(this);
00083         };
00084         virtual ~ConsoleRuleMethodIAdapterEx()
00085         {
00086                 Console::instance()->removeRule(this);
00087         };
00088 
00089         virtual void runRule(
00090                 Console *console,
00091                 const char *wholeLine,
00092                 std::vector<ConsoleRuleValue> &values)
00093         {
00094                 (inst_->*call_)(values);
00095         };
00096 
00097 protected:
00098         std::string name_;
00099         T *inst_;
00100         void (T::*call_)(std::vector<ConsoleRuleValue>&);
00101 };
00102 
00103 // Same as above but passed userdata to method
00104 template<class T>
00105 class ConsoleRuleMethodIAdapterEx2 : public ConsoleRule
00106 {
00107 public:
00108         ConsoleRuleMethodIAdapterEx2(T *inst, 
00109                 void (T::*call)(std::vector<ConsoleRuleValue>&, unsigned int), 
00110                 const char *name, const std::vector<ConsoleRuleParam> &params,
00111                 unsigned int userData) :
00112                 ConsoleRule(name, params, userData), inst_(inst), call_(call)
00113         {
00114                 Console::instance()->addRule(this);
00115         };
00116         virtual ~ConsoleRuleMethodIAdapterEx2()
00117         {
00118                 Console::instance()->removeRule(this);
00119         };
00120 
00121         virtual void runRule(
00122                 Console *console,
00123                 const char *wholeLine,
00124                 std::vector<ConsoleRuleValue> &values)
00125         {
00126                 (inst_->*call_)(values, userData_);
00127         };
00128 
00129 protected:
00130         std::string name_;
00131         T *inst_;
00132         void (T::*call_)(std::vector<ConsoleRuleValue>&, unsigned int);
00133 };
00134 
00135 #endif
00136 

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3