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

leaf.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: leaf.cpp,v 1.9 2004/04/30 09:25:01 rollmark 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 #include "class.h"
00023 #include "node.h"
00024 #include <iostream>
00025 
00026 using namespace boost;
00027 using namespace std;
00028 using namespace zeitgeist;
00029 
00030 Leaf::Leaf(const std::string &name) : mName(name), mCachedFullPath(NULL)
00031 {
00032 }
00033 
00034 Leaf::~Leaf()
00035 {
00036 }
00037 
00038 boost::weak_ptr<Node>& Leaf::GetParent()
00039 {
00040     return mParent;
00041 }
00042 
00043 const boost::weak_ptr<Node>& Leaf::GetParent() const
00044 {
00045     return mParent;
00046 }
00047 
00048 boost::shared_ptr<Leaf> Leaf::GetChild(const std::string &name, bool /*recursive*/)
00049 {
00050     if (name.compare("..") == 0)
00051     {
00052         return make_shared(GetParent());
00053     }
00054 
00055     if (name.compare(".") == 0)
00056     {
00057 
00058         return shared_static_cast<Leaf>(make_shared(GetSelf()));
00059     }
00060 
00061     return boost::shared_ptr<Leaf>();
00062 }
00063 
00064 boost::shared_ptr<Leaf> Leaf::GetChildOfClass(const std::string &/*name*/, bool /*recursive*/)
00065 {
00066     return boost::shared_ptr<Leaf>();
00067 }
00068 
00069 boost::shared_ptr<Leaf> Leaf::GetChildSupportingClass(const std::string &/*name*/, bool /*recursive*/)
00070 {
00071     return boost::shared_ptr<Leaf>();
00072 }
00073 
00074 void Leaf::GetChildren(const std::string &name, TLeafList &baseList, bool /*recursive*/)
00075 {
00076     if (name.compare("..") == 0)
00077     {
00078         baseList.push_back(make_shared(GetParent()));
00079     }
00080 
00081     if (name.compare(".") == 0)
00082     {
00083         baseList.push_back(shared_static_cast<Leaf>(make_shared(GetSelf())));
00084     }
00085 }
00086 
00087 void Leaf::GetChildrenOfClass(const std::string &/*name*/, TLeafList &/*baseList*/, bool /*recursive*/)
00088 {
00089 }
00090 
00091 void Leaf::GetChildrenSupportingClass(const std::string &/*name*/, TLeafList &/*baseList*/, bool /*recursive*/)
00092 {
00093 }
00094 
00095 boost::weak_ptr<Node>
00096 Leaf::GetParentSupportingClass(const std::string &name) const
00097 {
00098   shared_ptr<Node> node
00099     = shared_static_cast<Node>(make_shared(GetParent()));
00100 
00101   while
00102     (
00103      (node.get() != 0) &&
00104      (node->GetClass()) &&
00105      (! node->GetClass()->SupportsClass(name))
00106      )
00107     {
00108       node = make_shared(node->GetParent());
00109     }
00110 
00111   return weak_ptr<Node>(node);
00112 }
00113 
00114 bool Leaf::IsLeaf() const
00115 {
00116     return true;
00117 }
00118 
00119 void Leaf::RemoveChildReference(const boost::shared_ptr<Leaf> &/*base*/)
00120 {
00121 }
00122 
00123 bool Leaf::AddChildReference(const boost::shared_ptr<Leaf> &/*base*/)
00124 {
00125     return false;
00126 }
00127 
00128 void Leaf::Unlink()
00129 {
00130     // here we lose our reference to the parent
00131     SetParent(boost::shared_ptr<Node>());
00132 }
00133 
00134 void Leaf::UnlinkChildren()
00135 {
00136 }
00137 
00138 void Leaf::Dump() const
00139 {
00140     Object::Dump();
00141     cout << "Leaf: '" << GetName() << "'" << endl;
00142 }
00143 
00144 const std::string& Leaf::GetFullPath() const
00145 {
00146     //printf("this:getfullpath %x - %s - %x %x\n", this, GetName().c_str(), GetParent(), mCachedFullPath);
00147     if (mCachedFullPath == NULL)
00148     {
00149         std::string parentPath;
00150 
00151         if (shared_ptr<Leaf> p = GetParent().lock())
00152         {
00153             if (p)
00154             {
00155                 shared_ptr<Leaf> blah = make_shared(GetParent());
00156                 parentPath = blah->GetFullPath();
00157             }
00158         }
00159 
00160         // no cached data available
00161         if (IsLeaf())
00162             mCachedFullPath = new std::string(parentPath + mName);
00163         else
00164             mCachedFullPath = new std::string(parentPath + mName + "/");
00165     }
00166 
00167     return *mCachedFullPath;
00168 }
00169 
00170 void Leaf::ClearCachedData() const
00171 {
00172     delete mCachedFullPath;
00173     mCachedFullPath = NULL;
00174 }
00175 
00176 Leaf::TLeafList gFakeChildren;
00177 
00178 Leaf::TLeafList::iterator Leaf::begin()
00179 {
00180     return gFakeChildren.begin();
00181 }
00182 
00183 Leaf::TLeafList::const_iterator Leaf::begin() const
00184 {
00185     return gFakeChildren.begin();
00186 }
00187 
00188 Leaf::TLeafList::iterator Leaf::end()
00189 {
00190     return gFakeChildren.end();
00191 }
00192 
00193 Leaf::TLeafList::const_iterator Leaf::end() const
00194 {
00195     return gFakeChildren.end();
00196 }
00197 
00198 void Leaf::SetParent(const boost::shared_ptr<Node> &newParent)
00199 {
00200     shared_ptr<Node> oldParent = make_shared(GetParent());
00201     if (oldParent.get() != 0)
00202         {
00203             // we have a parent, so update our state
00204             shared_ptr<Leaf> self
00205                 = shared_static_cast<Leaf>(make_shared(GetSelf()));
00206 
00207             // here reference count should be > 1 (at least one in the
00208             // parent, and one in this routine)
00209             assert(self.use_count() > 1);
00210 
00211             if (newParent.get() == 0)
00212                 {
00213                     // time to clean up
00214                     OnUnlink();
00215                     ClearCachedData();
00216                     oldParent->RemoveChildReference(self);
00217                 }
00218 
00219             // we remove ourself from the old parent's list of children
00220             oldParent->RemoveChildReference(self);
00221 
00222             // we add ourself to the new parent's list of children
00223             if (newParent.get() != 0)
00224             {
00225                 newParent->AddChildReference(self);
00226             }
00227         }
00228 
00229     mParent = newParent;
00230     if (! mParent.expired())
00231         {
00232             // we have been linked, so now we can do something :)
00233             OnLink();
00234         }
00235 }
00236 
00237 void Leaf::OnLink()
00238 {
00239 }
00240 
00241 void Leaf::OnUnlink()
00242 {
00243 }

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