Console: Implement command execution
[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 #include "Parse.hh"
38
39 //#include "Node.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43 namespace console {
44
45     class DirectoryNode;
46     class CommandNode;
47
48     /** \brief
49       */
50     class GenericNode 
51         : public boost::enable_shared_from_this<GenericNode>
52     {
53     public:
54         ///////////////////////////////////////////////////////////////////////////
55         // Types
56
57         typedef boost::shared_ptr<GenericNode> ptr;
58         typedef boost::shared_ptr<GenericNode const> cptr;
59         typedef boost::weak_ptr<GenericNode> weak_ptr;
60
61         ///////////////////////////////////////////////////////////////////////////
62
63         virtual ~GenericNode();
64
65         std::string const & name() const;
66         boost::shared_ptr<DirectoryNode> parent() const;
67         bool managed() const;
68
69         std::string path() const;
70
71         ptr thisptr();
72         cptr thisptr() const;
73
74     protected:
75         explicit GenericNode(std::string const & name);
76
77         void name(std::string const & name);
78         static void name(GenericNode & node, std::string const & name);
79         void parent(DirectoryNode * parent);
80
81     private:
82         std::string name_;
83         DirectoryNode * parent_;
84
85         friend class intrusive_refcount_base;
86         friend class DirectoryNode;
87     };
88
89     /** \brief
90       */
91     class DirectoryNode : public GenericNode
92     {
93         typedef std::map<std::string, GenericNode::ptr> ChildMap;
94
95     public:
96         ///////////////////////////////////////////////////////////////////////////
97         // Types
98
99         typedef boost::shared_ptr<DirectoryNode> ptr;
100         typedef boost::shared_ptr<DirectoryNode const> cptr;
101         typedef boost::weak_ptr<DirectoryNode> weak_ptr;
102
103         typedef boost::iterator_range<ChildMap::const_iterator> ChildrenRange;
104         typedef ChildMap::const_iterator child_iterator;
105
106         ///////////////////////////////////////////////////////////////////////////
107
108         GenericNode & add(std::auto_ptr<GenericNode> node, bool uniquify = true);
109
110         DirectoryNode & operator[](std::string const & name) const;
111         CommandNode & operator()(std::string const & name) const;
112         GenericNode & get(std::string const & name) const;
113
114         DirectoryNode & mkdir(std::string const & name);
115         
116         ChildrenRange children() const;
117
118         template <class ForwardRange>
119         GenericNode & traverse(ForwardRange const & range);
120
121         ptr thisptr();
122         cptr thisptr() const;
123
124     protected:
125         explicit DirectoryNode(std::string const & name);
126
127     private:
128         void add(GenericNode::ptr node, bool uniquify);
129
130         ChildMap children_;
131
132         friend DirectoryNode & root();
133     };
134
135     struct DuplicateNodeNameException : public senf::Exception
136     { DuplicateNodeNameException() : senf::Exception("Duplicate node name") {}};
137
138     struct UnknownNodeNameException : public senf::Exception
139     { UnknownNodeNameException() : senf::Exception("Unknown node name") {}};
140
141     /** \brief
142       */
143     class CommandNode : public GenericNode
144     {
145     public:
146         ///////////////////////////////////////////////////////////////////////////
147         // Types
148
149         typedef boost::shared_ptr<CommandNode> ptr;
150         typedef boost::shared_ptr<CommandNode const> cptr;
151         typedef boost::weak_ptr<CommandNode> weak_ptr;
152
153         ///////////////////////////////////////////////////////////////////////////
154
155         virtual void operator()(std::ostream & output, 
156                                 ParseCommandInfo::ArgumentsRange const & arguments) = 0;
157
158         ptr thisptr();
159         cptr thisptr() const;
160
161     protected:
162         explicit CommandNode(std::string const & name);
163
164     private:
165
166     };
167
168     DirectoryNode & root();
169
170 }}
171
172 ///////////////////////////////hh.e////////////////////////////////////////
173 #include "Node.cci"
174 #include "Node.ct"
175 //#include "Node.cti"
176 #endif
177
178 \f
179 // Local Variables:
180 // mode: c++
181 // fill-column: 100
182 // comment-column: 40
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // End: