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