Equalizer
1.4.1
|
00001 00002 /* Copyright (c) 2006-2012, Stefan Eilemann <eile@equalizergraphics.com> 00003 * 00004 * This library is free software; you can redistribute it and/or modify it under 00005 * the terms of the GNU Lesser General Public License version 2.1 as published 00006 * by the Free Software Foundation. 00007 * 00008 * This library is distributed in the hope that it will be useful, but WITHOUT 00009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00010 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00011 * details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this library; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00016 */ 00017 00018 #ifndef CO_COMMANDFUNC_H 00019 #define CO_COMMANDFUNC_H 00020 00021 #include <co/types.h> 00022 #include <lunchbox/debug.h> 00023 00024 // If you get a warning in your code, add before in including this file: 00025 // #pragma warning( disable: 4407 ) 00026 // This warning is 'fixed' by https://github.com/Eyescale/Equalizer/issues/100 00027 00028 namespace co 00029 { 00036 template< typename T > class CommandFunc 00037 { 00038 public: 00039 CommandFunc( T* object, bool (T::*func)( Command& )) 00040 : _object( object ), _func( func ) {} 00041 00042 00043 template< typename O > CommandFunc( const CommandFunc< O >& from ) 00044 : _object( _convertThis< O >( from._object )), 00045 _func( static_cast<bool (T::*)( Command& )>(from._func)) 00046 {} 00047 00048 bool operator()( Command& command ) 00049 { 00050 LBASSERT( _object ); 00051 LBASSERT( _func ); 00052 return (_object->*_func)( command ); 00053 } 00054 00055 void clear() { _object = 0; _func = 0; } 00056 bool isValid() const { return _object && _func; } 00057 00058 // Can't make private because copy constructor needs access. Can't 00059 // declare friend because C++ does not allow generic template friends in 00060 // template classes. 00061 //private: 00062 T* _object; 00063 bool (T::*_func)( Command& ); 00064 00065 private: 00066 template< class F > T* _convertThis( F* ptr ) 00067 { 00068 #ifdef _MSC_VER 00069 // https://github.com/Eyescale/Equalizer/issues/100 00070 return reinterpret_cast< T* >( ptr ); 00071 #else 00072 return ptr; 00073 #endif 00074 } 00075 }; 00076 00077 template< typename T > 00078 inline std::ostream& operator << ( std::ostream& os, 00079 const CommandFunc<T>& func ) 00080 { 00081 if( func.isValid( )) 00082 os << "CommandFunc of " << lunchbox::className( func._object ); 00083 else 00084 os << "NULL CommandFunc"; 00085 return os; 00086 } 00087 } 00088 00089 #endif //CO_COMMANDFUNC_H