Utils/Console: Fix adding DirectoryNode's
[senf.git] / Utils / 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::ptr senf::console::GenericNode::thisptr()
38 {
39     return shared_from_this();
40 }
41
42 prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
43     const
44 {
45     return shared_from_this();
46 }
47
48 prefix_ senf::console::GenericNode::~GenericNode()
49 {}
50
51 prefix_ std::string const & senf::console::GenericNode::name()
52     const
53 {
54     return name_;
55 }
56
57 prefix_ senf::console::GenericNode::GenericNode()
58     : parent_ (0)
59 {}
60
61 prefix_ void senf::console::GenericNode::name(std::string const & name)
62 {
63     name_ = name;
64 }
65
66 prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
67     const
68 {
69     return boost::static_pointer_cast<DirectoryNode>(
70         parent_ ? parent_->shared_from_this() : ptr() );
71 }
72
73 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
74 {
75     if (parent_)
76         return parent()->remove(name());
77     else
78         return thisptr();
79 }
80
81 prefix_ void senf::console::GenericNode::help(std::ostream & output)
82     const
83 {
84     v_help(output);
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 prefix_ senf::console::GenericNode const & senf::console::GenericNode::followLink()
118     const
119 {
120     return isLink()
121         ? dynamic_cast<LinkNode const *>(this)->follow()
122         : *this;
123 }
124
125 prefix_ senf::console::GenericNode & senf::console::GenericNode::followLink()
126 {
127     return isLink()
128         ? dynamic_cast<LinkNode *>(this)->follow()
129         : *this;
130 }
131
132 ///////////////////////////////////////////////////////////////////////////
133 // senf::console::LinkNode
134
135 prefix_ senf::console::GenericNode & senf::console::LinkNode::follow()
136     const
137 {
138     return *node_;
139 }
140
141 prefix_ senf::console::LinkNode::ptr senf::console::LinkNode::create(GenericNode & node)
142 {
143     GenericNode::ptr p (node.thisptr());
144     while ( p->isLink() )
145         p = dynamic_cast<LinkNode&>(*p).follow().thisptr();
146     return ptr(new LinkNode(*p));
147 }
148
149 prefix_ senf::console::LinkNode::LinkNode(GenericNode & node)
150     : node_ (node.thisptr())
151 {}
152
153 ///////////////////////////////////////////////////////////////////////////
154 // senf::console::DirectoryNode
155
156 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
157 {
158     return ptr(new DirectoryNode());
159 }
160
161 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
162     const
163 {
164     ChildMap::const_iterator i (children_.find(name));
165     return i != children_.end();
166 }
167
168 prefix_ senf::console::GenericNode &
169 senf::console::DirectoryNode::get(std::string const & name)
170     const
171 {
172     return getLink(name).followLink();
173 }
174
175 prefix_ senf::console::DirectoryNode &
176 senf::console::DirectoryNode::getDirectory(std::string const & name)
177     const
178 {
179     try {
180         return dynamic_cast<DirectoryNode&>(get(name));
181     }
182     SENF_WRAP_EXC(std::bad_cast)
183 }
184
185 prefix_ senf::console::DirectoryNode &
186 senf::console::DirectoryNode::operator[](std::string const & name)
187     const
188 {
189     return getDirectory(name);
190 }
191
192 prefix_ senf::console::CommandNode &
193 senf::console::DirectoryNode::getCommand(std::string const & name)
194     const
195 {
196     try {
197         return dynamic_cast<CommandNode&>(get(name));
198     }
199     SENF_WRAP_EXC(std::bad_cast)
200 }
201
202 prefix_ senf::console::CommandNode &
203 senf::console::DirectoryNode::operator()(std::string const & name)
204     const
205 {
206     return getCommand(name);
207 }
208
209 prefix_ senf::console::DirectoryNode &
210 senf::console::DirectoryNode::mkdir(std::string const & name)
211 {
212     return add(name, create());
213 }
214
215 prefix_ senf::console::DirectoryNode &
216 senf::console::DirectoryNode::provideDirectory(std::string const & name)
217 {
218     return hasChild(name) ? getDirectory(name) : mkdir(name);
219 }
220
221 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
222     const
223 {
224     return boost::make_iterator_range(children_.begin(), children_.end());
225 }
226
227 prefix_ senf::console::DirectoryNode::ChildrenRange
228 senf::console::DirectoryNode::completions(std::string const & s)
229     const
230 {
231     return boost::make_iterator_range(children_.lower_bound(s),
232                                       children_.lower_bound(s + "\xff"));
233 }
234
235 prefix_ void senf::console::DirectoryNode::link(std::string const & name, GenericNode & target)
236 {
237     add(name, LinkNode::create(target));
238 }
239
240 prefix_ senf::console::DirectoryNode::DirectoryNode()
241 {}
242
243 prefix_ senf::console::DirectoryNode &
244 senf::console::DirectoryNode::doc(std::string const & doc)
245 {
246     doc_ = doc;
247     return *this;
248 }
249
250 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
251 {
252     return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
253 }
254
255 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
256     const
257 {
258     return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
259 }
260
261 ///////////////////////////////////////////////////////////////////////////
262 // senf::console::detail::NodeTraverser
263
264 prefix_ senf::console::detail::NodeTraverser::NodeTraverser(DirectoryNode & root,
265                                                             DirectoryNode & dir,
266                                                             bool autocomplete)
267     : root_ (root), dir_ (dir.thisptr()), autocomplete_ (autocomplete), init_ (false)
268 {}
269
270 ///////////////////////////////////////////////////////////////////////////
271 // senf::console::CommandNode
272
273 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
274 {
275     return boost::static_pointer_cast<CommandNode>(shared_from_this());
276 }
277
278 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
279     const
280 {
281     return boost::static_pointer_cast<CommandNode const>(shared_from_this());
282 }
283
284 prefix_ senf::console::CommandNode::CommandNode()
285 {}
286
287 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
288                                                  ParseCommandInfo const & command)
289     const
290 {
291     boost::any rv;
292     execute(rv, output, command);
293 }
294
295 prefix_ void senf::console::CommandNode::execute(boost::any & rv, std::ostream & output,
296                                                  ParseCommandInfo const & command)
297     const
298 {
299     rv = boost::any();
300     v_execute(rv, output, command);
301 }
302
303 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
304                                                     ParseCommandInfo const & command)
305     const
306 {
307     execute(output, command);
308 }
309
310 prefix_ void senf::console::CommandNode::operator()(boost::any & rv, std::ostream & output,
311                                                     ParseCommandInfo const & command)
312     const
313 {
314     execute(rv, output, command);
315 }
316
317 ///////////////////////////////////////////////////////////////////////////
318 // senf::console::SimpleCommandNode
319
320 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
321     : fn_ (fn)
322 {}
323
324 prefix_ senf::console::SimpleCommandNode::ptr
325 senf::console::SimpleCommandNode::create(Function const & fn)
326 {
327     return ptr(new SimpleCommandNode(fn));
328 }
329
330 prefix_ senf::console::SimpleCommandNode &
331 senf::console::SimpleCommandNode::doc(std::string const & doc)
332 {
333     doc_ = doc;
334     return *this;
335 }
336
337 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
338 {
339     return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
340 }
341
342 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
343     const
344 {
345     return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
346 }
347
348 #ifndef DOXYGEN
349
350 prefix_ senf::console::SimpleCommandNode &
351 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
352                                      SimpleCommandNode::Function fn, int)
353 {
354     return node.add(name, SimpleCommandNode::create(fn));
355 }
356
357 prefix_ senf::console::DirectoryNode &
358 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
359                                      DirectoryNode & dir, int)
360 {
361     return node.add(name, dir.thisptr());
362 }
363
364 #endif
365
366 ///////////////////////////////cci.e///////////////////////////////////////
367 #undef prefix_
368
369 \f
370 // Local Variables:
371 // mode: c++
372 // fill-column: 100
373 // comment-column: 40
374 // c-file-style: "senf"
375 // indent-tabs-mode: nil
376 // ispell-local-dictionary: "american"
377 // compile-command: "scons -u test"
378 // End: