Equalizer
1.2.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 <co/base/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, 00040 bool (T::*func)( Command& )) 00041 : _object( object ), _func( func ) {} 00042 00043 00044 template< typename O > CommandFunc( const CommandFunc< O >& from ) 00045 : _object( _convertThis< O >( from._object )), 00046 _func( static_cast<bool (T::*)( Command& )>(from._func)) 00047 {} 00048 00049 bool operator()( Command& command ) 00050 { 00051 EQASSERT( _object ); 00052 EQASSERT( _func ); 00053 return (_object->*_func)( command ); 00054 } 00055 00056 void clear() { _object = 0; _func = 0; } 00057 bool isValid() const { return _object && _func; } 00058 00059 // Can't make private because copy constructor needs access. Can't 00060 // declare friend because C++ does not allow generic template friends in 00061 // template classes. 00062 //private: 00063 T* _object; 00064 bool (T::*_func)( Command& ); 00065 00066 private: 00067 template< class F > T* _convertThis( F* ptr ) 00068 { 00069 #ifdef _MSC_VER 00070 // https://github.com/Eyescale/Equalizer/issues/100 00071 return reinterpret_cast< T* >( ptr ); 00072 #else 00073 return ptr; 00074 #endif 00075 } 00076 }; 00077 00078 template< typename T > 00079 inline std::ostream& operator << ( std::ostream& os, 00080 const CommandFunc<T>& func ) 00081 { 00082 if( func.isValid( )) 00083 os << "CommandFunc of " << base::className( func._object ); 00084 else 00085 os << "NULL CommandFunc"; 00086 return os; 00087 } 00088 } 00089 00090 #endif //CO_COMMANDFUNC_H