OpenWalnut  1.4.0
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 
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      * Shared pointer.
00049      */
00050     typedef osg::ref_ptr< WGEFunctorCallback > SPtr;
00051 
00052     /**
00053      * Const shared pointer.
00054      */
00055     typedef osg::ref_ptr< const WGEFunctorCallback > ConstSPtr;
00056 
00057     /**
00058      * The type of functor supported in this callback.
00059      */
00060     typedef boost::function< void ( Type* )> FunctorType;
00061 
00062     /**
00063      * Default constructor. Creates the callback and sets the specified functor instance.
00064      *
00065      * \param functor the function pointer.
00066      */
00067     explicit WGEFunctorCallback( FunctorType functor );
00068 
00069     /**
00070      * Destructor.
00071      */
00072     virtual ~WGEFunctorCallback();
00073 
00074     /**
00075      * This operator gets called by OSG every update cycle. It calls the specified functor.
00076      *
00077      * \param handled the osg node, stateset or whatever
00078      * \param nv the node visitor
00079      */
00080     virtual void operator()( Type* handled, osg::NodeVisitor* nv );
00081 
00082     /**
00083      * This gets called by OSG every update cycle. It calls the specified functor.
00084      * \note we provide several versions here as the OSG does not uniformly use operator().
00085      *
00086      * \param handled the osg node, stateset or whatever
00087      * \param nv the node visitor
00088      */
00089     virtual void update( osg::NodeVisitor* nv, Type* handled );
00090 
00091 protected:
00092 private:
00093     /**
00094      * The functor getting called each callback.
00095      */
00096     FunctorType m_functor;
00097 };
00098 
00099 template < typename Type >
00100 WGEFunctorCallback< Type >::WGEFunctorCallback( FunctorType functor ):
00101     WGECallbackTraits< Type >::CallbackType(),
00102     m_functor( functor )
00103 {
00104     // initialize members
00105 }
00106 
00107 template < typename Type >
00108 WGEFunctorCallback< Type >::~WGEFunctorCallback()
00109 {
00110     // cleanup
00111 }
00112 
00113 template < typename Type >
00114 void WGEFunctorCallback< Type >::operator()( Type* handled, osg::NodeVisitor* nv )
00115 {
00116     // call functor
00117     m_functor( handled );
00118     WGECallbackTraits< Type >::traverse( this, handled, nv );
00119 }
00120 
00121 template < typename Type >
00122 void WGEFunctorCallback< Type >::update( osg::NodeVisitor* nv, Type* handled )
00123 {
00124     operator()( handled, nv );
00125 }
00126 
00127 #endif  // WGEFUNCTORCALLBACK_H
00128