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