To greatly simplify parsing complex commands, we turn to automatic argument parsing.
- \subsection console_autoadd Adding automatically parsed commands
+ \subsection console_autoadd Adding
Automatically parsed commands are registered by just adding a callback which has the correct
arguments and return-value defined:
</pre>
\endhtmlonly
- \subsection command_overload Command overloading
+ \subsection command_overload Overloading
Automatically parsed commands can be overloaded: You can register multiple commands under the
same name. Each overload is tried in turn until no SyntaxErrorException is raised.
server:/$
</pre>
- \subsection console_attributes Attributes of automatically parsed commands
+ \subsection console_attributes Attributes
As have seen so far, some documentation is automatically provided. We can add more info, by
setting additional attributes.
</pre>
\endhtmlonly
-
- \subsection console_argattribpos Passing argument attributes as positional arguments
-
Since most of the time, we only need to set the name and possibly a description for arguments,
there is a shortcut: name and description can be specified as positional arguments in this
- order. So the following will give the exactly same result as the example in the previous section
-
+ order. So the following will give the exactly same result as above:
\code
namespace kw = senf::console::kw;
\endhtmlonly
- \subsection console_boostfn Adding non-function-pointer callable objects
+ \subsection console_boostfn Non-function-pointer commands
It is possible to add other callable objects besides function (and member-function)
pointers. However, since it is not possible to automatically deduce the argument and return
<table class="senf fixedwidth">
- <tr><td>\link senf::console::ParsedArgumentAttributorBase::doc() .doc\endlink ( \e doc )</td><td>Set
- documentation for all overloads</td></tr>
+ <tr><td style="width:14em">\link senf::console::ParsedArgumentAttributorBase::doc() .doc\endlink
+ ( \e doc )</td><td>Set documentation for all overloads</td></tr>
<tr><td>\link senf::console::ParsedArgumentAttributorBase::overloadDoc()
.overloadDoc\endlink ( \e doc )</td><td>Set documentation for a specific overload</td></tr>
<table class="senf fixed width">
- <tr><td>\link senf::console::kw::name kw::name\endlink</td><td>Parameter name</td></tr>
+ <tr><td style="width:14em">\link senf::console::kw::name kw::name\endlink</td><td>Parameter
+ name</td></tr>
<tr><td>\link senf::console::kw::description kw::description\endlink</td><td>One-line
description of the argument</td></tr>
\ref senf::console::kw for a list of all argument attribute keywords
- \section console_memberfn Registering member functions
+ \section console_memberfn Member functions
Member functions are supported like non-member functions. They must however be added through a
senf::console::ScopedDirectory instance to bind them to their instance.
--- /dev/null
+// $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 Variables inline template implementation */
+
+#include "Variables.ih"
+
+// Custom includes
+
+#define prefix_ inline
+///////////////////////////////cti.p///////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::detail::QueryVariable<Variable>
+
+template <class Variable>
+prefix_ senf::console::detail::QueryVariable<Variable>::QueryVariable(Variable const & var)
+ : var_ (var)
+{}
+
+template <class Variable>
+prefix_ Variable const & senf::console::detail::QueryVariable<Variable>::operator()()
+ const
+{
+ return var_;
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::detail::SetVariable<Variable>
+
+template <class Variable>
+prefix_ senf::console::detail::SetVariable<Variable>::SetVariable(Variable & var)
+ : var_ (var)
+{}
+
+template <class Variable>
+prefix_ void senf::console::detail::SetVariable<Variable>::operator()(Variable const & value)
+ const
+{
+ if (handler_) {
+ Variable old (var_);
+ var_ = value;
+ handler_(old);
+ }
+ else
+ var_ = value;
+}
+
+template <class Variable>
+prefix_ void senf::console::detail::SetVariable<Variable>::onChange(OnChangeHandler handler)
+{
+ handler_ = handler;
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::console::VariableAttributor<Variable>
+
+template <class Variable>
+prefix_
+senf::console::VariableAttributor<Variable>::VariableAttributor(QueryOverload & queryOverload,
+ SetOverload & setOverload)
+ : queryOverload_ (queryOverload), setOverload_ (setOverload)
+{}
+
+///////////////////////////////////////////////////////////////////////////
+
+template <class Variable>
+prefix_ senf::console::VariableAttributor<Variable>
+senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
+ Variable * var, int)
+{
+ typename VariableAttributor<Variable>::SetOverload & setOverload (
+ node.add(name, typename detail::SetVariable<Variable>::Function(
+ detail::SetVariable<Variable>(*var))).overload() );
+ typename VariableAttributor<Variable>::QueryOverload & queryOverload (
+ node.add(name, typename detail::QueryVariable<Variable>::Function(
+ detail::QueryVariable<Variable>(*var))).overload() );
+
+ return VariableAttributor<Variable>(queryOverload, setOverload);
+}
+
+///////////////////////////////cti.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:
--- /dev/null
+// $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 Variables public header */
+
+#ifndef HH_Variables_
+#define HH_Variables_ 1
+
+// Custom includes
+#include "ParsedCommand.hh"
+
+#include "Variables.ih"
+//#include "Variables.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace senf {
+namespace console {
+
+ template <class Variable>
+ class VariableAttributor
+ {
+ public:
+ typedef typename detail::SetVariable<Variable>::Traits::Overload SetOverload;
+ typedef typename detail::QueryVariable<Variable>::Traits::Overload QueryOverload;
+
+ typedef OverloadedCommandNode node_type;
+ typedef VariableAttributor return_type;
+
+ protected:
+
+ private:
+ VariableAttributor(QueryOverload & queryOverload, SetOverload & setOverload);
+
+ QueryOverload & queryOverload_;
+ SetOverload & setOverload_;
+
+#ifndef DOXYGEN
+
+ template <class V>
+ friend VariableAttributor<V> senf_console_add_node(DirectoryNode & node,
+ std::string const & name,
+ V * var, int);
+
+#endif
+
+ };
+
+ template <class Variable>
+ VariableAttributor<Variable> senf_console_add_node(DirectoryNode & node,
+ std::string const & name,
+ Variable * var, int);
+
+}}
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "Variables.cci"
+//#include "Variables.ct"
+#include "Variables.cti"
+#endif
+
+\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:
--- /dev/null
+// $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 Variables internal header */
+
+#ifndef IH_Variables_
+#define IH_Variables_ 1
+
+// Custom includes
+#include <boost/function.hpp>
+
+///////////////////////////////ih.p////////////////////////////////////////
+
+namespace senf {
+namespace console {
+namespace detail {
+
+ template <class Variable>
+ struct QueryVariable
+ {
+ typedef Variable const & Signature ();
+ typedef boost::function<Signature> Function;
+ typedef detail::ParsedCommandTraits<Signature> Traits;
+
+ QueryVariable(Variable const & var);
+
+ Variable const & operator()() const;
+
+ Variable const & var_;
+ };
+
+ template <class Variable>
+ struct SetVariable
+ {
+ typedef void Signature (Variable &);
+ typedef boost::function<Signature> Function;
+ typedef detail::ParsedCommandTraits<Signature> Traits;
+ typedef boost::function<void (Variable const &)> OnChangeHandler;
+
+ SetVariable(Variable & var);
+
+ void operator()(Variable const & value) const;
+ void onChange(OnChangeHandler handler);
+
+ Variable & var_;
+ OnChangeHandler handler_;
+ };
+
+}}}
+
+///////////////////////////////ih.e////////////////////////////////////////
+#endif
+
+\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:
--- /dev/null
+// $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 Variables.test unit tests */
+
+//#include "Variables.test.hh"
+//#include "Variables.test.ih"
+
+// Custom includes
+#include <iostream>
+#include <sstream>
+#include "Variables.hh"
+#include "Executor.hh"
+#include "Parse.hh"
+#include "ScopedDirectory.hh"
+
+#include <boost/test/auto_unit_test.hpp>
+#include <boost/test/test_tools.hpp>
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+BOOST_AUTO_UNIT_TEST(variables)
+{
+ senf::console::Executor executor;
+ senf::console::CommandParser parser;
+ senf::console::ScopedDirectory<> dir;
+ senf::console::root().add("test", dir);
+
+ int var (5);
+
+ std::stringstream ss;
+ dir.add("var", &var);
+ parser.parse("test/var; test/var 10; test/var",
+ boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
+ BOOST_CHECK_EQUAL( ss.str(), "5\n10\n" );
+}
+
+///////////////////////////////cc.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: