// $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 ParsedCommand inline template implementation */ #include "ParsedCommand.ih" // Custom includes #include "../../Utils/membind.hh" #include #include "../../Utils/parameter.hh" #define prefix_ inline ///////////////////////////////cti.p/////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// // senf::console::detail::ArgumentInfo template prefix_ typename senf::console::detail::ArgumentInfo::ptr senf::console::detail::ArgumentInfo::create() { return ptr(new ArgumentInfo()); } template prefix_ senf::console::detail::ArgumentInfo::ArgumentInfo() : ArgumentInfoBase ( ArgumentTraits::description(), ArgumentTraits::singleToken ), defaultValue () {} template prefix_ std::string senf::console::detail::ArgumentInfo::defaultValueStr() const { return hasDefault ? ArgumentTraits::str(defaultValue) : ""; } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandOverloadBase template prefix_ void senf::console::ParsedCommandOverloadBase::addParameter() { parameters_.push_back(detail::ArgumentInfo::create()); } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandOverload #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \ SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \ 2)) #include BOOST_PP_ITERATE() /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandAttributor template prefix_ Overload & senf::console::ParsedCommandAttributor::overload() const { return static_cast(ParsedCommandAttributorBase::overload()); } template prefix_ senf::console::ParsedCommandAttributor::ParsedCommandAttributor(Overload & overload, unsigned index) : ParsedCommandAttributorBase (overload, index) {} /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedArgumentAttributorBase template prefix_ Self senf::console::ParsedArgumentAttributorBase::doc(std::string const & doc) const { this->ParsedCommandAttributorBase::nodeDoc(doc); return static_cast(*this); } template prefix_ Self senf::console::ParsedArgumentAttributorBase:: shortdoc(std::string const & doc) const { this->ParsedCommandAttributorBase::shortDoc(doc); return static_cast(*this); } template prefix_ Self senf::console::ParsedArgumentAttributorBase:: overloadDoc(std::string const & doc) const { this->ParsedCommandAttributorBase::overloadDoc(doc); return static_cast(*this); } template prefix_ Self senf::console::ParsedArgumentAttributorBase:: formatter(typename Overload::Formatter f) const { this->overload().formatter(f); return static_cast(*this); } template prefix_ senf::console::ParsedArgumentAttributorBase:: ParsedArgumentAttributorBase(Overload & overload, unsigned index) : ParsedCommandAttributor (overload, index) {} template prefix_ Self senf::console::ParsedArgumentAttributorBase::doc(std::string const & doc) const { this->ParsedCommandAttributorBase::nodeDoc(doc); return static_cast(*this); } template prefix_ Self senf::console::ParsedArgumentAttributorBase:: shortdoc(std::string const & doc) const { this->ParsedCommandAttributorBase::shortDoc(doc); return static_cast(*this); } template prefix_ Self senf::console::ParsedArgumentAttributorBase:: overloadDoc(std::string const & doc) const { this->ParsedCommandAttributorBase::overloadDoc(doc); return static_cast(*this); } template prefix_ senf::console::ParsedArgumentAttributorBase:: ParsedArgumentAttributorBase(Overload & overload, unsigned index) : ParsedCommandAttributor (overload, index) {} /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedArgumentAttributor template prefix_ typename senf::console::ParsedArgumentAttributor::next_type senf::console::ParsedArgumentAttributor::arg() const { return next(); } template template prefix_ typename senf::console::ParsedArgumentAttributor::next_type senf::console::ParsedArgumentAttributor:: argInfo(ArgumentPack const & args) const { # define ProcessArg(tag) \ argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() ) ProcessArg(name); ProcessArg(description); ProcessArg(default_value); ProcessArg(type_name); ProcessArg(default_doc); ProcessArg(parser); return next(); # undef ProcessArg } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_) const {} template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::true_) const { this->argName(args[kw::name]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::true_) const { this->argDoc(args[kw::description]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::true_) const { this->defaultValue(args[kw::default_value]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::true_) const { this->typeName(args[kw::type_name]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::true_) const { BOOST_STATIC_ASSERT(( senf::has_parameter::value )); this->defaultDoc(args[kw::default_doc]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::true_) const { this->parser(args[kw::parser]); } template prefix_ senf::console::ParsedArgumentAttributor:: ParsedArgumentAttributor(Overload & overload) : ParsedArgumentAttributorBase (overload, index) {} template prefix_ typename senf::console::ParsedArgumentAttributor::next_type senf::console::ParsedArgumentAttributor::next() const { return ParsedArgumentAttributor(this->overload()); } template prefix_ void senf::console::ParsedArgumentAttributor:: defaultValue(value_type const & value) const { this->overload().arg().defaultValue = value; this->overload().arg(index).hasDefault = true; } template template prefix_ void senf::console::ParsedArgumentAttributor::parser(Fn fn) const { this->overload().arg().parser = fn; } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedArgumentAttributor template prefix_ senf::console::ParsedArgumentAttributor:: ParsedArgumentAttributor(Overload & overload) : ParsedArgumentAttributorBase< Overload, ParsedArgumentAttributor > (overload, index) {} /////////////////////////////////////////////////////////////////////////// // namespace members namespace senf { namespace console { namespace detail { #ifndef DOXYGEN struct ParsedCommandAddNodeAccess { template static Attributor attributor(Node & node) { return Attributor(node); } }; // What is THIS about ?? // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If // this argument is given, then the function will be called with the console output stream as // it's first argument. // // This is implemented in the following way: ParsedCommandOverload (the class responsible for // calling the callback) will ALWAYS pass the stream as first argument. If the user callback // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied // function will be directly passed to ParsedCommandOverload. // // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and // the create member will use boost::bind to DROP the first argument. template struct CreateParsedCommandOverload {}; template struct CreateParsedCommandOverload { typedef typename Traits::traits traits; template static typename senf::console::ParsedCommandOverload::ptr create(Function fn) { return senf::console::ParsedCommandOverload::create(fn); }; }; # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \ SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \ 4)) # include BOOST_PP_ITERATE() template typename senf::console::detail::ParsedCommandTraits::Attributor addOverloadedCommandNode(senf::console::DirectoryNode & node, std::string const & name, Fn fn) { senf::console::OverloadedCommandNode & cmdNode ( node.hasChild(name) ? dynamic_cast(node(name)) : node.add(name, senf::console::OverloadedCommandNode::create()) ); typedef senf::console::detail::ParsedCommandTraits CmdTraits; typedef senf::console::ParsedCommandOverload Overload; typedef senf::console::ParsedArgumentAttributor Attributor; return senf::console::detail::ParsedCommandAddNodeAccess::attributor( cmdNode.add( CreateParsedCommandOverload::create(fn) ) ); } #endif }}} #ifndef DOXYGEN template typename senf::console::detail::ParsedCommandTraits::Attributor senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int, typename boost::enable_if_c::is_callable>::type *) { return senf::console::detail::addOverloadedCommandNode(node, name, fn); } template typename senf::console::detail::ParsedCommandTraits::Attributor senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name, boost::function fn, int) { return senf::console::detail::addOverloadedCommandNode(node, name, fn); } template typename senf::console::detail::ParsedCommandTraits::Attributor senf::console::senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name, Function fn, int, typename boost::enable_if_c::is_member>::type *) { return senf::console::detail::addOverloadedCommandNode( node, name, senf::membind(fn,&owner)); } #endif ///////////////////////////////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: