Node.cci
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief Node inline non-template implementation */
16 
17 // Custom includes
18 
19 #define prefix_ inline
20 //-/////////////////////////////////////////////////////////////////////////////////////////////////
21 
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 // senf::console::GenericNode
24 
25 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::thisptr()
26 {
27  return shared_from_this();
28 }
29 
30 prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
31  const
32 {
33  return shared_from_this();
34 }
35 
36 prefix_ senf::console::GenericNode::~GenericNode()
37 {}
38 
39 prefix_ std::string const & senf::console::GenericNode::name()
40  const
41 {
42  return name_;
43 }
44 
45 prefix_ senf::console::GenericNode::GenericNode()
46  : parent_ (nullptr)
47 {}
48 
49 prefix_ void senf::console::GenericNode::name(std::string const & name)
50 {
51  name_ = name;
52 }
53 
54 prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
55  const
56 {
57  return boost::static_pointer_cast<DirectoryNode>(
58  parent_ ? parent_->shared_from_this() : ptr() );
59 }
60 
61 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
62 {
63  if (parent_)
64  return parent()->remove(name());
65  else
66  return thisptr();
67 }
68 
69 prefix_ void senf::console::GenericNode::help(std::ostream & output)
70  const
71 {
72  v_help(output);
73 }
74 
75 prefix_ std::string senf::console::GenericNode::shorthelp()
76  const
77 {
78  return v_shorthelp();
79 }
80 
81 prefix_ bool senf::console::GenericNode::operator==(GenericNode & other)
82  const
83 {
84  return this == & other;
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::isDirectory()
94  const
95 {
96  return dynamic_cast<DirectoryNode const *>(this);
97 }
98 
99 prefix_ bool senf::console::GenericNode::isLink()
100  const
101 {
102  return dynamic_cast<LinkNode const *>(this);
103 }
104 
105 prefix_ bool senf::console::GenericNode::isCommand()
106  const
107 {
108  return dynamic_cast<CommandNode const *>(this);
109 }
110 
111 prefix_ senf::console::GenericNode const & senf::console::GenericNode::followLink()
112  const
113 {
114  return isLink()
115  ? dynamic_cast<LinkNode const *>(this)->follow()
116  : *this;
117 }
118 
119 prefix_ senf::console::GenericNode & senf::console::GenericNode::followLink()
120 {
121  return isLink()
122  ? dynamic_cast<LinkNode *>(this)->follow()
123  : *this;
124 }
125 
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 // senf::console::LinkNode
128 
129 prefix_ senf::console::GenericNode & senf::console::LinkNode::follow()
130  const
131 {
132  return *node_;
133 }
134 
135 prefix_ senf::console::LinkNode::ptr senf::console::LinkNode::create(GenericNode & node)
136 {
137  GenericNode::ptr p (node.thisptr());
138  while ( p->isLink() )
139  p = dynamic_cast<LinkNode&>(*p).follow().thisptr();
140  return ptr(new LinkNode(*p));
141 }
142 
143 prefix_ senf::console::LinkNode::LinkNode(GenericNode & node)
144  : node_ (node.thisptr())
145 {}
146 
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148 // senf::console::DirectoryNode
149 
150 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
151 {
152  return ptr(new DirectoryNode());
153 }
154 
155 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
156  const
157 {
158  return children_.find(name) != children_.end();
159 }
160 
161 prefix_ senf::console::GenericNode &
162 senf::console::DirectoryNode::get(std::string const & name)
163  const
164 {
165  return getLink(name).followLink();
166 }
167 
168 prefix_ senf::console::DirectoryNode &
169 senf::console::DirectoryNode::getDirectory(std::string const & name)
170  const
171 {
172  try {
173  return dynamic_cast<DirectoryNode&>(get(name));
174  }
175  SENF_WRAP_EXC(std::bad_cast)
176 }
177 
178 prefix_ senf::console::DirectoryNode &
179 senf::console::DirectoryNode::operator[](std::string const & name)
180  const
181 {
182  return getDirectory(name);
183 }
184 
185 prefix_ senf::console::CommandNode &
186 senf::console::DirectoryNode::getCommand(std::string const & name)
187  const
188 {
189  try {
190  return dynamic_cast<CommandNode&>(get(name));
191  }
192  SENF_WRAP_EXC(std::bad_cast)
193 }
194 
195 prefix_ senf::console::CommandNode &
196 senf::console::DirectoryNode::operator()(std::string const & name)
197  const
198 {
199  return getCommand(name);
200 }
201 
202 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
203  const
204 {
205  return boost::make_iterator_range(children_.begin(), children_.end());
206 }
207 
208 prefix_ senf::console::DirectoryNode::ChildrenRange
209 senf::console::DirectoryNode::completions(std::string const & s)
210  const
211 {
212  return boost::make_iterator_range(children_.lower_bound(s),
213  children_.lower_bound(s + "\xff"));
214 }
215 
216 prefix_ senf::console::DirectoryNode::DirectoryNode()
217 {}
218 
219 prefix_ senf::console::DirectoryNode &
220 senf::console::DirectoryNode::doc(std::string const & doc)
221 {
222  doc_ = doc;
223  return *this;
224 }
225 
226 prefix_ senf::console::DirectoryNode &
227 senf::console::DirectoryNode::shortdoc(std::string const & doc)
228 {
229  shortdoc_ = doc;
230  return *this;
231 }
232 
233 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
234 {
235  return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
236 }
237 
238 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
239  const
240 {
241  return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
242 }
243 
244 //-/////////////////////////////////////////////////////////////////////////////////////////////////
245 // senf::console::CommandNode
246 
247 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
248 {
249  return boost::static_pointer_cast<CommandNode>(shared_from_this());
250 }
251 
252 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
253  const
254 {
255  return boost::static_pointer_cast<CommandNode const>(shared_from_this());
256 }
257 
258 prefix_ senf::console::CommandNode::CommandNode()
259 {}
260 
261 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
262  ParseCommandInfo const & command)
263  const
264 {
265  boost::any rv;
266  execute(rv, output, command);
267 }
268 
269 prefix_ void senf::console::CommandNode::execute(boost::any & rv, std::ostream & output,
270  ParseCommandInfo const & command)
271  const
272 {
273  rv = boost::any();
274  v_execute(rv, output, command);
275 }
276 
277 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
278  ParseCommandInfo const & command)
279  const
280 {
281  execute(output, command);
282 }
283 
284 prefix_ void senf::console::CommandNode::operator()(boost::any & rv, std::ostream & output,
285  ParseCommandInfo const & command)
286  const
287 {
288  execute(rv, output, command);
289 }
290 
291 //-/////////////////////////////////////////////////////////////////////////////////////////////////
292 // senf::console::SimpleCommandNode
293 
294 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
295  : fn_ (fn)
296 {}
297 
298 prefix_ senf::console::SimpleCommandNode::ptr
299 senf::console::SimpleCommandNode::create(Function const & fn)
300 {
301  return ptr(new SimpleCommandNode(fn));
302 }
303 
304 prefix_ senf::console::SimpleCommandNode &
305 senf::console::SimpleCommandNode::doc(std::string const & doc)
306 {
307  doc_ = doc;
308  return *this;
309 }
310 
311 prefix_ senf::console::SimpleCommandNode &
312 senf::console::SimpleCommandNode::shortdoc(std::string const & doc)
313 {
314  shortdoc_ = doc;
315  return *this;
316 }
317 
318 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
319 {
320  return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
321 }
322 
323 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
324  const
325 {
326  return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
327 }
328 
329 //-/////////////////////////////////////////////////////////////////////////////////////////////////
330 
331 prefix_ senf::console::DirectoryNode & senf::console::provideDirectory(DirectoryNode & dir,
332  std::string const & name)
333 {
334  return dir.hasChild(name) ? dir.getDirectory(name) : dir.add(name, factory::Directory());
335 }
336 
337 
338 //-/////////////////////////////////////////////////////////////////////////////////////////////////
339 // senf::console::factory::SimpleCommand
340 
341 prefix_ senf::console::factory::SimpleCommand::SimpleCommand(SimpleCommandNode::Function fn)
342  : node_ (SimpleCommandNode::create(fn))
343 {}
344 
345 prefix_ senf::console::SimpleCommandNode &
346 senf::console::factory::SimpleCommand::create(DirectoryNode & dir, std::string const & name)
347  const
348 {
349  return dir.add(name, node_);
350 }
351 
352 prefix_ senf::console::factory::SimpleCommand const &
353 senf::console::factory::SimpleCommand::doc(std::string const & doc)
354  const
355 {
356  node_->doc(doc);
357  return *this;
358 }
359 
360 prefix_ senf::console::factory::SimpleCommand const &
361 senf::console::factory::SimpleCommand::shortdoc(std::string const & doc)
362  const
363 {
364  node_->shortdoc(doc);
365  return *this;
366 }
367 
368 //-/////////////////////////////////////////////////////////////////////////////////////////////////
369 // senf::console::factory::Directory
370 
371 prefix_ senf::console::factory::Directory::Directory()
372  : node_ (DirectoryNode::create())
373 {}
374 
375 prefix_ senf::console::DirectoryNode &
376 senf::console::factory::Directory::create(DirectoryNode & dir, std::string const & name)
377  const
378 {
379  return dir.add(name, node_);
380 }
381 
382 prefix_ senf::console::factory::Directory const &
383 senf::console::factory::Directory::doc(std::string const & doc)
384  const
385 {
386  node_->doc(doc);
387  return *this;
388 }
389 
390 prefix_ senf::console::factory::Directory const &
391 senf::console::factory::Directory::shortdoc(std::string const & doc)
392  const
393 {
394  node_->shortdoc(doc);
395  return *this;
396 }
397 
398 //-/////////////////////////////////////////////////////////////////////////////////////////////////
399 // senf::console::factory::Link
400 
401 prefix_ senf::console::factory::Link::Link(GenericNode & target)
402  : node_ (LinkNode::create(target))
403 {}
404 
405 prefix_ senf::console::LinkNode & senf::console::factory::Link::create(DirectoryNode & dir,
406  std::string const & name)
407  const
408 {
409  return dir.add(name, node_);
410 }
411 
412 //-/////////////////////////////////////////////////////////////////////////////////////////////////
413 #undef prefix_
414 
415 
416 // Local Variables:
417 // mode: c++
418 // fill-column: 100
419 // comment-column: 40
420 // c-file-style: "senf"
421 // indent-tabs-mode: nil
422 // ispell-local-dictionary: "american"
423 // compile-command: "scons -u test"
424 // End: