Collage  1.7.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>
40 {
41 public:
46  CommandFunc(T* object, bool (T::*func)(ICommand&))
47  : _object(object)
48  , _func(func)
49  {
50  }
51 
56  template <typename O>
58  : _object(_convertThis<O>(from._object))
59  , _func(static_cast<bool (T::*)(ICommand&)>(from._func))
60  {
61  }
62 
64  bool operator()(ICommand& command)
65  {
66  LBASSERT(_object);
67  LBASSERT(_func);
68  return (_object->*_func)(command);
69  }
70 
74  void clear()
75  {
76  _object = 0;
77  _func = 0;
78  }
80  bool isValid() const { return _object && _func; }
81  // Can't make private because copy constructor needs access. Can't
82  // declare friend because C++ does not allow generic template friends in
83  // template classes.
84  // private:
85  T* _object;
86  bool (T::*_func)(ICommand&);
87 
88 
89 private:
90  template <class F>
91  T* _convertThis(F* ptr)
92  {
93 #ifdef _MSC_VER
94  // https://github.com/Eyescale/Equalizer/issues/100
95  return reinterpret_cast<T*>(ptr);
96 #else
97  return ptr;
98 #endif
99  }
100 };
101 
103 template <typename T>
104 inline std::ostream& operator<<(std::ostream& os, const CommandFunc<T>& func)
105 {
106  if (func.isValid())
107  os << "CommandFunc of " << lunchbox::className(func._object);
108  else
109  os << "NULL CommandFunc";
110  return os;
111 }
112 }
113 
114 #endif // CO_COMMANDFUNC_H
Object-oriented network library.
Definition: barrier.h:27
A class managing received commands.
Definition: iCommand.h:45
A wrapper to register a function callback on an object instance.
Definition: commandFunc.h:39
CommandFunc(const CommandFunc< O > &from)
Create a copy of a callback to a different base class.
Definition: commandFunc.h:57
CommandFunc(T *object, bool(T::*func)(ICommand &))
Create a new callback to the method on the given object.
Definition: commandFunc.h:46