Console: Factored out path traversal into generic traversal helper
[senf.git] / Console / Node.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Node public header */
25
26 #ifndef HH_Node_
27 #define HH_Node_ 1
28
29 // Custom includes
30 #include <map>
31 #include <boost/shared_ptr.hpp>
32 #include <boost/weak_ptr.hpp>
33 #include <boost/enable_shared_from_this.hpp>
34 #include <boost/utility.hpp>
35 #include <boost/range/iterator_range.hpp>
36 #include "../Utils/Exception.hh"
37
38 //#include "Node.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42 namespace console {
43
44     class DirectoryNode;
45     class CommandNode;
46
47     /** \brief
48       */
49     class GenericNode 
50         : public boost::enable_shared_from_this<GenericNode>
51     {
52     public:
53         ///////////////////////////////////////////////////////////////////////////
54         // Types
55
56         typedef boost::shared_ptr<GenericNode> ptr;
57         typedef boost::shared_ptr<GenericNode const> cptr;
58         typedef boost::weak_ptr<GenericNode> weak_ptr;
59
60         ///////////////////////////////////////////////////////////////////////////
61
62         virtual ~GenericNode();
63
64         std::string const & name() const;
65         boost::shared_ptr<DirectoryNode> parent() const;
66         bool managed() const;
67
68         std::string path() const;
69
70         ptr thisptr();
71         cptr thisptr() const;
72
73     protected:
74         explicit GenericNode(std::string const & name);
75
76         void name(std::string const & name);
77         static void name(GenericNode & node, std::string const & name);
78         void parent(DirectoryNode * parent);
79
80     private:
81         std::string name_;
82         DirectoryNode * parent_;
83
84         friend class intrusive_refcount_base;
85         friend class DirectoryNode;
86     };
87
88     /** \brief
89       */
90     class DirectoryNode : public GenericNode
91     {
92         typedef std::map<std::string, GenericNode::ptr> ChildMap;
93
94     public:
95         ///////////////////////////////////////////////////////////////////////////
96         // Types
97
98         typedef boost::shared_ptr<DirectoryNode> ptr;
99         typedef boost::shared_ptr<DirectoryNode const> cptr;
100         typedef boost::weak_ptr<DirectoryNode> weak_ptr;
101
102         typedef boost::iterator_range<ChildMap::const_iterator> ChildrenRange;
103         typedef ChildMap::const_iterator child_iterator;
104
105         ///////////////////////////////////////////////////////////////////////////
106
107         GenericNode & add(std::auto_ptr<GenericNode> node, bool uniquify = true);
108
109         DirectoryNode & operator[](std::string const & name) const;
110         CommandNode & operator()(std::string const & name) const;
111         GenericNode & get(std::string const & name) const;
112
113         DirectoryNode & mkdir(std::string const & name);
114         
115         ChildrenRange children() const;
116
117         template <class ForwardRange>
118         GenericNode & traverse(ForwardRange const & range);
119
120         ptr thisptr();
121         cptr thisptr() const;
122
123     protected:
124         explicit DirectoryNode(std::string const & name);
125
126     private:
127         void add(GenericNode::ptr node, bool uniquify);
128
129         ChildMap children_;
130
131         friend DirectoryNode & root();
132     };
133
134     struct DuplicateNodeNameException : public senf::Exception
135     { DuplicateNodeNameException() : senf::Exception("Duplicate node name") {}};
136
137     struct UnknownNodeNameException : public senf::Exception
138     { UnknownNodeNameException() : senf::Exception("Unknown node name") {}};
139
140     /** \brief
141       */
142     class CommandNode : public GenericNode
143     {
144     public:
145         ///////////////////////////////////////////////////////////////////////////
146         // Types
147
148         typedef boost::shared_ptr<CommandNode> ptr;
149         typedef boost::shared_ptr<CommandNode const> cptr;
150         typedef boost::weak_ptr<CommandNode> weak_ptr;
151
152         ///////////////////////////////////////////////////////////////////////////
153
154         ptr thisptr();
155         cptr thisptr() const;
156
157     protected:
158         explicit CommandNode(std::string const & name);
159
160     private:
161
162     };
163
164     DirectoryNode & root();
165
166 }}
167
168 ///////////////////////////////hh.e////////////////////////////////////////
169 #include "Node.cci"
170 #include "Node.ct"
171 //#include "Node.cti"
172 #endif
173
174 \f
175 // Local Variables:
176 // mode: c++
177 // fill-column: 100
178 // comment-column: 40
179 // c-file-style: "senf"
180 // indent-tabs-mode: nil
181 // ispell-local-dictionary: "american"
182 // compile-command: "scons -u test"
183 // End: