Move sourcecode into 'senf/' directory
[senf.git] / senf / Utils / Console / Node.cci
diff --git a/senf/Utils/Console/Node.cci b/senf/Utils/Console/Node.cci
new file mode 100644 (file)
index 0000000..f1935b3
--- /dev/null
@@ -0,0 +1,400 @@
+// $Id$
+//
+// Copyright (C) 2008 
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief Node inline non-template implementation */
+
+#include "Node.ih"
+
+// Custom includes
+#include "../../Utils/senfassert.hh"
+
+#define prefix_ inline
+///////////////////////////////cci.p///////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::GenericNode
+
+prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::thisptr()
+{
+    return shared_from_this();
+}
+
+prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
+    const
+{
+    return shared_from_this();
+}
+
+prefix_ senf::console::GenericNode::~GenericNode()
+{}
+
+prefix_ std::string const & senf::console::GenericNode::name()
+    const
+{
+    return name_;
+}
+
+prefix_ senf::console::GenericNode::GenericNode()
+    : parent_ (0)
+{}
+
+prefix_ void senf::console::GenericNode::name(std::string const & name)
+{
+    name_ = name;
+}
+
+prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
+    const
+{
+    return boost::static_pointer_cast<DirectoryNode>(
+        parent_ ? parent_->shared_from_this() : ptr() );
+}
+
+prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
+{
+    if (parent_)
+        return parent()->remove(name());
+    else
+        return thisptr();
+}
+
+prefix_ void senf::console::GenericNode::help(std::ostream & output)
+    const
+{
+    v_help(output);
+}
+
+prefix_ std::string senf::console::GenericNode::shorthelp()
+    const
+{
+    return v_shorthelp();
+}
+
+prefix_ bool senf::console::GenericNode::operator==(GenericNode & other)
+    const
+{
+    return this == & other;
+}
+
+prefix_ bool senf::console::GenericNode::operator!=(GenericNode & other)
+    const
+{
+    return this != & other;
+}
+
+prefix_ bool senf::console::GenericNode::isDirectory()
+    const
+{
+    return dynamic_cast<DirectoryNode const *>(this);
+}
+
+prefix_ bool senf::console::GenericNode::isLink()
+    const
+{
+    return dynamic_cast<LinkNode const *>(this);
+}
+
+prefix_ bool senf::console::GenericNode::isCommand()
+    const
+{
+    return dynamic_cast<CommandNode const *>(this);
+}
+
+prefix_ senf::console::GenericNode const & senf::console::GenericNode::followLink()
+    const
+{
+    return isLink()
+        ? dynamic_cast<LinkNode const *>(this)->follow()
+        : *this;
+}
+
+prefix_ senf::console::GenericNode & senf::console::GenericNode::followLink()
+{
+    return isLink()
+        ? dynamic_cast<LinkNode *>(this)->follow()
+        : *this;
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::LinkNode
+
+prefix_ senf::console::GenericNode & senf::console::LinkNode::follow()
+    const
+{
+    return *node_;
+}
+
+prefix_ senf::console::LinkNode::ptr senf::console::LinkNode::create(GenericNode & node)
+{
+    GenericNode::ptr p (node.thisptr());
+    while ( p->isLink() )
+        p = dynamic_cast<LinkNode&>(*p).follow().thisptr();
+    return ptr(new LinkNode(*p));
+}
+
+prefix_ senf::console::LinkNode::LinkNode(GenericNode & node)
+    : node_ (node.thisptr())
+{}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::DirectoryNode
+
+prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
+{
+    return ptr(new DirectoryNode());
+}
+
+prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
+    const
+{
+    ChildMap::const_iterator i (children_.find(name));
+    return i != children_.end();
+}
+
+prefix_ senf::console::GenericNode &
+senf::console::DirectoryNode::get(std::string const & name)
+    const
+{
+    return getLink(name).followLink();
+}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::DirectoryNode::getDirectory(std::string const & name)
+    const
+{
+    try {
+        return dynamic_cast<DirectoryNode&>(get(name));
+    }
+    SENF_WRAP_EXC(std::bad_cast)
+}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::DirectoryNode::operator[](std::string const & name)
+    const
+{
+    return getDirectory(name);
+}
+
+prefix_ senf::console::CommandNode &
+senf::console::DirectoryNode::getCommand(std::string const & name)
+    const
+{
+    try {
+        return dynamic_cast<CommandNode&>(get(name));
+    }
+    SENF_WRAP_EXC(std::bad_cast)
+}
+
+prefix_ senf::console::CommandNode &
+senf::console::DirectoryNode::operator()(std::string const & name)
+    const
+{
+    return getCommand(name);
+}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::DirectoryNode::mkdir(std::string const & name)
+{
+    return add(name, create());
+}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::DirectoryNode::provideDirectory(std::string const & name)
+{
+    return hasChild(name) ? getDirectory(name) : mkdir(name);
+}
+
+prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
+    const
+{
+    return boost::make_iterator_range(children_.begin(), children_.end());
+}
+
+prefix_ senf::console::DirectoryNode::ChildrenRange
+senf::console::DirectoryNode::completions(std::string const & s)
+    const
+{
+    return boost::make_iterator_range(children_.lower_bound(s),
+                                      children_.lower_bound(s + "\xff"));
+}
+
+prefix_ void senf::console::DirectoryNode::link(std::string const & name, GenericNode & target)
+{
+    add(name, LinkNode::create(target));
+}
+
+prefix_ senf::console::DirectoryNode::DirectoryNode()
+{}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::DirectoryNode::doc(std::string const & doc)
+{
+    doc_ = doc;
+    return *this;
+}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::DirectoryNode::shortdoc(std::string const & doc)
+{
+    shortdoc_ = doc;
+    return *this;
+}
+
+prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
+{
+    return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
+}
+
+prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
+    const
+{
+    return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::detail::NodeTraverser
+#ifndef DOXYGEN
+
+prefix_ senf::console::detail::NodeTraverser::NodeTraverser(DirectoryNode & root,
+                                                            DirectoryNode & dir,
+                                                            bool autocomplete)
+    : root_ (root), dir_ (dir.thisptr()), autocomplete_ (autocomplete), init_ (false)
+{}
+#endif
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::CommandNode
+
+prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
+{
+    return boost::static_pointer_cast<CommandNode>(shared_from_this());
+}
+
+prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
+    const
+{
+    return boost::static_pointer_cast<CommandNode const>(shared_from_this());
+}
+
+prefix_ senf::console::CommandNode::CommandNode()
+{}
+
+prefix_ void senf::console::CommandNode::execute(std::ostream & output,
+                                                 ParseCommandInfo const & command)
+    const
+{
+    boost::any rv;
+    execute(rv, output, command);
+}
+
+prefix_ void senf::console::CommandNode::execute(boost::any & rv, std::ostream & output,
+                                                 ParseCommandInfo const & command)
+    const
+{
+    rv = boost::any();
+    v_execute(rv, output, command);
+}
+
+prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
+                                                    ParseCommandInfo const & command)
+    const
+{
+    execute(output, command);
+}
+
+prefix_ void senf::console::CommandNode::operator()(boost::any & rv, std::ostream & output,
+                                                    ParseCommandInfo const & command)
+    const
+{
+    execute(rv, output, command);
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::SimpleCommandNode
+
+prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
+    : fn_ (fn)
+{}
+
+prefix_ senf::console::SimpleCommandNode::ptr
+senf::console::SimpleCommandNode::create(Function const & fn)
+{
+    return ptr(new SimpleCommandNode(fn));
+}
+
+prefix_ senf::console::SimpleCommandNode &
+senf::console::SimpleCommandNode::doc(std::string const & doc)
+{
+    doc_ = doc;
+    return *this;
+}
+
+prefix_ senf::console::SimpleCommandNode &
+senf::console::SimpleCommandNode::shortdoc(std::string const & doc)
+{
+    shortdoc_ = doc;
+    return *this;
+}
+
+prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
+{
+    return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
+}
+
+prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
+    const
+{
+    return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
+}
+
+#ifndef DOXYGEN
+
+prefix_ senf::console::SimpleCommandNode &
+senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
+                                     SimpleCommandNode::Function fn, int)
+{
+    return node.add(name, SimpleCommandNode::create(fn));
+}
+
+prefix_ senf::console::DirectoryNode &
+senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
+                                     DirectoryNode & dir, int)
+{
+    return node.add(name, dir.thisptr());
+}
+
+#endif
+
+///////////////////////////////cci.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// End: