8fe2c2bca879c4fd5495c7ed04d8a41d0ed4042a
[senf.git] / Console / Node.cci
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 inline non-template implementation */
25
26 //#include "Node.ih"
27
28 // Custom includes
29 #include "../Utils/senfassert.hh"
30
31 #define prefix_ inline
32 ///////////////////////////////cci.p///////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::console::GenericNode
36
37 prefix_ senf::console::GenericNode::~GenericNode()
38 {}
39
40 prefix_ std::string const & senf::console::GenericNode::name()
41     const
42 {
43     return name_;
44 }
45
46 prefix_ senf::console::GenericNode::GenericNode()
47     : parent_ (0)
48 {}
49
50 prefix_ void senf::console::GenericNode::name(std::string const & name)
51 {
52     name_ = name;
53 }
54
55 prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
56     const
57 {
58     return boost::static_pointer_cast<DirectoryNode>(
59         parent_ ? parent_->shared_from_this() : ptr() );
60 }
61
62 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
63 {
64     if (parent_)
65         return parent()->remove(name());
66     else
67         return thisptr();
68 }
69
70 prefix_ void senf::console::GenericNode::help(std::ostream & output)
71     const
72 {
73     v_help(output);
74 }
75
76 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::thisptr()
77 {
78     return shared_from_this();
79 }
80
81 prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
82     const
83 {
84     return shared_from_this();
85 }
86
87 ///////////////////////////////////////////////////////////////////////////
88 // senf::console::DirectoryNode
89
90 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
91 {
92     return ptr(new DirectoryNode());
93 }
94
95 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
96     const
97 {
98     ChildMap::const_iterator i (children_.find(name));
99     return i != children_.end();
100 }
101
102 prefix_ senf::console::DirectoryNode &
103 senf::console::DirectoryNode::getDirectory(std::string const & name)
104     const
105 {
106     try {
107         return dynamic_cast<DirectoryNode&>(get(name));
108     }
109     SENF_WRAP_EXC(std::bad_cast)
110 }
111
112 prefix_ senf::console::DirectoryNode &
113 senf::console::DirectoryNode::operator[](std::string const & name)
114     const
115 {
116     return getDirectory(name);
117 }
118
119 prefix_ senf::console::CommandNode &
120 senf::console::DirectoryNode::getCommand(std::string const & name)
121     const
122 {
123     try {
124         return dynamic_cast<CommandNode&>(get(name));
125     }
126     SENF_WRAP_EXC(std::bad_cast)
127 }
128
129 prefix_ senf::console::CommandNode &
130 senf::console::DirectoryNode::operator()(std::string const & name)
131     const
132 {
133     return getCommand(name);
134 }
135
136 prefix_ senf::console::DirectoryNode &
137 senf::console::DirectoryNode::mkdir(std::string const & name)
138 {
139     return add(name, create());
140 }
141
142 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
143     const
144 {
145     return boost::make_iterator_range(children_.begin(), children_.end());
146 }
147
148 prefix_ senf::console::DirectoryNode::ChildrenRange
149 senf::console::DirectoryNode::completions(std::string const & s)
150     const
151 {
152     return boost::make_iterator_range(children_.lower_bound(s),
153                                       children_.lower_bound(s + "\xff"));
154 }
155
156 prefix_ senf::console::DirectoryNode::DirectoryNode()
157 {}
158
159 prefix_ senf::console::DirectoryNode &
160 senf::console::DirectoryNode::doc(std::string const & doc)
161 {
162     doc_ = doc;
163     return *this;
164 }
165
166 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
167 {
168     return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
169 }
170
171 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
172     const
173 {
174     return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
175 }
176
177 ///////////////////////////////////////////////////////////////////////////
178 // senf::console::SyntaxErrorException
179
180 prefix_ senf::console::SyntaxErrorException::SyntaxErrorException(std::string const & msg)
181     : message_(msg)
182 {}
183
184 prefix_ senf::console::SyntaxErrorException::~SyntaxErrorException()
185     throw()
186 {}
187
188 prefix_ std::string const & senf::console::SyntaxErrorException::message()
189     const
190 {
191     return message_;
192 }
193
194 ///////////////////////////////////////////////////////////////////////////
195 // senf::console::CommandNode
196
197 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
198 {
199     return boost::static_pointer_cast<CommandNode>(shared_from_this());
200 }
201
202 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
203     const
204 {
205     return boost::static_pointer_cast<CommandNode const>(shared_from_this());
206 }
207
208 prefix_ senf::console::CommandNode::CommandNode()
209 {}
210
211 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
212                                                  ParseCommandInfo const & command)
213     const
214 {
215     v_execute(output, command);
216 }
217
218 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
219                                                     ParseCommandInfo const & command)
220     const
221 {
222     execute(output, command);
223 }
224
225 ///////////////////////////////////////////////////////////////////////////
226 // senf::console::SimpleCommandNode
227
228 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
229     : fn_ (fn)
230 {}
231
232 prefix_ senf::console::SimpleCommandNode::ptr
233 senf::console::SimpleCommandNode::create(Function const & fn)
234 {
235     return ptr(new SimpleCommandNode(fn));
236 }
237
238 prefix_ senf::console::SimpleCommandNode &
239 senf::console::SimpleCommandNode::doc(std::string const & doc)
240 {
241     doc_ = doc;
242     return *this;
243 }
244
245 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
246 {
247     return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
248 }
249
250 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
251     const
252 {
253     return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
254 }
255
256 prefix_ senf::console::SimpleCommandNode &
257 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
258                                      SimpleCommandNode::Function fn, int)
259 {
260     return node.add(name, SimpleCommandNode::create(fn));
261 }
262
263
264 ///////////////////////////////cci.e///////////////////////////////////////
265 #undef prefix_
266
267 \f
268 // Local Variables:
269 // mode: c++
270 // fill-column: 100
271 // comment-column: 40
272 // c-file-style: "senf"
273 // indent-tabs-mode: nil
274 // ispell-local-dictionary: "american"
275 // compile-command: "scons -u test"
276 // End: