GNodeVisitor.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 //
00026 // C++ Interface: GNodeVisitor
00027 //
00028 // Description: 
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2008
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 #ifndef sofa_simulation_treeGNodeVisitor_h
00037 #define sofa_simulation_treeGNodeVisitor_h
00038 
00039 #include <sofa/simulation/common/Visitor.h>
00040 #include <sofa/simulation/tree/GNode.h>
00041 
00042 namespace sofa {
00043 
00044 namespace simulation {
00045 
00046 namespace tree {
00047 
00053 class GNodeVisitor : public sofa::simulation::Visitor
00054 {
00055 public:
00056     GNodeVisitor();
00057 
00058     ~GNodeVisitor();
00059 
00061     virtual Result processNodeTopDown(GNode* /*node*/) { return RESULT_CONTINUE; }
00062 
00064     virtual void processNodeBottomUp(GNode* /*node*/) { }
00065 
00068     virtual Result processNodeTopDown(GNode* node, LocalStorage*) { return processNodeTopDown(node); }
00069 
00072     virtual void processNodeBottomUp(GNode* node, LocalStorage*) { processNodeBottomUp(node); }
00073 
00075     virtual Result processNodeTopDown(simulation::Node* node)
00076     {
00077     GNode* g = dynamic_cast<GNode*>(node);
00078     if (!g)
00079     {
00080         std::cerr << "GNodeVisitor: node is not a GNode !\n";
00081         return RESULT_PRUNE;
00082     }
00083     else
00084     {
00085         return processNodeTopDown(g);
00086     }
00087     }
00088 
00090     virtual void processNodeBottomUp(simulation::Node* node)
00091     {
00092     GNode* g = dynamic_cast<GNode*>(node);
00093     if (!g)
00094     {
00095         std::cerr << "GNodeVisitor: node is not a GNode !\n";
00096     }
00097     else
00098     {
00099         processNodeBottomUp(g);
00100     }
00101     }
00102 
00104     template < class Act, class Container, class Object >
00105     void for_each(Act* action, GNode* node, const Container& list, void (Act::*fn)(GNode*, Object*))
00106     {
00107         if (node->getLogTime())
00108         {
00109             const std::string category = getCategoryName();
00110             ctime_t t0 = node->startTime();
00111             for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00112             {
00113                 (action->*fn)(node, *it);
00114                 t0 = node->endTime(t0, category, *it);
00115             }
00116         }
00117         else
00118         {
00119             for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00120             {
00121                 (action->*fn)(node, *it);
00122             }
00123         }
00124     }
00125 
00127         template < class Act, class Container, class Object >
00128                 Visitor::Result for_each_r(Act* action, GNode* node, const Container& list, Visitor::Result (Act::*fn)(GNode*, Object*))
00129                 {
00130                     Visitor::Result res = Visitor::RESULT_CONTINUE;
00131                     if (node->getLogTime())
00132                     {
00133                         const std::string category = getCategoryName();
00134                         ctime_t t0 = node->startTime();
00135                         for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00136                         {
00137                             res = (action->*fn)(node, *it);
00138                             t0 = node->endTime(t0, category, *it);
00139                         }
00140                     }
00141                     else
00142                     {
00143                         for (typename Container::iterator it=list.begin(); it != list.end(); ++it)
00144                         {
00145                             res = (action->*fn)(node, *it);
00146                         }
00147                     }
00148                     return res;
00149                 }
00150 
00151 };
00152 
00153 }
00154 
00155 }
00156 
00157 }
00158 
00159 #endif

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