OSGAction.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 #ifndef _OSGACTION_H_
00040 #define _OSGACTION_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044 
00045 //---------------------------------------------------------------------------
00046 //  Includes
00047 //---------------------------------------------------------------------------
00048
00049 #include <vector>
00050 #include <stack>
00051
00052 #include <boost/function.hpp>
00053
00054 #include "OSGSystemDef.h"
00055 #include "OSGBaseTypes.h"
00056 #include "OSGFieldContainerType.h"
00057 #include "OSGContainerForwards.h"
00058 #include "OSGNode.h"
00059 #include "OSGNodeCore.h"
00060 #include "OSGActionBase.h"
00061
00062 OSG_BEGIN_NAMESPACE
00063
00064 //---------------------------------------------------------------------------
00065 //  Forward References
00066 //---------------------------------------------------------------------------
00067
00068 //---------------------------------------------------------------------------
00069 //   Types
00070 //---------------------------------------------------------------------------
00071
00072 class Node;
00073 class Action;
00074 class MultiCore;
00075
00076 template <class ParentT>
00077 class StageHandlerMixin;
00078
00079 //---------------------------------------------------------------------------
00080 //  Class
00081 //---------------------------------------------------------------------------
00082
00086 class OSG_SYSTEM_DLLMAPPING Action : public ActionBase
00087 {
00088   public:
00089
00090     //-----------------------------------------------------------------------
00091     //   enums                                                               
00092     //-----------------------------------------------------------------------
00093
00094     typedef boost::function<ResultE(NodeCore * const,
00095                                     Action   *      )> Functor;
00096     typedef boost::function<ResultE(Node     * const,
00097                                     Action   *      )> NodeFunctor;
00098
00099     typedef std::vector<Functor>                       FunctorStore;
00100     typedef FunctorStore::iterator                     FunctorStoreIt;
00101     typedef FunctorStore::const_iterator               FunctorStoreConstIt;
00102
00103     typedef ActionBase::ResultE (NodeCore::*Callback)(Action *);
00104
00105
00106     //-----------------------------------------------------------------------
00107     //   types                                                               
00108     //-----------------------------------------------------------------------
00109
00110     //-----------------------------------------------------------------------
00111     //   class functions                                                     
00112     //-----------------------------------------------------------------------
00113
00114     // create a new action by cloning the prototype
00115     static Action *create(void);
00116
00117     // prototype access
00118     // after setting the prototype all new actions are clones of it
00119
00120     static void    setPrototype(Action *proto);
00121     static Action *getPrototype(void         );
00122
00123     //-----------------------------------------------------------------------
00124     //   instance functions                                                  
00125     //-----------------------------------------------------------------------
00126
00127     virtual ~Action(void);
00128
00129     /*------------------------- your_category -------------------------------*/
00130
00131     // default registration. static, so it can be called during static init
00132
00133     static void registerEnterDefault (const FieldContainerType &type,
00134                                       const Functor            &func);
00135
00136     static void registerLeaveDefault (const FieldContainerType &type,
00137                                       const Functor            &func);
00138
00139     // instance registration
00140
00141            void registerEnterFunction(const FieldContainerType &type,
00142                                       const Functor            &func);
00143
00144            void registerLeaveFunction(const FieldContainerType &type,
00145                                       const Functor            &func);
00146
00147     /*------------------------- your_category -------------------------------*/
00148
00149     // application
00150
00151     virtual ResultE apply(std::vector<Node *>::iterator begin,
00152                           std::vector<Node *>::iterator end  );
00153
00154     virtual ResultE apply(Node * const                   node);
00155
00156     /*------------------------- your_category -------------------------------*/
00157
00158     // the node being traversed. Might be needed by the traversed core
00159
00160     inline Node           *getActNode  (void);
00161     inline FieldContainer *getActParent(void);
00162
00163     // the node being traversed. Might be needed by the traversed core
00164     // needs to be set by the RenderAction, as the draw tree is traversed 
00165     // after the graph traversal
00166
00167     void setActNode(Node * const node);
00168
00169     /*------------------------- your_category -------------------------------*/
00170
00171     // Node access: 
00172     // the number of active nodes 
00173
00174     UInt32       getNNodes  (void                 ) const;
00175
00176     // you can access a single node by getNode
00177
00178     Node        *getNode    (int             index);
00179
00180     // per default all child nodes are traversed. If addNode is called, 
00181     // only the added nodes will be traversed.
00182
00183     void         addNode    (Node * const node);
00184
00185     // Common case: going through the children list and picking up some of 
00186     // them, but it's not clear if any at all. Call useNodeList() and then
00187     // addNode() for every node to traverse, or not at all. 
00188
00189     void         useNodeList(bool bVal = true    );
00190
00191     /*------------------------- assignment ----------------------------------*/
00192
00193     UInt32 getTravMask (void      ) const;
00194     void   setTravMask (UInt32 val);
00195
00196     void   andTravMask (UInt32 val);
00197
00198     /*------------------------- comparison ----------------------------------*/
00199
00200     void pushTravMask(void);
00201     void popTravMask (void);
00202
00203     /*------------------------- comparison ----------------------------------*/
00204
00205     bool operator <  (const Action &other);
00206
00207     bool operator == (const Action &other);
00208     bool operator != (const Action &other);
00209
00210   protected:
00211
00212     //-----------------------------------------------------------------------
00213     //   enums                                                               
00214     //-----------------------------------------------------------------------
00215
00216     //-----------------------------------------------------------------------
00217     //   types                                                               
00218     //-----------------------------------------------------------------------
00219
00220     //-----------------------------------------------------------------------
00221     //   class variables                                                     
00222     //-----------------------------------------------------------------------
00223
00224     //-----------------------------------------------------------------------
00225     //   class functions                                                     
00226     //-----------------------------------------------------------------------
00227
00228     //-----------------------------------------------------------------------
00229     //   instance variables                                                  
00230     //-----------------------------------------------------------------------
00231
00232     //-----------------------------------------------------------------------
00233     //   instance functions                                                  
00234     //-----------------------------------------------------------------------
00235
00236     // protected to allow derived access
00237     Action(void);
00238     Action(const Action &source);
00239
00240     // call the single node. used for cascading actions
00241
00242     inline ResultE callEnter(NodeCore * const core);
00243     inline ResultE callLeave(NodeCore * const core);
00244
00245     // start/stop functions for the action.
00246     // called at the very beginning/end, can return a list of nodes
00247     // via addNode() which is traversed before/after the traversal is done
00248     // main use: collecting actions use stop() to emit their collection
00249
00250     virtual ResultE start(void       );
00251     virtual ResultE stop (ResultE res); // res is the exit code of the action
00252
00253     // recurse through the node
00254     ResultE recurse(Node * const node);
00255
00256     // call the _newList list of nodes
00257
00258     ResultE callNewList(void);
00259
00260     void setActParent(FieldContainer * const parent);
00261
00262     // access default functors
00263
00264     virtual FunctorStore* getDefaultEnterFunctors(void);
00265     virtual FunctorStore* getDefaultLeaveFunctors(void);
00266
00267     // default function
00268
00269     static ResultE _defaultEnterFunction(NodeCore * const node,
00270                                          Action   *       action);
00271     static ResultE _defaultLeaveFunction(NodeCore * const node,
00272                                          Action   *       action);
00273
00274     // functors
00275     // just protected, so that derived actions can access them
00276
00277     FunctorStore _enterFunctors;
00278     FunctorStore _leaveFunctors;
00279
00280
00281     //-----------------------------------------------------------------------
00282     //   instance variables                                                  
00283     //-----------------------------------------------------------------------
00284
00285     Node                 *_actNode;   // the node being traversed right now
00286     FieldContainer       *_actParent;
00287
00288     std::vector<Node  *> *_actList;  // list of active objects for this level
00289                                      // if empty, use the actNode's children
00290
00291     bool                  _useNewList;// set by clearNodeList
00292     std::vector<Node  *>  _newList;   // list of active object for this level
00293
00294     UInt32                _travMask;
00295     std::stack<UInt32>    _sTravMask;
00296
00297     NodeFunctor           _nodeEnterCB;
00298     NodeFunctor           _nodeLeaveCB;
00299
00300   private:
00301
00302     //-----------------------------------------------------------------------
00303     //   enums                                                               
00304     //-----------------------------------------------------------------------
00305
00306     //-----------------------------------------------------------------------
00307     //   types                                                               
00308     //-----------------------------------------------------------------------
00309
00310     //-----------------------------------------------------------------------
00311     //   friend classes                                                      
00312     //-----------------------------------------------------------------------
00313
00314     //-----------------------------------------------------------------------
00315     //   friend functions                                                    
00316     //-----------------------------------------------------------------------
00317
00318     template <class ParentT>
00319     friend class StageHandlerMixin;
00320
00321     friend class MultiCore;
00322
00323     //-----------------------------------------------------------------------
00324     //   class variables                                                     
00325     //-----------------------------------------------------------------------
00326
00327     // the prototype which is copied to create new actions
00328     static Action * _prototype;
00329
00330     // default functors for instantiation
00331     static std::vector<Functor> *_defaultEnterFunctors;
00332     static std::vector<Functor> *_defaultLeaveFunctors;
00333
00334     //-----------------------------------------------------------------------
00335     //   class functions                                                     
00336     //-----------------------------------------------------------------------
00337
00338     //-----------------------------------------------------------------------
00339     //   instance functions                                                  
00340     //-----------------------------------------------------------------------
00341
00342     // helper functions for start/stop, that also call the results of 
00343     // start/stop
00344
00345     ResultE callStart(void       );
00346     ResultE callStop (ResultE res);
00347
00348     Action& operator =(const Action &source);
00349 };
00350
00351 //---------------------------------------------------------------------------
00352 //   Exported Types
00353 //---------------------------------------------------------------------------
00354
00355 // class pointer
00356
00357 typedef Action *ActionP;
00358
00359 /*---------------------------------------------------------------------*/
00363 typedef boost::function<
00364           Action::ResultE (Node * const   )> TraverseEnterFunctor;
00365 typedef boost::function<
00366           Action::ResultE (Node * const,
00367                            Action::ResultE)> TraverseLeaveFunctor;
00368
00369 typedef boost::function<ActionBase::ResultE (Action *)> RenderActionFunctor;
00370
00371 /*
00372 typedef ArgsCollector<ActionBase::ResultE> ArgsT;
00373 
00374 typedef TypedFunctor1Base<Action::ResultE, 
00375                           CPtrRefCallArg<NodePtr> > TraverseEnterFunctor;
00376 typedef TypedFunctor2Base<Action::ResultE, 
00377                           CPtrRefCallArg<NodePtr>, 
00378                           ArgsT                   > TraverseLeaveFunctor;
00379  */
00380
00381 OSG_SYSTEM_DLLMAPPING
00382 ActionBase::ResultE traverse(      Node * const          root,
00383                                    TraverseEnterFunctor  func);
00384 OSG_SYSTEM_DLLMAPPING
00385 ActionBase::ResultE traverse(const std::vector<Node *> &nodeList,
00386                                    TraverseEnterFunctor  func);
00387
00388 OSG_SYSTEM_DLLMAPPING
00389 ActionBase::ResultE traverse(const MFUnrecChildNodePtr  &nodeList,
00390                                    TraverseEnterFunctor  func);
00391
00392 OSG_SYSTEM_DLLMAPPING
00393 ActionBase::ResultE traverse(      Node * const          root,
00394                                    TraverseEnterFunctor  enter,
00395                                    TraverseLeaveFunctor  leave);
00396 OSG_SYSTEM_DLLMAPPING
00397 ActionBase::ResultE traverse(const std::vector<Node *>  &nodeList,
00398                                    TraverseEnterFunctor  enter,
00399                                    TraverseLeaveFunctor  leave);
00400
00401 OSG_SYSTEM_DLLMAPPING
00402 ActionBase::ResultE traverse(const MFUnrecChildNodePtr  &nodeList,
00403                                    TraverseEnterFunctor  enter,
00404                                    TraverseLeaveFunctor  leave);
00405
00408 OSG_END_NAMESPACE
00409
00410 #include "OSGAction.inl"
00411
00412 #endif /* _OSGACTION_H_ */
00413
00414