Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

node.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: node.cpp,v 1.13 2004/05/14 12:33:43 fruit Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 
00022    Class
00023 
00024    HISTORY:
00025                 04.06.2002 MK
00026                         - initial version
00027 
00028 */
00029 #include <boost/tokenizer.hpp>
00030 #include "node.h"
00031 #include <salt/path.h>
00032 #include <iostream>
00033 
00034 /*
00035 std::ostream& zeitgeist::operator<<(std::ostream& output, const Node& node)
00036 {
00037         output << (int)&node << " '" << node.GetName() << "'" << std::endl;
00038 
00039         //for (Node::TLeafList::const_iterator i=leaf.mChildren.begin(); i!=leaf.mChildren.end(); ++i)
00040         //{
00041         //      output << *(*i);
00042         //}
00043         return output;
00044 }*/
00045 
00046 //=============================================================================
00047 using namespace boost;
00048 using namespace salt;
00049 using namespace std;
00050 using namespace zeitgeist;
00051 
00052 
00053 Node::Node(const std::string& name) : Leaf(name)
00054 {
00055 }
00056 
00057 Node::~Node()
00058 {
00059 }
00060 
00061 boost::shared_ptr<Leaf>
00062 Node::GetChild(const std::string& name, bool recursive)
00063 {
00064     shared_ptr<Leaf> leaf   = Leaf::GetChild(name, recursive);
00065 
00066     if (leaf.get() != NULL) return leaf;
00067 
00068     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00069     {
00070         // check if we have found a match and return it
00071         if ((*i)->GetName().compare(name) == 0)
00072             return (*i);
00073 
00074         if (recursive)
00075         {
00076             shared_ptr<Leaf> ret = (*i)->GetChild(name, recursive);
00077             if (ret.get() != NULL) return ret;
00078         }
00079     }
00080 
00081     return boost::shared_ptr<Leaf>();
00082 }
00083 
00084 boost::shared_ptr<Leaf>
00085 Node::GetChildOfClass(const std::string& name, bool recursive)
00086 {
00087     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00088     {
00089         // check if we have found a match and add it
00090         shared_ptr<Class> theClass = (*i)->GetClass();
00091         if (theClass.get() != NULL && theClass->GetName().compare(name) == 0)
00092         {
00093             return (*i);
00094         }
00095 
00096         if (recursive)
00097         {
00098             shared_ptr<Leaf> ret = (*i)->GetChildOfClass(name, recursive);
00099             if (ret.get() != NULL) return ret;
00100         }
00101     }
00102 
00103     return shared_ptr<Leaf>();
00104 }
00105 
00106 boost::shared_ptr<Leaf>
00107 Node::GetChildSupportingClass(const std::string& name, bool recursive)
00108 {
00109     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00110     {
00111         // check if we have found a match and add it
00112         shared_ptr<Class> theClass = (*i)->GetClass();
00113         if (theClass.get() != NULL && theClass->SupportsClass(name))
00114         {
00115             return (*i);
00116         }
00117 
00118         if (recursive)
00119         {
00120             shared_ptr<Leaf> ret = (*i)->GetChildSupportingClass(name, recursive);
00121             if (ret.get() != NULL) return ret;
00122         }
00123     }
00124 
00125     return shared_ptr<Leaf>();
00126 }
00127 
00128 void
00129 Node::GetChildren(const std::string& name, TLeafList& baseList, bool recursive)
00130 {
00131     Leaf::GetChildren(name, baseList, recursive);
00132 
00133     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00134     {
00135         // check if we have found a match and add it
00136         if ((*i)->GetName().compare(name) == 0)
00137         {
00138             baseList.push_back(*i);
00139         }
00140 
00141         if (recursive)
00142             (*i)->GetChildren(name, baseList, recursive);
00143     }
00144 }
00145 
00146 void
00147 Node::GetChildrenOfClass(const std::string& name, TLeafList& baseList, bool recursive)
00148 {
00149     Leaf::GetChildrenOfClass(name, baseList, recursive);
00150 
00151     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00152     {
00153         // check if we have found a match and add it
00154         shared_ptr<Class> theClass = (*i)->GetClass();
00155         if (theClass.get() != NULL && theClass->GetName().compare(name) == 0)
00156         {
00157             baseList.push_back(*i);
00158         }
00159 
00160         if (recursive)
00161             (*i)->GetChildrenOfClass(name, baseList, recursive);
00162     }
00163 }
00164 
00165 void
00166 Node::GetChildrenSupportingClass(const std::string& name, TLeafList& baseList, bool recursive)
00167 {
00168     Leaf::GetChildrenSupportingClass(name, baseList, recursive);
00169 
00170     for (TLeafList::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
00171     {
00172         // check if we have found a match and add it
00173         shared_ptr<Class> theClass = (*i)->GetClass();
00174         if (theClass.get() != NULL && theClass->SupportsClass(name))
00175         {
00176             baseList.push_back(*i);
00177         }
00178 
00179         if (recursive)
00180         {
00181             (*i)->GetChildrenSupportingClass(name, baseList, recursive);
00182         }
00183     }
00184 }
00185 
00186 bool
00187 Node::IsLeaf() const
00188 {
00189     return false;
00190 }
00191 
00192 void
00193 Node::RemoveChildReference(const boost::shared_ptr<Leaf>& leaf)
00194 {
00195     mChildren.remove(leaf);
00196 }
00197 
00198 void
00199 Node::UnlinkChildren()
00200 {
00201     string cname;
00202     if (GetClass().get() != 0)
00203     {
00204         cname = GetClass()->GetName();
00205     }
00206 
00207     while (! mChildren.empty())
00208     {
00209         shared_ptr<Leaf> node = mChildren.front();
00210 
00211         string className;
00212         if (node->GetClass().get()!= 0)
00213         {
00214             className=node->GetClass()->GetName();
00215         }
00216         node->UnlinkChildren();
00217         node->Unlink();
00218     }
00219 }
00220 
00221 bool
00222 Node::AddChildReference(const boost::shared_ptr<Leaf>& leaf)
00223 {
00224     if (leaf.get() == 0)
00225     {
00226         return false;
00227     }
00228 
00229     if (leaf->GetClass() == 0)
00230     {
00231         if (leaf->GetName() != "ClassClass")
00232         {
00233             cerr << "(Node::AddChildReference) ERROR: object "
00234                  << leaf->GetName()
00235                  << " has no assigned class object." << endl;
00236         }
00237         return false;
00238     }
00239 
00240     mChildren.push_back(leaf);
00241     leaf->SetParent(shared_static_cast<Node>(make_shared(GetSelf())));
00242     return true;
00243 }
00244 
00245 void
00246 Node::Dump() const
00247 {
00248     Leaf::Dump();
00249     cout << "Node: numChildren = " << mChildren.size() << endl;
00250 
00251     for (TLeafList::const_iterator i = mChildren.begin(); i!=mChildren.end(); ++i)
00252     {
00253         (*i)->Dump();
00254         cout << endl;
00255     }
00256 
00257     cout << "End Node" << endl;
00258 }
00259 
00260 Leaf::TLeafList::iterator
00261 Node::begin()
00262 {
00263     return mChildren.begin();
00264 }
00265 
00266 Leaf::TLeafList::const_iterator
00267 Node::begin() const
00268 {
00269     return mChildren.begin();
00270 }
00271 
00272 Leaf::TLeafList::iterator
00273 Node::end()
00274 {
00275     return mChildren.end();
00276 }
00277 
00278 Leaf::TLeafList::const_iterator
00279 Node::end() const
00280 {
00281     return mChildren.end();
00282 }
00283 
00284 void
00285 Node::UpdateCached()
00286 {
00287     // update all Leaves found
00288     for (TLeafList::iterator iter = begin();
00289          iter != end();
00290          ++iter
00291         )
00292     {
00293         // node specific update
00294         (*iter)->UpdateCachedInternal();
00295 
00296         // recurse
00297         (*iter)->UpdateCached();
00298     }
00299 }
00300 

Generated on Thu Apr 6 15:25:39 2006 for rcssserver3d by  doxygen 1.4.4