Line data Source code
1 :
2 : /* Copyright (c) 2006-2012, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This file is part of Collage <https://github.com/Eyescale/Collage>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #ifndef CO_COMMANDFUNC_H
21 : #define CO_COMMANDFUNC_H
22 :
23 : #include <co/types.h>
24 : #include <lunchbox/debug.h>
25 :
26 : // If you get a warning in your code, add this before in including this file:
27 : // #pragma warning( disable: 4407 )
28 : // This warning is 'fixed' by https://github.com/Eyescale/Equalizer/issues/100
29 :
30 : namespace co
31 : {
32 : /**
33 : * A wrapper to register a function callback on an object instance.
34 : *
35 : * This wrapper is used by the Dispatcher to register and save callback
36 : * methods of derived classes.
37 : */
38 : template< typename T > class CommandFunc
39 : {
40 : public:
41 : /**
42 : * Create a new callback to the method on the given object.
43 : * @version 1.0
44 : */
45 1308234 : CommandFunc( T* object, bool (T::*func)( ICommand& ))
46 1308234 : : _object( object ), _func( func ) {}
47 :
48 : /**
49 : * Create a copy of a callback to a different base class.
50 : * @version 1.0
51 : */
52 2709 : template< typename O > CommandFunc( const CommandFunc< O >& from )
53 2709 : : _object( _convertThis< O >( from._object )),
54 2709 : _func( static_cast<bool (T::*)( ICommand& )>(from._func))
55 2709 : {}
56 :
57 : /** @internal Invoke the callback. */
58 665848 : bool operator()( ICommand& command )
59 : {
60 665848 : LBASSERT( _object );
61 665844 : LBASSERT( _func );
62 665840 : return (_object->*_func)( command );
63 : }
64 :
65 : /** @internal */
66 : //@{
67 : /** @internal reset the callback. */
68 645231 : void clear() { _object = 0; _func = 0; }
69 : /** @internal @return true if the callback is valid. */
70 645240 : bool isValid() const { return _object && _func; }
71 :
72 : // Can't make private because copy constructor needs access. Can't
73 : // declare friend because C++ does not allow generic template friends in
74 : // template classes.
75 : //private:
76 : T* _object; //!< @internal
77 : bool (T::*_func)( ICommand& ); //!< @internal
78 : //@}
79 :
80 : private:
81 2709 : template< class F > T* _convertThis( F* ptr )
82 : {
83 : #ifdef _MSC_VER
84 : // https://github.com/Eyescale/Equalizer/issues/100
85 : return reinterpret_cast< T* >( ptr );
86 : #else
87 2709 : return ptr;
88 : #endif
89 : }
90 : };
91 :
92 : /** Output the given CommandFunc. */
93 : template< typename T >
94 0 : inline std::ostream& operator << ( std::ostream& os,
95 : const CommandFunc<T>& func )
96 : {
97 0 : if( func.isValid( ))
98 0 : os << "CommandFunc of " << lunchbox::className( func._object );
99 : else
100 0 : os << "NULL CommandFunc";
101 0 : return os;
102 : }
103 : }
104 :
105 : #endif //CO_COMMANDFUNC_H
|