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

leaf.h

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.h,v 1.17 2005/10/14 11:04:01 jamu 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    Leaf
00023 
00024         HISTORY:
00025                 04.06.2002 MK
00026                         - initial version
00027                 13.06.2002 MK
00028                         - reworked Base to use a quad-linked tree
00029                 15.06.2002 MK
00030                         - split Base up into Base and Node classes
00031                 02.09.2002 MK
00032                         - removed quad-link
00033                 22.02.2003 MK
00034                         - renamed to Leaf
00035 
00036 */
00037 #ifndef ZEITGEIST_LEAF_H
00038 #define ZEITGEIST_LEAF_H
00039 
00040 #include <list>
00041 #include <string>
00042 #include "object.h"
00043 
00044 namespace zeitgeist
00045 {
00046 
00050 class Leaf : public Object
00051 {
00052     friend class Node;
00053     //
00054     // types
00055     //
00056 public:
00057     typedef std::list< boost::shared_ptr<Leaf> > TLeafList;
00058 
00059     //
00060     // functions
00061     //
00062 public:
00064     Leaf(const std::string &name = "<unnamed>");
00065     virtual ~Leaf();
00066 
00068     std::string& GetName()
00069     {       return mName;   }
00070 
00072     const std::string&  GetName() const
00073     {       return mName;   }
00074 
00075     // hierarchy
00076 
00078     boost::weak_ptr<Node>& GetParent();
00079 
00081     const boost::weak_ptr<Node>& GetParent() const;
00082 
00087     virtual boost::shared_ptr<Leaf>
00088     GetChild(const std::string &name, bool recursive = false);
00089 
00093     virtual boost::shared_ptr<Leaf>
00094     GetChildOfClass(const std::string &name, bool recursive = false);
00095 
00100     virtual boost::shared_ptr<Leaf>
00101     GetChildSupportingClass(const std::string &name, bool recursive = false);
00102 
00110     template<class CLASS>
00111     boost::shared_ptr<CLASS>
00112     FindChildSupportingClass(bool recursive = false)
00113     {
00114         TLeafList::iterator lstEnd = end(); // avoid repeated virtual calls
00115         for (TLeafList::iterator i = begin(); i != lstEnd; ++i)
00116             {
00117                 // check if we have found a match and return it
00118                 boost::shared_ptr<CLASS> child = boost::shared_dynamic_cast<CLASS>(*i);
00119                 if (child.get() != 0)
00120                     {
00121                         return child;
00122                     }
00123 
00124                 if (recursive)
00125                     {
00126                         return (*i)->FindChildSupportingClass<CLASS>(recursive);
00127                     }
00128             }
00129 
00130         return boost::shared_ptr<CLASS>();
00131     }
00132 
00135     virtual void
00136     GetChildren(const std::string &name, TLeafList &baseList, bool recursive = false);
00137 
00140     virtual void GetChildrenOfClass(const std::string &name, TLeafList &baseList, bool recursive = false);
00141 
00146     virtual void GetChildrenSupportingClass(const std::string &name, TLeafList &baseList, bool recursive = false);
00147 
00154     template<class CLASS>
00155     void ListChildrenSupportingClass(TLeafList& list, bool recursive = false)
00156     {
00157         TLeafList::iterator lstEnd = end(); // avoid repeated virtual calls
00158         for (TLeafList::iterator i = begin(); i != lstEnd; ++i)
00159             {
00160                 // check if we have found a match and add it
00161                 boost::shared_ptr<CLASS> child = boost::shared_dynamic_cast<CLASS>(*i);
00162                 if (child.get() != 0)
00163                     {
00164                         list.push_back(child);
00165                     }
00166 
00167                 if (recursive)
00168                     {
00169                         (*i)->ListChildrenSupportingClass<CLASS>(list,recursive);
00170                     }
00171             }
00172     }
00173 
00178     virtual boost::weak_ptr<Node>
00179     GetParentSupportingClass(const std::string &name) const;
00180 
00188     template<class CLASS>
00189     boost::weak_ptr<CLASS>
00190     FindParentSupportingClass() const;
00191 //     {
00192 //         boost::shared_ptr<Node> node
00193 //             = boost::shared_static_cast<Node>(make_shared(GetParent()));
00194 
00195 //         while (node.get() != 0)
00196 //         {
00197 //             boost::shared_ptr<CLASS> test =
00198 //                 boost::shared_dynamic_cast<CLASS>(node);
00199 
00200 //             if (test.get() != 0)
00201 //             {
00202 //                 return test;
00203 //             }
00204 
00205 //             node = boost::shared_static_cast<Node>(make_shared(node->GetParent()));
00206 //         }
00207 
00208 //         return boost::shared_ptr<CLASS>();
00209 //     }
00210 
00211 
00214     virtual bool IsLeaf() const;
00215 
00217     virtual void RemoveChildReference(const boost::shared_ptr<Leaf> &base);
00218 
00220     virtual bool AddChildReference(const boost::shared_ptr<Leaf> &base);
00221 
00223     void Unlink();
00224 
00226     virtual void UnlinkChildren();
00227 
00229     virtual void Dump() const;
00230 
00232     virtual void UpdateCached() {}
00233 
00237     const std::string& GetFullPath() const;
00238 
00241     void ClearCachedData() const;
00242 
00244     void        SetName(const std::string &name)
00245         {   mName = name;  ClearCachedData(); }
00246 
00247     // iterator helpers
00248     virtual TLeafList::iterator begin();
00249     virtual TLeafList::const_iterator begin() const;
00250     virtual TLeafList::iterator end();
00251     virtual TLeafList::const_iterator end() const;
00252 protected:
00256     virtual void UpdateCachedInternal() {}
00257 
00265     void SetParent(const boost::shared_ptr<Node> &parent);
00266 
00272     virtual void OnLink();
00273 
00278     virtual void OnUnlink();
00279 
00280 private:
00281     Leaf(const Leaf &obj);
00282     Leaf& operator=(const Leaf &obj);
00283 
00284     //
00285     // members
00286     //
00287 protected:
00292     boost::weak_ptr<Node>       mParent;
00293 
00294 private:
00296     std::string mName;
00297 
00299     mutable std::string *mCachedFullPath;
00300 };
00301 
00306 } //namespace zeitgeist;
00307 
00308 #endif //ZEITGEIST_LEAF_H

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