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 00051 #include <OSGSystemDef.h> 00052 #include <OSGBaseTypes.h> 00053 #include <OSGFieldContainerType.h> 00054 #include <OSGNodePtr.h> 00055 00056 #include <OSGTypedFunctors.h> 00057 00058 OSG_BEGIN_NAMESPACE 00059 00060 //--------------------------------------------------------------------------- 00061 // Forward References 00062 //--------------------------------------------------------------------------- 00063 00064 //--------------------------------------------------------------------------- 00065 // Types 00066 //--------------------------------------------------------------------------- 00067 00068 class Node; 00069 class Action; 00070 00071 //--------------------------------------------------------------------------- 00072 // Class 00073 //--------------------------------------------------------------------------- 00074 00075 00079 class OSG_SYSTEMLIB_DLLMAPPING Action 00080 { 00081 public: 00082 00083 //----------------------------------------------------------------------- 00084 // enums 00085 //----------------------------------------------------------------------- 00086 00087 enum ResultE 00088 { 00089 Continue, // continue with my children 00090 Skip, // skip my children 00091 // really needed? Cancel, // skip my brothers, go one step up 00092 Quit // forget it, you're done 00093 }; 00094 00095 typedef ArgsCollector<Action *> FunctorArgs; 00096 00097 typedef TypedFunctor2Base<ResultE, 00098 CPtrRefCallArg<CNodePtr>, 00099 FunctorArgs > Functor; 00100 00101 //----------------------------------------------------------------------- 00102 // types 00103 //----------------------------------------------------------------------- 00104 00105 //----------------------------------------------------------------------- 00106 // class functions 00107 //----------------------------------------------------------------------- 00108 00109 static const char *getClassname(void) { return "Action"; } 00110 00111 // create a new action by cloning the prototype 00112 static Action * create( void ); 00113 00114 // prototype access 00115 // after setting the prototype all new actions are clones of it 00116 static void setPrototype( Action * proto ); 00117 static Action *getPrototype( void ); 00118 00119 //----------------------------------------------------------------------- 00120 // instance functions 00121 //----------------------------------------------------------------------- 00122 00123 virtual ~Action(void); 00124 00125 /*------------------------- your_category -------------------------------*/ 00126 00127 // default registration. static, so it can be called during static init 00128 00129 static void registerEnterDefault( const FieldContainerType &type, 00130 const Functor &func); 00131 00132 static void registerLeaveDefault( const FieldContainerType &type, 00133 const Functor &func); 00134 00135 // instance registration 00136 00137 void registerEnterFunction( const FieldContainerType &type, 00138 const Functor &func); 00139 00140 void registerLeaveFunction( const FieldContainerType &type, 00141 const Functor &func); 00142 00143 // application 00144 00145 virtual ResultE apply(std::vector<NodePtr>::iterator begin, 00146 std::vector<NodePtr>::iterator end); 00147 00148 virtual ResultE apply(NodePtr node); 00149 00150 00151 // the node being traversed. Might be needed by the traversed core 00152 00153 inline NodePtr getActNode( void ); 00154 00155 00156 // Node access: 00157 // the number of active nodes 00158 00159 UInt32 getNNodes( void ) const; 00160 00161 // you can access a single node by getNode 00162 00163 const NodePtr getNode( int index ); 00164 00165 // per default all child nodes are traversed. If addNode is called, only the 00166 // added nodes will be traversed. 00167 00168 void addNode( NodePtr node ); 00169 00170 // Common case: going through the children list and picking up some of them, 00171 // but it's not clear if any at all. Call useNodeList() and then 00172 // addNode() for every node to traverse, or not at all. 00173 00174 void useNodeList( void ); 00175 00176 /*------------------------- your_operators ------------------------------*/ 00177 00178 00179 /*------------------------- assignment ----------------------------------*/ 00180 00181 inline UInt32 getTravMask (void) const; 00182 00183 inline void setTravMask (UInt32 val); 00184 00185 00186 /*------------------------- comparison ----------------------------------*/ 00187 00188 bool operator < (const Action &other); 00189 00190 bool operator == (const Action &other); 00191 bool operator != (const Action &other); 00192 00193 protected: 00194 00195 //----------------------------------------------------------------------- 00196 // enums 00197 //----------------------------------------------------------------------- 00198 00199 //----------------------------------------------------------------------- 00200 // types 00201 //----------------------------------------------------------------------- 00202 00203 //----------------------------------------------------------------------- 00204 // class variables 00205 //----------------------------------------------------------------------- 00206 00207 //----------------------------------------------------------------------- 00208 // class functions 00209 //----------------------------------------------------------------------- 00210 00211 //----------------------------------------------------------------------- 00212 // instance variables 00213 //----------------------------------------------------------------------- 00214 00215 //----------------------------------------------------------------------- 00216 // instance functions 00217 //----------------------------------------------------------------------- 00218 00219 // protected to allow derived access 00220 Action(void); 00221 Action(const Action &source); 00222 00223 // call the single node. used for cascading actions 00224 00225 inline ResultE callEnter( NodePtr node ); 00226 inline ResultE callLeave( NodePtr node ); 00227 00228 // start/stop functions for the action. 00229 // called at the very beginning/end, can return a list of nodes 00230 // via addNode() which is traversed before/after the traversal is done 00231 // main use: collecting actions use stop() to emit their collection 00232 00233 virtual ResultE start( void ); 00234 virtual ResultE stop( ResultE res ); // res is the exit code of the action 00235 00236 // recurse through the node 00237 00238 ResultE recurse( NodePtr node ); 00239 00240 // call the _newList list of nodes 00241 00242 ResultE callNewList( void ); 00243 00244 // access default functors 00245 00246 virtual std::vector<Functor>* getDefaultEnterFunctors( void ); 00247 virtual std::vector<Functor>* getDefaultLeaveFunctors( void ); 00248 00249 // default function 00250 00251 static ResultE _defaultEnterFunction( CNodePtr &node, Action *action); 00252 static ResultE _defaultLeaveFunction( CNodePtr &node, Action *action); 00253 00254 // functors 00255 // just protected, so that derived actions can access them 00256 00257 std::vector<Functor> _enterFunctors; 00258 std::vector<Functor> _leaveFunctors; 00259 00260 // the node being traversed. Might be needed by the traversed core 00261 // needs to be set by the RenderAction, as the draw tree is traversed 00262 // after the graph traversal 00263 00264 inline void setActNode(NodePtr node); 00265 00266 private: 00267 00268 //----------------------------------------------------------------------- 00269 // enums 00270 //----------------------------------------------------------------------- 00271 00272 //----------------------------------------------------------------------- 00273 // types 00274 //----------------------------------------------------------------------- 00275 00276 //----------------------------------------------------------------------- 00277 // friend classes 00278 //----------------------------------------------------------------------- 00279 00280 //----------------------------------------------------------------------- 00281 // friend functions 00282 //----------------------------------------------------------------------- 00283 00284 //----------------------------------------------------------------------- 00285 // class variables 00286 //----------------------------------------------------------------------- 00287 00288 static char cvsid[]; 00289 00290 // the prototype which is copied to create new actions 00291 static Action * _prototype; 00292 00293 // default functors for instantiation 00294 static std::vector<Functor> *_defaultEnterFunctors; 00295 static std::vector<Functor> *_defaultLeaveFunctors; 00296 00297 //----------------------------------------------------------------------- 00298 // class functions 00299 //----------------------------------------------------------------------- 00300 00301 //----------------------------------------------------------------------- 00302 // instance variables 00303 //----------------------------------------------------------------------- 00304 00305 NodePtr _actNode; // the node being traversed right now 00306 00307 std::vector<NodePtr> *_actList; // list of active objects for this level 00308 // if empty, use the actNode's children 00309 00310 bool _useNewList;// set by clearNodeList 00311 std::vector<NodePtr> _newList; // list of active object for this level 00312 00313 UInt32 _travMask; 00314 00315 //----------------------------------------------------------------------- 00316 // instance functions 00317 //----------------------------------------------------------------------- 00318 00319 // helper functions for start/stop, that also call the results of 00320 // start/stop 00321 00322 ResultE callStart( void ); 00323 ResultE callStop( ResultE res ); 00324 00325 Action& operator =(const Action &source); 00326 }; 00327 00328 //--------------------------------------------------------------------------- 00329 // Exported Types 00330 //--------------------------------------------------------------------------- 00331 00332 // class pointer 00333 00334 typedef Action *ActionP; 00335 00336 /*---------------------------------------------------------------------*/ 00340 typedef ArgsCollector<Action::ResultE> ArgsT; 00341 00342 typedef TypedFunctor1Base<Action::ResultE, 00343 CPtrRefCallArg<NodePtr> > TraverseEnterFunctor; 00344 typedef TypedFunctor2Base<Action::ResultE, 00345 CPtrRefCallArg<NodePtr>, 00346 ArgsT > TraverseLeaveFunctor; 00347 00348 OSG_SYSTEMLIB_DLLMAPPING 00349 Action::ResultE traverse(NodePtr root, 00350 TraverseEnterFunctor func); 00351 OSG_SYSTEMLIB_DLLMAPPING 00352 Action::ResultE traverse(std::vector<NodePtr> &nodeList, 00353 TraverseEnterFunctor func); 00354 00355 OSG_SYSTEMLIB_DLLMAPPING 00356 Action::ResultE traverse(NodePtr root, 00357 TraverseEnterFunctor enter, 00358 TraverseLeaveFunctor leave); 00359 OSG_SYSTEMLIB_DLLMAPPING 00360 Action::ResultE traverse(std::vector<NodePtr> &nodeList, 00361 TraverseEnterFunctor enter, 00362 TraverseLeaveFunctor leave); 00363 00366 OSG_END_NAMESPACE 00367 00368 #include "OSGAction.inl" 00369 00370 #endif /* _OSGACTION_H_ */ 00371 00372
1.5.5