00001 ////////////////////////////////////////////////////////////////////// 00002 // 6/25/02 Aaron Lefohn Scientific Computing and Imaging Institute 00003 // School of Computing University of Utah 00004 // 00005 // This library is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // This library 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 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with this library; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 #ifndef _GLIFT_COMMAND_H_ 00020 #define _GLIFT_COMMAND_H_ 00021 00022 #include "gliftDecl.h" 00023 00024 namespace glift { 00025 00026 /// Typdefs 00027 typedef void (*VoidFuncPtr)(); /// A Ptr to a non-member func. that takes no args and returns no value. 00028 00029 ///////////////////////////////////////////////////////////////////////// 00030 /// 00031 /// command.h - A simple "Command" object that allows for 'void name()' member 00032 /// callbacks. 00033 /// 00034 /// - Taken from pp. 239-241 of "Design Patterns: Elements of \n 00035 /// Reusable Object-Oriented Software" by Gamma, Helm, Johnson, and Vlissides. \n 00036 /// Copywrite 1995, Addison Wessley. 00037 /// - To make class that takes either a C-style function pointer or a \n 00038 /// pointer-to-member (PTM), have it take a Command* and either instantiate a \n 00039 /// "Command0" or a "MemberCommand0". 00040 /// - Instantiate 'MemberCommand0' with an instance of the class containing \n 00041 /// the callback method and a pointer to the callback name. 00042 /// 00043 /// Example: 00044 /// \code 00045 /// AClass a; 00046 /// MemberCommand0<AClass>* callback = new MemberCommand0(&a, &AClass::daCallBack); 00047 /// callback->execute(); //Calls a.daCallBack() 00048 /// \endcode 00049 /// 00050 /// Command - Base class 00051 ////////////////////////// 00052 class _export_ Command 00053 { 00054 public: 00055 virtual ~Command(); 00056 virtual void execute() = 0; 00057 virtual Command* clone() = 0; 00058 00059 protected: 00060 Command() {} 00061 00062 }; 00063 00064 ////////////////////////// 00065 /// Command0 - Base class 00066 ////////////////////////// 00067 class _export_ Command0 : public Command 00068 { 00069 public: 00070 typedef void (*Action) (); 00071 00072 Command0( Action a ); 00073 00074 virtual void execute() {m_action();} 00075 virtual Command* clone(); 00076 00077 private: 00078 Action m_action; 00079 }; 00080 00081 /////////////////////////////// 00082 /// MemberCommand0 - Base class 00083 /////////////////////////////// 00084 template <class Receiver> 00085 class _export_ MemberCommand0 : public Command 00086 { 00087 public: 00088 typedef void (Receiver::* Action) (); 00089 00090 MemberCommand0( Receiver* r, Action a ); 00091 00092 virtual void execute(); 00093 virtual Command* clone(); 00094 00095 private: 00096 Receiver* m_receiver; 00097 Action m_action; 00098 }; 00099 00100 template <class Receiver> 00101 MemberCommand0<Receiver>::MemberCommand0( Receiver* r, Action a ) 00102 : m_receiver(r), 00103 m_action(a) 00104 {} 00105 00106 template <class Receiver> 00107 void MemberCommand0<Receiver>::execute() { 00108 (m_receiver->*m_action)(); 00109 } 00110 00111 template <class Receiver> 00112 Command* MemberCommand0<Receiver>::clone() 00113 { 00114 return new MemberCommand0<Receiver>(*this); 00115 } 00116 00117 } /// End of namespace glift 00118 00119 #endif