From: g0dil Date: Wed, 16 Apr 2008 23:45:30 +0000 (+0000) Subject: Console: Begin implementation of 'Variable' commands X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=80c83d2565c50f8ad33af2be0f4cb3e5735cafcf;p=senf.git Console: Begin implementation of 'Variable' commands git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@815 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Console/Mainpage.dox b/Console/Mainpage.dox index d1de4fe..62cffd6 100644 --- a/Console/Mainpage.dox +++ b/Console/Mainpage.dox @@ -266,7 +266,7 @@ 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: @@ -332,7 +332,7 @@ \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. @@ -358,7 +358,7 @@ server:/$ - \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. @@ -444,13 +444,9 @@ \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; @@ -513,7 +509,7 @@ \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 @@ -550,8 +546,8 @@ - + @@ -565,7 +561,8 @@
\link senf::console::ParsedArgumentAttributorBase::doc() .doc\endlink ( \e doc )Set - documentation for all overloads
\link senf::console::ParsedArgumentAttributorBase::doc() .doc\endlink + ( \e doc )Set documentation for all overloads
\link senf::console::ParsedArgumentAttributorBase::overloadDoc() .overloadDoc\endlink ( \e doc )Set documentation for a specific overload
- + @@ -581,7 +578,7 @@ \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. diff --git a/Console/Variables.cti b/Console/Variables.cti new file mode 100644 index 0000000..2cdff1b --- /dev/null +++ b/Console/Variables.cti @@ -0,0 +1,114 @@ +// $Id$ +// +// Copyright (C) 2008 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// 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 + +template +prefix_ senf::console::detail::QueryVariable::QueryVariable(Variable const & var) + : var_ (var) +{} + +template +prefix_ Variable const & senf::console::detail::QueryVariable::operator()() + const +{ + return var_; +} + +/////////////////////////////////////////////////////////////////////////// +// senf::console::detail::SetVariable + +template +prefix_ senf::console::detail::SetVariable::SetVariable(Variable & var) + : var_ (var) +{} + +template +prefix_ void senf::console::detail::SetVariable::operator()(Variable const & value) + const +{ + if (handler_) { + Variable old (var_); + var_ = value; + handler_(old); + } + else + var_ = value; +} + +template +prefix_ void senf::console::detail::SetVariable::onChange(OnChangeHandler handler) +{ + handler_ = handler; +} + +/////////////////////////////////////////////////////////////////////////// +// senf::console::VariableAttributor + +template +prefix_ +senf::console::VariableAttributor::VariableAttributor(QueryOverload & queryOverload, + SetOverload & setOverload) + : queryOverload_ (queryOverload), setOverload_ (setOverload) +{} + +/////////////////////////////////////////////////////////////////////////// + +template +prefix_ senf::console::VariableAttributor +senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name, + Variable * var, int) +{ + typename VariableAttributor::SetOverload & setOverload ( + node.add(name, typename detail::SetVariable::Function( + detail::SetVariable(*var))).overload() ); + typename VariableAttributor::QueryOverload & queryOverload ( + node.add(name, typename detail::QueryVariable::Function( + detail::QueryVariable(*var))).overload() ); + + return VariableAttributor(queryOverload, setOverload); +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// 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: diff --git a/Console/Variables.hh b/Console/Variables.hh new file mode 100644 index 0000000..58924b2 --- /dev/null +++ b/Console/Variables.hh @@ -0,0 +1,90 @@ +// $Id$ +// +// Copyright (C) 2008 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// 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 VariableAttributor + { + public: + typedef typename detail::SetVariable::Traits::Overload SetOverload; + typedef typename detail::QueryVariable::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 + friend VariableAttributor senf_console_add_node(DirectoryNode & node, + std::string const & name, + V * var, int); + +#endif + + }; + + template + VariableAttributor 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 + + +// 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: diff --git a/Console/Variables.ih b/Console/Variables.ih new file mode 100644 index 0000000..e1a2ce8 --- /dev/null +++ b/Console/Variables.ih @@ -0,0 +1,83 @@ +// $Id$ +// +// Copyright (C) 2008 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// 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 + +///////////////////////////////ih.p//////////////////////////////////////// + +namespace senf { +namespace console { +namespace detail { + + template + struct QueryVariable + { + typedef Variable const & Signature (); + typedef boost::function Function; + typedef detail::ParsedCommandTraits Traits; + + QueryVariable(Variable const & var); + + Variable const & operator()() const; + + Variable const & var_; + }; + + template + struct SetVariable + { + typedef void Signature (Variable &); + typedef boost::function Function; + typedef detail::ParsedCommandTraits Traits; + typedef boost::function OnChangeHandler; + + SetVariable(Variable & var); + + void operator()(Variable const & value) const; + void onChange(OnChangeHandler handler); + + Variable & var_; + OnChangeHandler handler_; + }; + +}}} + +///////////////////////////////ih.e//////////////////////////////////////// +#endif + + +// 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: diff --git a/Console/Variables.test.cc b/Console/Variables.test.cc new file mode 100644 index 0000000..74cdece --- /dev/null +++ b/Console/Variables.test.cc @@ -0,0 +1,71 @@ +// $Id$ +// +// Copyright (C) 2008 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// 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 +#include +#include "Variables.hh" +#include "Executor.hh" +#include "Parse.hh" +#include "ScopedDirectory.hh" + +#include +#include + +#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( boost::ref(executor), boost::ref(ss), _1 )); + BOOST_CHECK_EQUAL( ss.str(), "5\n10\n" ); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// 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:
\link senf::console::kw::name kw::name\endlinkParameter name
\link senf::console::kw::name kw::name\endlinkParameter + name
\link senf::console::kw::description kw::description\endlinkOne-line description of the argument