Visitor.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
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* /*node*/) { return RESULT_CONTINUE; }
00050 
00052     virtual void processNodeBottomUp(simulation::Node* /*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     //template < class Act, class Container, class Object >
00111     //void for_each(Act* action, const Container& list, void (Act::*fn)(Object))
00112     //{
00113     //  for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00114     //  {
00115     //      (action->*fn)(*it);
00116     //  }
00117     //}
00118 
00119     typedef simulation::Node::ctime_t ctime_t;
00120 
00123     ctime_t begin(simulation::Node* node, core::objectmodel::BaseObject* /*obj*/)
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 } // namespace simulation
00152 
00153 } // namespace sofa
00154 
00155 #endif

Generated on Sun Nov 23 04:05:47 2008 for Sofa by  doxygen 1.5.7.1