fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Action / Base / OSGAction.h
blobe2531868f6303c431c031acd32f55397eac99510
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGACTION_H_
40 #define _OSGACTION_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 //---------------------------------------------------------------------------
46 // Includes
47 //---------------------------------------------------------------------------
49 #include <vector>
50 #include <stack>
52 #include <boost/function.hpp>
54 #include "OSGSystemDef.h"
55 #include "OSGBaseTypes.h"
56 #include "OSGFieldContainerType.h"
57 #include "OSGContainerForwards.h"
58 #include "OSGNode.h"
59 #include "OSGNodeCore.h"
60 #include "OSGActionBase.h"
62 OSG_BEGIN_NAMESPACE
64 //---------------------------------------------------------------------------
65 // Forward References
66 //---------------------------------------------------------------------------
68 //---------------------------------------------------------------------------
69 // Types
70 //---------------------------------------------------------------------------
72 class Node;
73 class Action;
74 class MultiCore;
76 template <class ParentT>
77 class StageHandlerMixin;
79 //---------------------------------------------------------------------------
80 // Class
81 //---------------------------------------------------------------------------
83 /*! \brief Action base class
84 \ingroup GrpSystemActionBase
85 \ingroup GrpLibOSGSystem
88 class OSG_SYSTEM_DLLMAPPING Action : public ActionBase
90 public:
92 OSG_GEN_INTERNAL_MEMOBJPTR(Action);
94 //-----------------------------------------------------------------------
95 // enums
96 //-----------------------------------------------------------------------
98 typedef boost::function<ResultE(NodeCore * const,
99 Action * )> Functor;
100 typedef boost::function<ResultE(Node * const,
101 Action * )> NodeFunctor;
103 typedef std::vector<Functor> FunctorStore;
104 typedef FunctorStore::iterator FunctorStoreIt;
105 typedef FunctorStore::const_iterator FunctorStoreConstIt;
107 typedef Action::ResultE (NodeCore::*Callback)(Action *);
110 //-----------------------------------------------------------------------
111 // types
112 //-----------------------------------------------------------------------
114 //-----------------------------------------------------------------------
115 // class functions
116 //-----------------------------------------------------------------------
118 // create a new action by cloning the prototype
119 static ObjTransitPtr create(void);
121 // prototype access
122 // after setting the prototype all new actions are clones of it
124 static void setPrototype(Action *proto);
125 static Action *getPrototype(void );
127 //-----------------------------------------------------------------------
128 // instance functions
129 //-----------------------------------------------------------------------
131 /*------------------------- your_category -------------------------------*/
133 // default registration. static, so it can be called during static init
135 static void registerEnterDefault (const FieldContainerType &type,
136 const Functor &func);
138 static void registerLeaveDefault (const FieldContainerType &type,
139 const Functor &func);
141 // instance registration
143 void registerEnterFunction(const FieldContainerType &type,
144 const Functor &func);
146 void registerLeaveFunction(const FieldContainerType &type,
147 const Functor &func);
149 /*------------------------- your_category -------------------------------*/
151 // application
153 virtual ResultE apply(std::vector<Node *>::iterator begin,
154 std::vector<Node *>::iterator end );
156 virtual ResultE apply(Node * const node);
158 /*------------------------- your_category -------------------------------*/
160 // the node being traversed. Might be needed by the traversed core
162 inline Node *getActNode (void);
163 inline FieldContainer *getActParent(void);
165 // the node being traversed. Might be needed by the traversed core
166 // needs to be set by the RenderAction, as the draw tree is traversed
167 // after the graph traversal
169 void setActNode(Node * const node);
171 /*------------------------- your_category -------------------------------*/
173 // Node access:
174 // the number of active nodes
176 UInt32 getNNodes (void ) const;
178 // you can access a single node by getNode
180 Node *getNode (UInt32 index);
182 // per default all child nodes are traversed. If addNode is called,
183 // only the added nodes will be traversed.
185 void addNode (Node * const node);
187 // Common case: going through the children list and picking up some of
188 // them, but it's not clear if any at all. Call useNodeList() and then
189 // addNode() for every node to traverse, or not at all.
191 void useNodeList(bool bVal = true );
193 /*------------------------- assignment ----------------------------------*/
195 UInt32 getTravMask (void ) const;
196 void setTravMask (UInt32 val);
198 void andTravMask (UInt32 val);
199 void orTravMask (UInt32 val);
201 /*------------------------- comparison ----------------------------------*/
203 void pushTravMask(void);
204 void popTravMask (void);
206 /*------------------------- comparison ----------------------------------*/
208 bool operator < (const Action &other);
210 bool operator == (const Action &other);
211 bool operator != (const Action &other);
213 protected:
215 //-----------------------------------------------------------------------
216 // enums
217 //-----------------------------------------------------------------------
219 //-----------------------------------------------------------------------
220 // types
221 //-----------------------------------------------------------------------
223 typedef ActionBase Inherited;
225 //-----------------------------------------------------------------------
226 // class variables
227 //-----------------------------------------------------------------------
229 //-----------------------------------------------------------------------
230 // class functions
231 //-----------------------------------------------------------------------
233 //-----------------------------------------------------------------------
234 // instance variables
235 //-----------------------------------------------------------------------
237 //-----------------------------------------------------------------------
238 // instance functions
239 //-----------------------------------------------------------------------
241 // protected to allow derived access
242 Action(void);
243 Action(const Action &source);
245 virtual ~Action(void);
247 // call the single node. used for cascading actions
249 inline ResultE callEnter(NodeCore * const core);
250 inline ResultE callLeave(NodeCore * const core);
252 // start/stop functions for the action.
253 // called at the very beginning/end, can return a list of nodes
254 // via addNode() which is traversed before/after the traversal is done
255 // main use: collecting actions use stop() to emit their collection
257 virtual ResultE start(void );
258 virtual ResultE stop (ResultE res); // res is the exit code of the action
260 // recurse through the node
261 ResultE recurse (Node * const node );
262 ResultE recurseNoCallback (Node * const node );
263 ResultE recurseMultiCoreFrom(Node * const node,
264 MultiCore * const core,
265 NodeCore * const from );
267 // call the _newList list of nodes
269 ResultE callNewList(void);
271 void setActParent(FieldContainer * const parent);
273 // access default functors
275 virtual FunctorStore* getDefaultEnterFunctors(void);
276 virtual FunctorStore* getDefaultLeaveFunctors(void);
278 // default function
280 static ResultE _defaultEnterFunction(NodeCore * const node,
281 Action * action);
282 static ResultE _defaultLeaveFunction(NodeCore * const node,
283 Action * action);
285 // functors
286 // just protected, so that derived actions can access them
288 FunctorStore _enterFunctors;
289 FunctorStore _leaveFunctors;
292 //-----------------------------------------------------------------------
293 // instance variables
294 //-----------------------------------------------------------------------
296 Node *_actNode; // the node being traversed right now
297 FieldContainer *_actParent;
299 std::vector<Node *> *_actList; // list of active objects for this level
300 // if empty, use the actNode's children
302 bool _useNewList;// set by clearNodeList
303 std::vector<Node *> _newList; // list of active object for this level
305 UInt32 _travMask;
306 std::stack<UInt32> _sTravMask;
308 NodeFunctor _nodeEnterCB;
309 NodeFunctor _nodeLeaveCB;
311 private:
313 //-----------------------------------------------------------------------
314 // enums
315 //-----------------------------------------------------------------------
317 //-----------------------------------------------------------------------
318 // types
319 //-----------------------------------------------------------------------
321 //-----------------------------------------------------------------------
322 // friend classes
323 //-----------------------------------------------------------------------
325 //-----------------------------------------------------------------------
326 // friend functions
327 //-----------------------------------------------------------------------
329 template <class ParentT>
330 friend class StageHandlerMixin;
332 friend class MultiCore;
334 //-----------------------------------------------------------------------
335 // class variables
336 //-----------------------------------------------------------------------
338 // the prototype which is copied to create new actions
339 static Action *_prototype;
341 // default functors for instantiation
342 static FunctorStore *_defaultEnterFunctors;
343 static FunctorStore *_defaultLeaveFunctors;
345 //-----------------------------------------------------------------------
346 // class functions
347 //-----------------------------------------------------------------------
349 //-----------------------------------------------------------------------
350 // instance functions
351 //-----------------------------------------------------------------------
353 // helper functions for start/stop, that also call the results of
354 // start/stop
356 ResultE callStart(void );
357 ResultE callStop (ResultE res);
359 Action& operator =(const Action &source);
362 //---------------------------------------------------------------------------
363 // Exported Types
364 //---------------------------------------------------------------------------
366 // class pointer
368 typedef Action *ActionP;
370 OSG_GEN_MEMOBJPTR(Action);
372 /*---------------------------------------------------------------------*/
373 /*! \name Traversal Functions */
374 /*! \{ */
376 /*! \ingroup GrpSystemActionFuncs
379 typedef boost::function<
380 Action::ResultE (Node * const )> TraverseEnterFunctor;
382 /*! \ingroup GrpSystemActionFuncs
385 typedef boost::function<
386 Action::ResultE (Node * const,
387 Action::ResultE)> TraverseLeaveFunctor;
389 /*! \ingroup GrpSystemActionFuncs
392 typedef boost::function<Action::ResultE (Action *)> RenderActionFunctor;
394 /*! \ingroup GrpSystemActionFuncs
397 OSG_SYSTEM_DLLMAPPING
398 Action::ResultE traverse( Node * const root,
399 TraverseEnterFunctor func);
401 /*! \ingroup GrpSystemActionFuncs
404 OSG_SYSTEM_DLLMAPPING
405 Action::ResultE traverse(const std::vector<Node *> &nodeList,
406 TraverseEnterFunctor func);
408 /*! \ingroup GrpSystemActionFuncs
411 OSG_SYSTEM_DLLMAPPING
412 Action::ResultE traverse(const MFUnrecChildNodePtr &nodeList,
413 TraverseEnterFunctor func);
415 /*! \ingroup GrpSystemActionFuncs
418 OSG_SYSTEM_DLLMAPPING
419 Action::ResultE traverse( Node * const root,
420 TraverseEnterFunctor enter,
421 TraverseLeaveFunctor leave);
422 /*! \ingroup GrpSystemActionFuncs
425 OSG_SYSTEM_DLLMAPPING
426 Action::ResultE traverse(const std::vector<Node *> &nodeList,
427 TraverseEnterFunctor enter,
428 TraverseLeaveFunctor leave);
430 /*! \ingroup GrpSystemActionFuncs
433 OSG_SYSTEM_DLLMAPPING
434 Action::ResultE traverse(const MFUnrecChildNodePtr &nodeList,
435 TraverseEnterFunctor enter,
436 TraverseLeaveFunctor leave);
438 /*! \} */
439 /*---------------------------------------------------------------------*/
441 OSG_END_NAMESPACE
443 #include "OSGAction.inl"
445 #endif /* _OSGACTION_H_ */