Collage  1.6.0
High-performance C++ library for developing object-oriented distributed applications.
commandFunc.h
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 {
38  template< typename T > class CommandFunc
39  {
40  public:
45  CommandFunc( T* object, bool (T::*func)( ICommand& ))
46  : _object( object ), _func( func ) {}
47 
52  template< typename O > CommandFunc( const CommandFunc< O >& from )
53  : _object( _convertThis< O >( from._object )),
54  _func( static_cast<bool (T::*)( ICommand& )>(from._func))
55  {}
56 
58  bool operator()( ICommand& command )
59  {
60  LBASSERT( _object );
61  LBASSERT( _func );
62  return (_object->*_func)( command );
63  }
64 
68  void clear() { _object = 0; _func = 0; }
70  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;
77  bool (T::*_func)( ICommand& );
78 
79 
80  private:
81  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  return ptr;
88 #endif
89  }
90  };
91 
93  template< typename T >
94  inline std::ostream& operator << ( std::ostream& os,
95  const CommandFunc<T>& func )
96  {
97  if( func.isValid( ))
98  os << "CommandFunc of " << lunchbox::className( func._object );
99  else
100  os << "NULL CommandFunc";
101  return os;
102  }
103 }
104 
105 #endif //CO_COMMANDFUNC_H
Object-oriented network library.
Definition: barrier.h:27
A class managing received commands.
Definition: iCommand.h:43
A wrapper to register a function callback on an object instance.
Definition: commandFunc.h:38
CommandFunc(const CommandFunc< O > &from)
Create a copy of a callback to a different base class.
Definition: commandFunc.h:52
CommandFunc(T *object, bool(T::*func)(ICommand &))
Create a new callback to the method on the given object.
Definition: commandFunc.h:45