OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WGEFunctorCallback.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGEFUNCTORCALLBACK_H
00026 #define WGEFUNCTORCALLBACK_H
00027 
00028 #include <boost/signals2.hpp>
00029 
00030 #include <osg/Node>
00031 #include <osg/NodeCallback>
00032 
00033 #include "WGECallbackTraits.h"
00034 #include "../WExportWGE.h"
00035 
00036 /**
00037  * This callback allows you a simple usage of callbacks in your module. The callback uses function pointers and calls them every update cycle.
00038  * 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
00039  * parent module.
00040  *
00041  * \tparam Type the callback type. You can specify every class that has a nested class called "Callback".
00042  */
00043 template < typename Type = osg::Node >
00044 class WGEFunctorCallback: public WGECallbackTraits< Type >::CallbackType
00045 {
00046 public:
00047 
00048     /**
00049      * Shared pointer.
00050      */
00051     typedef osg::ref_ptr< WGEFunctorCallback > SPtr;
00052 
00053     /**
00054      * Const shared pointer.
00055      */
00056     typedef osg::ref_ptr< const WGEFunctorCallback > ConstSPtr;
00057 
00058     /**
00059      * The type of functor supported in this callback.
00060      */
00061     typedef boost::function< void ( Type* )> FunctorType;
00062 
00063     /**
00064      * Default constructor. Creates the callback and sets the specified functor instance.
00065      *
00066      * \param functor the function pointer.
00067      */
00068     explicit WGEFunctorCallback( FunctorType functor );
00069 
00070     /**
00071      * Destructor.
00072      */
00073     virtual ~WGEFunctorCallback();
00074 
00075     /**
00076      * This operator gets called by OSG every update cycle. It calls the specified functor.
00077      *
00078      * \param handled the osg node, stateset or whatever
00079      * \param nv the node visitor
00080      */
00081     virtual void operator()( Type* handled, osg::NodeVisitor* nv );
00082 
00083     /**
00084      * This gets called by OSG every update cycle. It calls the specified functor.
00085      * \note we provide several versions here as the OSG does not uniformly use operator().
00086      *
00087      * \param handled the osg node, stateset or whatever
00088      * \param nv the node visitor
00089      */
00090     virtual void update( osg::NodeVisitor* nv, Type* handled );
00091 
00092 protected:
00093 private:
00094 
00095     /**
00096      * The functor getting called each callback.
00097      */
00098     FunctorType m_functor;
00099 };
00100 
00101 template < typename Type >
00102 WGEFunctorCallback< Type >::WGEFunctorCallback( FunctorType functor ):
00103     WGECallbackTraits< Type >::CallbackType(),
00104     m_functor( functor )
00105 {
00106     // initialize members
00107 }
00108 
00109 template < typename Type >
00110 WGEFunctorCallback< Type >::~WGEFunctorCallback()
00111 {
00112     // cleanup
00113 }
00114 
00115 template < typename Type >
00116 void WGEFunctorCallback< Type >::operator()( Type* handled, osg::NodeVisitor* nv )
00117 {
00118     // call functor
00119     m_functor( handled );
00120     WGECallbackTraits< Type >::traverse( this, handled, nv );
00121 }
00122 
00123 template < typename Type >
00124 void WGEFunctorCallback< Type >::update( osg::NodeVisitor* nv, Type* handled )
00125 {
00126     operator()( handled, nv );
00127 }
00128 
00129 #endif  // WGEFUNCTORCALLBACK_H
00130 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends