Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

tree.h

Go to the documentation of this file.
00001 /* Tree -- Base class for tree structures 
00002    Copyright (C) 2002 Aaron Bentley
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software Foundation,
00016    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00017 
00018 /*template <typename Payload>
00019 class Node
00020 {
00021 private:
00022    Node *parent;
00023    vector <Node *> children;
00024 
00025    Payload *loadptr;
00026    Payload &loadref;
00027 
00028 public:
00029    Node (Payload *a_payload)
00030       :
00031    loadptr(a_payload), loadref;
00032    {
00033       child=NULL;
00034       parent=NULL;
00035       next=NULL;
00036       prev=NULL;
00037       first=NULL;
00038       last=NULL;
00039    }
00040    virtual ~Node()
00041    {
00042       for (vector<Node *>::iterator it=children.begin(); it!=children.end();
00043     ++it)
00044       {
00045     delete *it;
00046       }
00047       delete a_payload;
00048    }
00049    Payload &operator *()
00050    {
00051       return loadref;
00052    }
00053    
00054    void addChild(Node *ch)
00055    {
00056       children.push_back(ch);
00057    }
00058 };
00059 
00060 class XmlContents()
00061 {
00062    virtual void getStream(ostream &stream, );
00063 }*/
00064 #include <string>
00065 using std::string;
00066 
00067 template <typename T>
00068 class TreeNode
00069 {
00070    private:
00071    T *parent;
00072    T *child;
00073    T *next;
00074    T *prev;
00075    T &othis;
00076    public:
00077    TreeNode(T &a_othis)
00078       :
00079    parent(NULL), child(NULL), next(NULL), prev(NULL), othis(a_othis)
00080    {}
00081    T* addChild(T *ch)
00082    {
00083       if (ch==NULL) return ch;
00084       if (child==NULL)
00085       {
00086     child=ch;
00087     ch->parent=&othis;
00088       }
00089       else child->getLastRecur().setNext(ch);
00090       return ch;
00091    }
00092 
00093    T& getLastRecur()
00094    {
00095       if (next!=NULL) return next->getLastRecur();
00096       else return othis;
00097    }
00098 
00099    void setNext(T *ch)
00100    {
00101       for (T *cur=ch; cur!=NULL; cur=cur->next)
00102       {
00103     cur->parent=parent;
00104       }
00105       next=ch;
00106       next->prev=&othis;
00107    }
00108    T const *getFirstChild() const
00109    {
00110       return child;
00111    }
00112    T *getFirstChild()
00113    {
00114       return child;
00115    }
00116    T const *getNext() const
00117    {
00118       return next;
00119    }
00120    T *getNext()
00121    {
00122       return next;
00123    }
00124 };
00125 
00126 class XmlNode : public TreeNode <XmlNode>
00127 {
00128 public:
00129    XmlNode()
00130       :
00131    TreeNode<XmlNode>(*this)
00132    {}
00133    virtual string getString(int indent=0) const
00134    {
00135       return "";
00136    }
00137 };
00138 
00139 
00140 class XmlText : public XmlNode
00141 {
00142 private:
00143    string text;
00144 public:
00145    XmlText(string const &is);
00146 
00147    string getString(int indent=0) const;
00148 };
00149 
00150 XmlText::XmlText(string const &a_text)
00151    :
00152    text(a_text)
00153 {
00154 }
00155 string XmlText::getString(int indent=0) const
00156 {
00157    return text;
00158 }
00159 
00160 class XmlTag : public XmlNode
00161 {
00162 private:
00163    string tag;
00164 
00165 public:
00166    XmlTag(string a_tag);
00167    string getString(int indent=0) const;
00168 };
00169 
00170 XmlTag::XmlTag(string a_tag)
00171    :
00172 tag(a_tag)
00173 {
00174 }
00175 
00176 string XmlTag::getString(int indent=0) const
00177 {
00178    string output;
00179   
00180    if (indent>0) 
00181       output+='\n';
00182 
00183    for (int i=0; i<indent; ++i)
00184       output+=' ';
00185 
00186    output+=string("<")+tag;
00187 
00188    if (getFirstChild()==NULL) return output+" />";
00189    else
00190    {
00191       output+=">";
00192       for (XmlNode const *cur=getFirstChild(); cur!=NULL; cur=cur->getNext())
00193       {
00194     output+=cur->getString(indent+3);
00195       }
00196       output+=string("</")+tag+">";
00197    }
00198    
00199    return output;
00200 }
00201 class XmlTree public XmlNode;
00202 {
00203    static string getString(XmlNode const &);
00204 };
00205 string XmlTree::getString(XmlNode const &top)
00206 {
00207    string output
00208    int indent=0;
00209    for (XmlNode const *cur=top; cur!=NULL)
00210    {
00211       if (cur->getFirstChild()!==NULL)
00212       {
00213     output+=
00214       }
00215    }
00216 }
00217 #include <iostream>
00218 int main()
00219 {
00220    XmlTag top("xml");
00221    top.addChild(new XmlTag("contents"))->addChild(new XmlText("Foo"));
00222    std::cout<<top.getString()<<std::endl;
00223    return 0;
00224 }

Generated on Mon Apr 7 19:41:47 2003 for DuTree by doxygen1.2.18