Console: Implement current directory management and builtins (cd, dirstack, ls)
[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::weak_ptr<GenericNode> weak_ptr;
58
59         ///////////////////////////////////////////////////////////////////////////
60
61         virtual ~GenericNode();
62
63         std::string const & name() const;
64         boost::shared_ptr<DirectoryNode> parent() const;
65         bool managed() const;
66
67         std::string path() const;
68
69     protected:
70         explicit GenericNode(std::string const & name);
71
72         void name(std::string const & name);
73         static void name(GenericNode & node, std::string const & name);
74         void parent(DirectoryNode * parent);
75
76     private:
77         std::string name_;
78         DirectoryNode * parent_;
79
80         friend class intrusive_refcount_base;
81         friend class DirectoryNode;
82     };
83
84     /** \brief
85       */
86     class DirectoryNode : public GenericNode
87     {
88         typedef std::map<std::string, GenericNode::ptr> ChildMap;
89
90     public:
91         ///////////////////////////////////////////////////////////////////////////
92         // Types
93
94         typedef boost::shared_ptr<DirectoryNode> ptr;
95         typedef boost::weak_ptr<DirectoryNode> weak_ptr;
96
97         typedef boost::iterator_range<ChildMap::const_iterator> ChildrenRange;
98         typedef ChildMap::const_iterator child_iterator;
99
100         ///////////////////////////////////////////////////////////////////////////
101
102         GenericNode & add(std::auto_ptr<GenericNode> node, bool uniquify = true);
103
104         DirectoryNode & operator[](std::string const & name) const;
105         CommandNode & operator()(std::string const & name) const;
106
107         DirectoryNode & mkdir(std::string const & name);
108         
109         ChildrenRange children() const;
110
111     protected:
112         explicit DirectoryNode(std::string const & name);
113
114     private:
115         void add(GenericNode::ptr node, bool uniquify);
116         GenericNode & lookup(std::string const & name) const;
117
118         ChildMap children_;
119
120         friend DirectoryNode & root();
121     };
122
123     struct DuplicateNodeNameException : public senf::Exception
124     { DuplicateNodeNameException() : senf::Exception("Duplicate node name") {}};
125
126     struct UnknownNodeNameException : public senf::Exception
127     { UnknownNodeNameException() : senf::Exception("Unknown node name") {}};
128
129     /** \brief
130       */
131     class CommandNode : public GenericNode
132     {
133     public:
134         ///////////////////////////////////////////////////////////////////////////
135         // Types
136
137         typedef boost::shared_ptr<CommandNode> ptr;
138         typedef boost::weak_ptr<CommandNode> weak_ptr;
139
140         ///////////////////////////////////////////////////////////////////////////
141
142     protected:
143         explicit CommandNode(std::string const & name);
144
145     private:
146
147     };
148
149     DirectoryNode & root();
150
151 }}
152
153 ///////////////////////////////hh.e////////////////////////////////////////
154 #include "Node.cci"
155 //#include "Node.ct"
156 //#include "Node.cti"
157 #endif
158
159 \f
160 // Local Variables:
161 // mode: c++
162 // fill-column: 100
163 // comment-column: 40
164 // c-file-style: "senf"
165 // indent-tabs-mode: nil
166 // ispell-local-dictionary: "american"
167 // compile-command: "scons -u test"
168 // End: