Sierra Toolkit Version of the Day
Callback.hpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2002 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 // Callback class basics from Herb Sutter, http://www.gotw.ca, GOTW #83.
00010 
00011 #ifndef STK_UTIL_UTIL_Callback_hpp
00012 #define STK_UTIL_UTIL_Callback_hpp
00013 
00021 namespace sierra {
00022 
00023 template<class T>
00024 class Callback;
00025 
00030 template<>
00031 class Callback<void>
00032 {
00033 public:
00039   virtual void operator()() const = 0;
00040 
00045   virtual ~Callback()
00046   {}
00047 };
00048 
00049 typedef Callback<void> CallbackBase;      
00050 
00055 template<typename T>
00056 class Callback : public Callback<void>
00057 {
00058 public:
00059   typedef void (T::*F)();       
00060 
00069   Callback(T &t, F f)
00070     : m_t(&t),
00071       m_f(f)
00072   {}
00073 
00078   virtual ~Callback()
00079   {}
00080 
00086   virtual void operator()() const {
00087     (m_t->*m_f)();
00088   }
00089 
00090 private:
00091   T *   m_t;          
00092   F   m_f;          
00093 };
00094 
00105 template<typename T>
00106 CallbackBase *
00107 create_callback(
00108   T &   t,
00109   void    (T::*f)())
00110 {
00111   return new Callback<T>(t, f);
00112 }
00113 
00114 } // namespace sierra
00115 
00116 #endif // STK_UTIL_UTIL_Callback_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines