Visitor.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef SOFA_SIMULATION_ACTION_H
00026 #define SOFA_SIMULATION_ACTION_H
00027
00028 #include <sofa/simulation/common/Node.h>
00029 #include <sofa/simulation/common/LocalStorage.h>
00030
00031 namespace sofa
00032 {
00033
00034 namespace simulation
00035 {
00036
00037
00038 class LocalStorage;
00039
00041 class Visitor
00042 {
00043 public:
00044 virtual ~Visitor() {}
00045
00046 enum Result { RESULT_CONTINUE, RESULT_PRUNE };
00047
00049 virtual Result processNodeTopDown(simulation::Node* ) { return RESULT_CONTINUE; }
00050
00052 virtual void processNodeBottomUp(simulation::Node* ) {}
00053
00056 virtual const char* getCategoryName() const { return "default"; }
00057
00059 template < class Act, class Container, class Object >
00060 void for_each(Act* action, simulation::Node* node, const Container& list, void (Act::*fn)(simulation::Node*, Object*))
00061 {
00062
00063 if (node->getLogTime())
00064 {
00065 const std::string category = getCategoryName();
00066 ctime_t t0 = node->startTime();
00067 for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00068 {
00069 (action->*fn)(node, *it);
00070 t0 = node->endTime(t0, category, *it);
00071 }
00072 }
00073 else
00074 {
00075 for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00076 {
00077 (action->*fn)(node, *it);
00078 }
00079 }
00080
00081 }
00082
00084 template < class Act, class Container, class Object >
00085 Visitor::Result for_each_r(Act* action, simulation::Node* node, const Container& list, Visitor::Result (Act::*fn)(simulation::Node*, Object*))
00086 {
00087
00088 Visitor::Result res = Visitor::RESULT_CONTINUE;
00089 if (node->getLogTime())
00090 {
00091 const std::string category = getCategoryName();
00092 ctime_t t0 = node->startTime();
00093 for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00094 {
00095 res = (action->*fn)(node, *it);
00096 t0 = node->endTime(t0, category, *it);
00097 }
00098 }
00099 else
00100 {
00101 for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00102 {
00103 res = (action->*fn)(node, *it);
00104 }
00105 }
00106 return res;
00107
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 typedef simulation::Node::ctime_t ctime_t;
00120
00123 ctime_t begin(simulation::Node* node, core::objectmodel::BaseObject* )
00124 {
00125 return node->startTime();
00126 }
00127
00130 void end(simulation::Node* node, core::objectmodel::BaseObject* obj, ctime_t t0)
00131 {
00132 node->endTime(t0, getCategoryName(), obj);
00133 }
00134
00136 void execute(core::objectmodel::BaseContext*);
00137
00138
00140 virtual bool isThreadSafe() const { return false; }
00141
00144 virtual Result processNodeTopDown(simulation::Node* node, LocalStorage*) { return processNodeTopDown(node); }
00145
00148 virtual void processNodeBottomUp(simulation::Node* node, LocalStorage*) { processNodeBottomUp(node); }
00149 };
00150
00151 }
00152
00153 }
00154
00155 #endif