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 #include <boost/signals2.hpp>
29 
30 #include <osg/Node>
31 #include <osg/NodeCallback>
32 
33 #include "WGECallbackTraits.h"
34 
35 
36 /**
37  * This callback allows you a simple usage of callbacks in your module. The callback uses function pointers and calls them every update cycle.
38  * 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
39  * parent module.
40  *
41  * \tparam Type the callback type. You can specify every class that has a nested class called "Callback".
42  */
43 template < typename Type = osg::Node >
44 class WGEFunctorCallback: public WGECallbackTraits< Type >::CallbackType
45 {
46 public:
47  /**
48  * Shared pointer.
49  */
50  typedef osg::ref_ptr< WGEFunctorCallback > SPtr;
51 
52  /**
53  * Const shared pointer.
54  */
55  typedef osg::ref_ptr< const WGEFunctorCallback > ConstSPtr;
56 
57  /**
58  * The type of functor supported in this callback.
59  */
60  typedef boost::function< void ( Type* )> FunctorType;
61 
62  /**
63  * Default constructor. Creates the callback and sets the specified functor instance.
64  *
65  * \param functor the function pointer.
66  */
67  explicit WGEFunctorCallback( FunctorType functor );
68 
69  /**
70  * Destructor.
71  */
72  virtual ~WGEFunctorCallback();
73 
74  /**
75  * This operator gets called by OSG every update cycle. It calls the specified functor.
76  *
77  * \param handled the osg node, stateset or whatever
78  * \param nv the node visitor
79  */
80  virtual void operator()( Type* handled, osg::NodeVisitor* nv );
81 
82  /**
83  * This gets called by OSG every update cycle. It calls the specified functor.
84  * \note we provide several versions here as the OSG does not uniformly use operator().
85  *
86  * \param handled the osg node, stateset or whatever
87  * \param nv the node visitor
88  */
89  virtual void update( osg::NodeVisitor* nv, Type* handled );
90 
91 protected:
92 private:
93  /**
94  * The functor getting called each callback.
95  */
97 };
98 
99 template < typename Type >
101  WGECallbackTraits< Type >::CallbackType(),
102  m_functor( functor )
103 {
104  // initialize members
105 }
106 
107 template < typename Type >
109 {
110  // cleanup
111 }
112 
113 template < typename Type >
114 void WGEFunctorCallback< Type >::operator()( Type* handled, osg::NodeVisitor* nv )
115 {
116  // call functor
117  m_functor( handled );
118  WGECallbackTraits< Type >::traverse( this, handled, nv );
119 }
120 
121 template < typename Type >
122 void WGEFunctorCallback< Type >::update( osg::NodeVisitor* nv, Type* handled )
123 {
124  operator()( handled, nv );
125 }
126 
127 #endif // WGEFUNCTORCALLBACK_H
128 
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.