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 <boost/function.hpp> 00051 00052 #include "OSGSystemDef.h" 00053 #include "OSGBaseTypes.h" 00054 #include "OSGFieldContainerType.h" 00055 #include "OSGContainerForwards.h" 00056 #include "OSGNode.h" 00057 #include "OSGNodeCore.h" 00058 #include "OSGActionBase.h" 00059 00060 OSG_BEGIN_NAMESPACE 00061 00062 //--------------------------------------------------------------------------- 00063 // Forward References 00064 //--------------------------------------------------------------------------- 00065 00066 //--------------------------------------------------------------------------- 00067 // Types 00068 //--------------------------------------------------------------------------- 00069 00070 class Node; 00071 class Action; 00072 00073 ActionBase::ResultE MultiCoreRenderEnter(const NodeCorePtr &pCore, 00074 Action *action); 00075 ActionBase::ResultE MultiCoreRenderLeave(const NodeCorePtr &pCore, 00076 Action *action); 00077 00078 //--------------------------------------------------------------------------- 00079 // Class 00080 //--------------------------------------------------------------------------- 00081 00085 class OSG_SYSTEM_DLLMAPPING Action : public ActionBase 00086 { 00087 public: 00088 00089 //----------------------------------------------------------------------- 00090 // enums 00091 //----------------------------------------------------------------------- 00092 00093 /* 00094 typedef ArgsCollector<Action *> FunctorArgs; 00095 00096 typedef TypedFunctor2Base<ResultE, 00097 CPtrRefCallArg<NodeCorePtr>, 00098 FunctorArgs > Functor; 00099 */ 00100 00101 typedef boost::function<ResultE (NodeCorePtrConstArg, Action *)> Functor; 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<NodePtr>::iterator begin, 00152 std::vector<NodePtr>::iterator end ); 00153 00154 virtual ResultE apply(NodePtrConstArg node ); 00155 00156 /*------------------------- your_category -------------------------------*/ 00157 00158 // the node being traversed. Might be needed by the traversed core 00159 00160 inline NodePtr getActNode( void ); 00161 00162 // the node being traversed. Might be needed by the traversed core 00163 // needs to be set by the RenderAction, as the draw tree is traversed 00164 // after the graph traversal 00165 00166 void setActNode(NodePtrConstArg node); 00167 00168 /*------------------------- your_category -------------------------------*/ 00169 00170 // Node access: 00171 // the number of active nodes 00172 00173 UInt32 getNNodes (void ) const; 00174 00175 // you can access a single node by getNode 00176 00177 NodePtr getNode (int index); 00178 00179 // per default all child nodes are traversed. If addNode is called, 00180 // only the added nodes will be traversed. 00181 00182 void addNode (NodePtrConstArg node); 00183 00184 // Common case: going through the children list and picking up some of 00185 // them, but it's not clear if any at all. Call useNodeList() and then 00186 // addNode() for every node to traverse, or not at all. 00187 00188 void useNodeList(void ); 00189 00190 /*------------------------- assignment ----------------------------------*/ 00191 00192 UInt32 getTravMask (void ) const; 00193 00194 void setTravMask (UInt32 val); 00195 00196 /*------------------------- comparison ----------------------------------*/ 00197 00198 bool operator < (const Action &other); 00199 00200 bool operator == (const Action &other); 00201 bool operator != (const Action &other); 00202 00203 protected: 00204 00205 //----------------------------------------------------------------------- 00206 // enums 00207 //----------------------------------------------------------------------- 00208 00209 //----------------------------------------------------------------------- 00210 // types 00211 //----------------------------------------------------------------------- 00212 00213 //----------------------------------------------------------------------- 00214 // class variables 00215 //----------------------------------------------------------------------- 00216 00217 //----------------------------------------------------------------------- 00218 // class functions 00219 //----------------------------------------------------------------------- 00220 00221 //----------------------------------------------------------------------- 00222 // instance variables 00223 //----------------------------------------------------------------------- 00224 00225 //----------------------------------------------------------------------- 00226 // instance functions 00227 //----------------------------------------------------------------------- 00228 00229 // protected to allow derived access 00230 Action(void); 00231 Action(const Action &source); 00232 00233 // call the single node. used for cascading actions 00234 00235 inline ResultE callEnter(NodeCorePtrConstArg core); 00236 inline ResultE callLeave(NodeCorePtrConstArg core); 00237 00238 // start/stop functions for the action. 00239 // called at the very beginning/end, can return a list of nodes 00240 // via addNode() which is traversed before/after the traversal is done 00241 // main use: collecting actions use stop() to emit their collection 00242 00243 virtual ResultE start(void ); 00244 virtual ResultE stop (ResultE res); // res is the exit code of the action 00245 00246 // recurse through the node 00247 00248 ResultE recurse(NodePtrConstArg node); 00249 00250 // call the _newList list of nodes 00251 00252 ResultE callNewList(void); 00253 00254 // access default functors 00255 00256 virtual std::vector<Functor>* getDefaultEnterFunctors(void); 00257 virtual std::vector<Functor>* getDefaultLeaveFunctors(void); 00258 00259 // default function 00260 00261 static ResultE _defaultEnterFunction(NodeCorePtrConstArg node, 00262 Action *action); 00263 static ResultE _defaultLeaveFunction(NodeCorePtrConstArg node, 00264 Action *action); 00265 00266 // functors 00267 // just protected, so that derived actions can access them 00268 00269 std::vector<Functor> _enterFunctors; 00270 std::vector<Functor> _leaveFunctors; 00271 00272 00273 //----------------------------------------------------------------------- 00274 // instance variables 00275 //----------------------------------------------------------------------- 00276 00277 NodePtr _actNode; // the node being traversed right now 00278 00279 std::vector<NodePtr> *_actList; // list of active objects for this level 00280 // if empty, use the actNode's children 00281 00282 bool _useNewList;// set by clearNodeList 00283 std::vector<NodePtr> _newList; // list of active object for this level 00284 00285 UInt32 _travMask; 00286 00287 private: 00288 00289 //----------------------------------------------------------------------- 00290 // enums 00291 //----------------------------------------------------------------------- 00292 00293 //----------------------------------------------------------------------- 00294 // types 00295 //----------------------------------------------------------------------- 00296 00297 //----------------------------------------------------------------------- 00298 // friend classes 00299 //----------------------------------------------------------------------- 00300 00301 //----------------------------------------------------------------------- 00302 // friend functions 00303 //----------------------------------------------------------------------- 00304 00305 friend ActionBase::ResultE MultiCoreRenderEnter(const NodeCorePtr &pCore, 00306 Action *action); 00307 friend ActionBase::ResultE MultiCoreRenderLeave(const NodeCorePtr &pCore, 00308 Action *action); 00309 00310 //----------------------------------------------------------------------- 00311 // class variables 00312 //----------------------------------------------------------------------- 00313 00314 // the prototype which is copied to create new actions 00315 static Action * _prototype; 00316 00317 // default functors for instantiation 00318 static std::vector<Functor> *_defaultEnterFunctors; 00319 static std::vector<Functor> *_defaultLeaveFunctors; 00320 00321 //----------------------------------------------------------------------- 00322 // class functions 00323 //----------------------------------------------------------------------- 00324 00325 //----------------------------------------------------------------------- 00326 // instance functions 00327 //----------------------------------------------------------------------- 00328 00329 // helper functions for start/stop, that also call the results of 00330 // start/stop 00331 00332 ResultE callStart(void ); 00333 ResultE callStop (ResultE res); 00334 00335 Action& operator =(const Action &source); 00336 }; 00337 00338 //--------------------------------------------------------------------------- 00339 // Exported Types 00340 //--------------------------------------------------------------------------- 00341 00342 // class pointer 00343 00344 typedef Action *ActionP; 00345 00346 /*---------------------------------------------------------------------*/ 00350 typedef boost::function< 00351 Action::ResultE (NodePtrConstArg)> TraverseEnterFunctor; 00352 typedef boost::function< 00353 Action::ResultE (NodePtrConstArg, 00354 Action::ResultE)> TraverseLeaveFunctor; 00355 00356 00357 /* 00358 typedef ArgsCollector<ActionBase::ResultE> ArgsT; 00359 00360 typedef TypedFunctor1Base<Action::ResultE, 00361 CPtrRefCallArg<NodePtr> > TraverseEnterFunctor; 00362 typedef TypedFunctor2Base<Action::ResultE, 00363 CPtrRefCallArg<NodePtr>, 00364 ArgsT > TraverseLeaveFunctor; 00365 */ 00366 00367 OSG_SYSTEM_DLLMAPPING 00368 ActionBase::ResultE traverse(NodePtrConstArg root, 00369 TraverseEnterFunctor func); 00370 OSG_SYSTEM_DLLMAPPING 00371 ActionBase::ResultE traverse(const std::vector<NodePtr> &nodeList, 00372 TraverseEnterFunctor func); 00373 00374 OSG_SYSTEM_DLLMAPPING 00375 ActionBase::ResultE traverse(NodePtrConstArg root, 00376 TraverseEnterFunctor enter, 00377 TraverseLeaveFunctor leave); 00378 OSG_SYSTEM_DLLMAPPING 00379 ActionBase::ResultE traverse(const std::vector<NodePtr> &nodeList, 00380 TraverseEnterFunctor enter, 00381 TraverseLeaveFunctor leave); 00382 00385 OSG_END_NAMESPACE 00386 00387 #include "OSGAction.inl" 00388 00389 #endif /* _OSGACTION_H_ */ 00390 00391