Utils: Document new utilities
[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 prefix_ bool senf::console::GenericNode::operator==(GenericNode & other)
88     const
89 {
90     return this == & other;
91 }
92
93 prefix_ bool senf::console::GenericNode::operator!=(GenericNode & other)
94     const
95 {
96     return this != & other;
97 }
98
99 prefix_ bool senf::console::GenericNode::isDirectory()
100     const
101 {
102     return dynamic_cast<DirectoryNode const *>(this);
103 }
104
105 prefix_ bool senf::console::GenericNode::isLink()
106     const
107 {
108     return dynamic_cast<LinkNode const *>(this);
109 }
110
111 prefix_ bool senf::console::GenericNode::isCommand()
112     const
113 {
114     return dynamic_cast<CommandNode const *>(this);
115 }
116
117 ///////////////////////////////////////////////////////////////////////////
118 // senf::console::LinkNode
119
120 prefix_ senf::console::LinkNode::ptr senf::console::LinkNode::create(GenericNode & node)
121 {
122     GenericNode::ptr p (node.thisptr());
123     while ( p->isLink() )
124         p = dynamic_cast<LinkNode&>(*p).follow().thisptr();
125     return ptr(new LinkNode(*p));
126 }
127
128 prefix_ senf::console::GenericNode & senf::console::LinkNode::follow()
129     const
130 {
131     return *node_;
132 }
133
134 prefix_ senf::console::LinkNode::LinkNode(GenericNode & node)
135     : node_ (node.thisptr())
136 {}
137
138 ///////////////////////////////////////////////////////////////////////////
139 // senf::console::DirectoryNode
140
141 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
142 {
143     return ptr(new DirectoryNode());
144 }
145
146 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
147     const
148 {
149     ChildMap::const_iterator i (children_.find(name));
150     return i != children_.end();
151 }
152
153 prefix_ senf::console::GenericNode &
154 senf::console::DirectoryNode::get(std::string const & name)
155     const
156 {
157     GenericNode & node (getLink(name));
158     return node.isLink()
159         ? dynamic_cast<LinkNode&>(node).follow()
160         : node;
161 }
162
163 prefix_ senf::console::DirectoryNode &
164 senf::console::DirectoryNode::getDirectory(std::string const & name)
165     const
166 {
167     try {
168         return dynamic_cast<DirectoryNode&>(get(name));
169     }
170     SENF_WRAP_EXC(std::bad_cast)
171 }
172
173 prefix_ senf::console::DirectoryNode &
174 senf::console::DirectoryNode::operator[](std::string const & name)
175     const
176 {
177     return getDirectory(name);
178 }
179
180 prefix_ senf::console::CommandNode &
181 senf::console::DirectoryNode::getCommand(std::string const & name)
182     const
183 {
184     try {
185         return dynamic_cast<CommandNode&>(get(name));
186     }
187     SENF_WRAP_EXC(std::bad_cast)
188 }
189
190 prefix_ senf::console::CommandNode &
191 senf::console::DirectoryNode::operator()(std::string const & name)
192     const
193 {
194     return getCommand(name);
195 }
196
197 prefix_ senf::console::DirectoryNode &
198 senf::console::DirectoryNode::mkdir(std::string const & name)
199 {
200     return add(name, create());
201 }
202
203 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
204     const
205 {
206     return boost::make_iterator_range(children_.begin(), children_.end());
207 }
208
209 prefix_ senf::console::DirectoryNode::ChildrenRange
210 senf::console::DirectoryNode::completions(std::string const & s)
211     const
212 {
213     return boost::make_iterator_range(children_.lower_bound(s),
214                                       children_.lower_bound(s + "\xff"));
215 }
216
217 prefix_ void senf::console::DirectoryNode::link(std::string const & name, GenericNode & target)
218 {
219     add(name, LinkNode::create(target));
220 }
221
222 prefix_ senf::console::DirectoryNode::DirectoryNode()
223 {}
224
225 prefix_ senf::console::DirectoryNode &
226 senf::console::DirectoryNode::doc(std::string const & doc)
227 {
228     doc_ = doc;
229     return *this;
230 }
231
232 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
233 {
234     return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
235 }
236
237 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
238     const
239 {
240     return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
241 }
242
243 ///////////////////////////////////////////////////////////////////////////
244 // senf::console::detail::NodeTraverser
245
246 prefix_ senf::console::detail::NodeTraverser::NodeTraverser(DirectoryNode & root,
247                                                             DirectoryNode & dir,
248                                                             bool autocomplete)
249     : root_ (root), dir_ (dir.thisptr()), autocomplete_ (autocomplete), init_ (false)
250 {}
251
252 ///////////////////////////////////////////////////////////////////////////
253 // senf::console::SyntaxErrorException
254
255 prefix_ senf::console::SyntaxErrorException::SyntaxErrorException(std::string const & msg)
256     : message_(msg)
257 {}
258
259 prefix_ senf::console::SyntaxErrorException::~SyntaxErrorException()
260     throw()
261 {}
262
263 prefix_ std::string const & senf::console::SyntaxErrorException::message()
264     const
265 {
266     return message_;
267 }
268
269 ///////////////////////////////////////////////////////////////////////////
270 // senf::console::CommandNode
271
272 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
273 {
274     return boost::static_pointer_cast<CommandNode>(shared_from_this());
275 }
276
277 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
278     const
279 {
280     return boost::static_pointer_cast<CommandNode const>(shared_from_this());
281 }
282
283 prefix_ senf::console::CommandNode::CommandNode()
284 {}
285
286 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
287                                                  ParseCommandInfo const & command)
288     const
289 {
290     v_execute(output, command);
291 }
292
293 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
294                                                     ParseCommandInfo const & command)
295     const
296 {
297     execute(output, command);
298 }
299
300 ///////////////////////////////////////////////////////////////////////////
301 // senf::console::SimpleCommandNode
302
303 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
304     : fn_ (fn)
305 {}
306
307 prefix_ senf::console::SimpleCommandNode::ptr
308 senf::console::SimpleCommandNode::create(Function const & fn)
309 {
310     return ptr(new SimpleCommandNode(fn));
311 }
312
313 prefix_ senf::console::SimpleCommandNode &
314 senf::console::SimpleCommandNode::doc(std::string const & doc)
315 {
316     doc_ = doc;
317     return *this;
318 }
319
320 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
321 {
322     return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
323 }
324
325 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
326     const
327 {
328     return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
329 }
330
331 #ifndef DOXYGEN
332
333 prefix_ senf::console::SimpleCommandNode &
334 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
335                                      SimpleCommandNode::Function fn, int)
336 {
337     return node.add(name, SimpleCommandNode::create(fn));
338 }
339
340 #endif
341
342 ///////////////////////////////cci.e///////////////////////////////////////
343 #undef prefix_
344
345 \f
346 // Local Variables:
347 // mode: c++
348 // fill-column: 100
349 // comment-column: 40
350 // c-file-style: "senf"
351 // indent-tabs-mode: nil
352 // ispell-local-dictionary: "american"
353 // compile-command: "scons -u test"
354 // End: