21482ffb239be81cabd7b1a5f98ff025ac52a477
[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
30 #define prefix_ inline
31 ///////////////////////////////cci.p///////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::console::GenericNode
35
36 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::thisptr()
37 {
38     return shared_from_this();
39 }
40
41 prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
42     const
43 {
44     return shared_from_this();
45 }
46
47 prefix_ senf::console::GenericNode::~GenericNode()
48 {}
49
50 prefix_ std::string const & senf::console::GenericNode::name()
51     const
52 {
53     return name_;
54 }
55
56 prefix_ senf::console::GenericNode::GenericNode()
57     : parent_ (0)
58 {}
59
60 prefix_ void senf::console::GenericNode::name(std::string const & name)
61 {
62     name_ = name;
63 }
64
65 prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
66     const
67 {
68     return boost::static_pointer_cast<DirectoryNode>(
69         parent_ ? parent_->shared_from_this() : ptr() );
70 }
71
72 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
73 {
74     if (parent_)
75         return parent()->remove(name());
76     else
77         return thisptr();
78 }
79
80 prefix_ void senf::console::GenericNode::help(std::ostream & output)
81     const
82 {
83     v_help(output);
84 }
85
86 prefix_ std::string senf::console::GenericNode::shorthelp()
87     const
88 {
89     return v_shorthelp();
90 }
91
92 prefix_ bool senf::console::GenericNode::operator==(GenericNode & other)
93     const
94 {
95     return this == & other;
96 }
97
98 prefix_ bool senf::console::GenericNode::operator!=(GenericNode & other)
99     const
100 {
101     return this != & other;
102 }
103
104 prefix_ bool senf::console::GenericNode::isDirectory()
105     const
106 {
107     return dynamic_cast<DirectoryNode const *>(this);
108 }
109
110 prefix_ bool senf::console::GenericNode::isLink()
111     const
112 {
113     return dynamic_cast<LinkNode const *>(this);
114 }
115
116 prefix_ bool senf::console::GenericNode::isCommand()
117     const
118 {
119     return dynamic_cast<CommandNode const *>(this);
120 }
121
122 prefix_ senf::console::GenericNode const & senf::console::GenericNode::followLink()
123     const
124 {
125     return isLink()
126         ? dynamic_cast<LinkNode const *>(this)->follow()
127         : *this;
128 }
129
130 prefix_ senf::console::GenericNode & senf::console::GenericNode::followLink()
131 {
132     return isLink()
133         ? dynamic_cast<LinkNode *>(this)->follow()
134         : *this;
135 }
136
137 ///////////////////////////////////////////////////////////////////////////
138 // senf::console::LinkNode
139
140 prefix_ senf::console::GenericNode & senf::console::LinkNode::follow()
141     const
142 {
143     return *node_;
144 }
145
146 prefix_ senf::console::LinkNode::ptr senf::console::LinkNode::create(GenericNode & node)
147 {
148     GenericNode::ptr p (node.thisptr());
149     while ( p->isLink() )
150         p = dynamic_cast<LinkNode&>(*p).follow().thisptr();
151     return ptr(new LinkNode(*p));
152 }
153
154 prefix_ senf::console::LinkNode::LinkNode(GenericNode & node)
155     : node_ (node.thisptr())
156 {}
157
158 ///////////////////////////////////////////////////////////////////////////
159 // senf::console::DirectoryNode
160
161 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
162 {
163     return ptr(new DirectoryNode());
164 }
165
166 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
167     const
168 {
169     ChildMap::const_iterator i (children_.find(name));
170     return i != children_.end();
171 }
172
173 prefix_ senf::console::GenericNode &
174 senf::console::DirectoryNode::get(std::string const & name)
175     const
176 {
177     return getLink(name).followLink();
178 }
179
180 prefix_ senf::console::DirectoryNode &
181 senf::console::DirectoryNode::getDirectory(std::string const & name)
182     const
183 {
184     try {
185         return dynamic_cast<DirectoryNode&>(get(name));
186     }
187     SENF_WRAP_EXC(std::bad_cast)
188 }
189
190 prefix_ senf::console::DirectoryNode &
191 senf::console::DirectoryNode::operator[](std::string const & name)
192     const
193 {
194     return getDirectory(name);
195 }
196
197 prefix_ senf::console::CommandNode &
198 senf::console::DirectoryNode::getCommand(std::string const & name)
199     const
200 {
201     try {
202         return dynamic_cast<CommandNode&>(get(name));
203     }
204     SENF_WRAP_EXC(std::bad_cast)
205 }
206
207 prefix_ senf::console::CommandNode &
208 senf::console::DirectoryNode::operator()(std::string const & name)
209     const
210 {
211     return getCommand(name);
212 }
213
214 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
215     const
216 {
217     return boost::make_iterator_range(children_.begin(), children_.end());
218 }
219
220 prefix_ senf::console::DirectoryNode::ChildrenRange
221 senf::console::DirectoryNode::completions(std::string const & s)
222     const
223 {
224     return boost::make_iterator_range(children_.lower_bound(s),
225                                       children_.lower_bound(s + "\xff"));
226 }
227
228 prefix_ senf::console::DirectoryNode::DirectoryNode()
229 {}
230
231 prefix_ senf::console::DirectoryNode &
232 senf::console::DirectoryNode::doc(std::string const & doc)
233 {
234     doc_ = doc;
235     return *this;
236 }
237
238 prefix_ senf::console::DirectoryNode &
239 senf::console::DirectoryNode::shortdoc(std::string const & doc)
240 {
241     shortdoc_ = doc;
242     return *this;
243 }
244
245 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
246 {
247     return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
248 }
249
250 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
251     const
252 {
253     return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
254 }
255
256 ///////////////////////////////////////////////////////////////////////////
257 // senf::console::detail::NodeTraverser
258 #ifndef DOXYGEN
259
260 prefix_ senf::console::detail::NodeTraverser::NodeTraverser(DirectoryNode & root,
261                                                             DirectoryNode & dir,
262                                                             bool autocomplete)
263     : root_ (root), dir_ (dir.thisptr()), autocomplete_ (autocomplete), init_ (false)
264 {}
265
266 #endif
267
268 ///////////////////////////////////////////////////////////////////////////
269 // senf::console::CommandNode
270
271 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
272 {
273     return boost::static_pointer_cast<CommandNode>(shared_from_this());
274 }
275
276 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
277     const
278 {
279     return boost::static_pointer_cast<CommandNode const>(shared_from_this());
280 }
281
282 prefix_ senf::console::CommandNode::CommandNode()
283 {}
284
285 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
286                                                  ParseCommandInfo const & command)
287     const
288 {
289     boost::any rv;
290     execute(rv, output, command);
291 }
292
293 prefix_ void senf::console::CommandNode::execute(boost::any & rv, std::ostream & output,
294                                                  ParseCommandInfo const & command)
295     const
296 {
297     rv = boost::any();
298     v_execute(rv, output, command);
299 }
300
301 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
302                                                     ParseCommandInfo const & command)
303     const
304 {
305     execute(output, command);
306 }
307
308 prefix_ void senf::console::CommandNode::operator()(boost::any & rv, std::ostream & output,
309                                                     ParseCommandInfo const & command)
310     const
311 {
312     execute(rv, output, command);
313 }
314
315 ///////////////////////////////////////////////////////////////////////////
316 // senf::console::SimpleCommandNode
317
318 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
319     : fn_ (fn)
320 {}
321
322 prefix_ senf::console::SimpleCommandNode::ptr
323 senf::console::SimpleCommandNode::create(Function const & fn)
324 {
325     return ptr(new SimpleCommandNode(fn));
326 }
327
328 prefix_ senf::console::SimpleCommandNode &
329 senf::console::SimpleCommandNode::doc(std::string const & doc)
330 {
331     doc_ = doc;
332     return *this;
333 }
334
335 prefix_ senf::console::SimpleCommandNode &
336 senf::console::SimpleCommandNode::shortdoc(std::string const & doc)
337 {
338     shortdoc_ = doc;
339     return *this;
340 }
341
342 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
343 {
344     return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
345 }
346
347 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
348     const
349 {
350     return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
351 }
352
353 ///////////////////////////////////////////////////////////////////////////
354
355 prefix_ senf::console::DirectoryNode & senf::console::provideDirectory(DirectoryNode & dir,
356                                                                        std::string const & name)
357 {
358     return dir.hasChild(name) ? dir.getDirectory(name) : dir.add(name, factory::Directory());
359 }
360
361
362 ///////////////////////////////////////////////////////////////////////////
363 // senf::console::factory::SimpleCommand
364
365 prefix_ senf::console::factory::SimpleCommand::SimpleCommand(SimpleCommandNode::Function fn)
366     : node_ (SimpleCommandNode::create(fn))
367 {}
368
369 prefix_ senf::console::SimpleCommandNode &
370 senf::console::factory::SimpleCommand::create(DirectoryNode & dir, std::string const & name)
371     const
372 {
373     return dir.add(name, node_);
374 }
375
376 prefix_ senf::console::factory::SimpleCommand const &
377 senf::console::factory::SimpleCommand::doc(std::string const & doc)
378     const
379 {
380     node_->doc(doc);
381     return *this;
382 }
383
384 prefix_ senf::console::factory::SimpleCommand const &
385 senf::console::factory::SimpleCommand::shortdoc(std::string const & doc)
386     const
387 {
388     node_->shortdoc(doc);
389     return *this;
390 }
391
392 ///////////////////////////////////////////////////////////////////////////
393 // senf::console::factory::Directory
394
395 prefix_ senf::console::factory::Directory::Directory()
396     : node_ (DirectoryNode::create())
397 {}
398
399 prefix_ senf::console::DirectoryNode &
400 senf::console::factory::Directory::create(DirectoryNode & dir, std::string const & name)
401     const
402 {
403     return dir.add(name, node_);
404 }
405
406 prefix_ senf::console::factory::Directory const &
407 senf::console::factory::Directory::doc(std::string const & doc)
408     const
409 {
410     node_->doc(doc);
411     return *this;
412 }
413
414 prefix_ senf::console::factory::Directory const &
415 senf::console::factory::Directory::shortdoc(std::string const & doc)
416     const
417 {
418     node_->shortdoc(doc);
419     return *this;
420 }
421
422 ///////////////////////////////////////////////////////////////////////////
423 // senf::console::factory::Link
424
425 prefix_ senf::console::factory::Link::Link(GenericNode & target)
426     : node_ (LinkNode::create(target))
427 {}
428
429 prefix_ senf::console::LinkNode & senf::console::factory::Link::create(DirectoryNode & dir,
430                                                                        std::string const & name)
431     const
432 {
433     return dir.add(name, node_);
434 }
435
436 ///////////////////////////////cci.e///////////////////////////////////////
437 #undef prefix_
438
439 \f
440 // Local Variables:
441 // mode: c++
442 // fill-column: 100
443 // comment-column: 40
444 // c-file-style: "senf"
445 // indent-tabs-mode: nil
446 // ispell-local-dictionary: "american"
447 // compile-command: "scons -u test"
448 // End: