00001 /*************************************************************************** 00002 * Copyright (C) 2008 by Sverre Rabbelier * 00003 * sverre@rabbelier.nl * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 3 of the License, or * 00008 * (at your option) any later version. * 00009 * * 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 * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the * 00017 * Free Software Foundation, Inc., * 00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00019 ***************************************************************************/ 00020 #pragma once 00021 00029 #include "Assert.h" 00030 #include "Types.h" 00031 00032 00038 template <class T> 00039 class CommandObject 00040 { 00041 public: 00042 typedef void (T::*CommandFunction)(const std::string& argument); 00045 CommandObject(const char* name, CommandFunction command, bool fullname = false); 00046 00048 ~CommandObject(); 00049 00050 00052 void Run(T* owner, const std::string& argument) const; 00053 00055 const char* getName() const; 00056 00058 bool fullName() const; 00059 00060 protected: 00061 const char* m_name; 00062 CommandFunction m_command; 00063 bool m_fullName; 00064 }; 00065 00066 template <class T> 00067 const char* CommandObject<T>::getName() const 00068 { 00069 return m_name; 00070 } 00071 00072 template <class T> 00073 bool CommandObject<T>::fullName() const 00074 { 00075 return m_fullName; 00076 } 00077 00078 template <class T> 00079 CommandObject<T>::CommandObject(const char* name, CommandFunction command, bool fullname) : 00080 m_name(name), 00081 m_command(command), 00082 m_fullName(fullname) 00083 { 00084 Assert(name); 00085 } 00086 00087 template <class T> 00088 CommandObject<T>::~CommandObject() 00089 { 00090 } 00091 00092 template <class T> 00093 void CommandObject<T>::Run(T* owner, const std::string& argument) const 00094 { 00095 Assert(m_command); 00096 Assert(owner); 00097 00098 (owner->*m_command)(argument); 00099 }