OpenWalnut  1.4.0
WGEFunctorCallback.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEFUNCTORCALLBACK_H
26 #define WGEFUNCTORCALLBACK_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/signals2.hpp>
30 #endif
31 
32 #include <osg/Node>
33 #include <osg/NodeCallback>
34 
35 #include "WGECallbackTraits.h"
36 
37 
38 /**
39  * This callback allows you a simple usage of callbacks in your module. The callback uses function pointers and calls them every update cycle.
40  * This is especially useful if you want to use a callback in a module without the need of writing subclasses providing a shared_ptr to the
41  * parent module.
42  *
43  * \tparam Type the callback type. You can specify every class that has a nested class called "Callback".
44  */
45 template < typename Type = osg::Node >
46 class WGEFunctorCallback: public WGECallbackTraits< Type >::CallbackType
47 {
48 public:
49  /**
50  * Shared pointer.
51  */
52  typedef osg::ref_ptr< WGEFunctorCallback > SPtr;
53 
54  /**
55  * Const shared pointer.
56  */
57  typedef osg::ref_ptr< const WGEFunctorCallback > ConstSPtr;
58 
59  /**
60  * The type of functor supported in this callback.
61  */
62  typedef boost::function< void ( Type* )> FunctorType;
63 
64  /**
65  * Default constructor. Creates the callback and sets the specified functor instance.
66  *
67  * \param functor the function pointer.
68  */
69  explicit WGEFunctorCallback( FunctorType functor );
70 
71  /**
72  * Destructor.
73  */
74  virtual ~WGEFunctorCallback();
75 
76  /**
77  * This operator gets called by OSG every update cycle. It calls the specified functor.
78  *
79  * \param handled the osg node, stateset or whatever
80  * \param nv the node visitor
81  */
82  virtual void operator()( Type* handled, osg::NodeVisitor* nv );
83 
84  /**
85  * This gets called by OSG every update cycle. It calls the specified functor.
86  * \note we provide several versions here as the OSG does not uniformly use operator().
87  *
88  * \param handled the osg node, stateset or whatever
89  * \param nv the node visitor
90  */
91  virtual void update( osg::NodeVisitor* nv, Type* handled );
92 
93 protected:
94 private:
95  /**
96  * The functor getting called each callback.
97  */
98  FunctorType m_functor;
99 };
100 
101 template < typename Type >
103  WGECallbackTraits< Type >::CallbackType(),
104  m_functor( functor )
105 {
106  // initialize members
107 }
108 
109 template < typename Type >
111 {
112  // cleanup
113 }
114 
115 template < typename Type >
116 void WGEFunctorCallback< Type >::operator()( Type* handled, osg::NodeVisitor* nv )
117 {
118  // call functor
119  m_functor( handled );
120  WGECallbackTraits< Type >::traverse( this, handled, nv );
121 }
122 
123 template < typename Type >
124 void WGEFunctorCallback< Type >::update( osg::NodeVisitor* nv, Type* handled )
125 {
126  operator()( handled, nv );
127 }
128 
129 #endif // WGEFUNCTORCALLBACK_H
130 
boost::function< void(Type *)> FunctorType
The type of functor supported in this callback.
FunctorType m_functor
The functor getting called each callback.
osg::ref_ptr< const WGEFunctorCallback > ConstSPtr
Const shared pointer.
virtual void operator()(Type *handled, osg::NodeVisitor *nv)
This operator gets called by OSG every update cycle.
This callback allows you a simple usage of callbacks in your module.
static void traverse(CallbackType *inst, HandledType *handled, osg::NodeVisitor *nv)
Call traversal method if existing for the specific callback type.
osg::ref_ptr< WGEFunctorCallback > SPtr
Shared pointer.
virtual ~WGEFunctorCallback()
Destructor.
WGEFunctorCallback(FunctorType functor)
Default constructor.
This class is needed as OSG does not define a uniform callback type.
virtual void update(osg::NodeVisitor *nv, Type *handled)
This gets called by OSG every update cycle.