// $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 #define prefix_ inline ///////////////////////////////cti.p/////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandOverloadBase template prefix_ senf::console::detail::ParameterInfo & senf::console::ParsedCommandOverloadBase::arg(unsigned n) const { return dynamic_cast &>(arg(n)); } template prefix_ void senf::console::ParsedCommandOverloadBase::addParameter() { parameters_.push_back(detail::ParameterInfo::create()); } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandOverload #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \ SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \ 2)) #include BOOST_PP_ITERATE() /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandAttributorBase template prefix_ void senf::console::ParsedCommandAttributorBase::defaultValue(Type const & value) const { overload().arg(index_).defaultValue = value; overload().arg(index_).hasDefault = true; } /////////////////////////////////////////////////////////////////////////// // 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::ParsedAttributeAttributorBase template prefix_ Self senf::console::ParsedAttributeAttributorBase::doc(std::string const & doc) const { this->ParsedCommandAttributorBase::nodeDoc(doc); return static_cast(*this); } template prefix_ Self senf::console::ParsedAttributeAttributorBase:: overloadDoc(std::string const & doc) const { this->ParsedCommandAttributorBase::overloadDoc(doc); return static_cast(*this); } template prefix_ senf::console::ParsedAttributeAttributorBase:: ParsedAttributeAttributorBase(Overload & overload, unsigned index) : ParsedCommandAttributor (overload, index) {} /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedAttributeAttributor template template prefix_ typename senf::console::ParsedAttributeAttributor::next_type senf::console::ParsedAttributeAttributor:: argInfo(ArgumentPack const & args) const { typedef typename boost::parameter::binding< ArgumentPack, tag::detail::default_value_>::type default_value_t; return argInfo( args, boost::is_same() ); } template template prefix_ typename senf::console::ParsedAttributeAttributor::next_type senf::console::ParsedAttributeAttributor:: argInfo(ArgumentPack const & args, boost::mpl::true_) const { return argInfo( args[tag::name_ | ""], args[tag::description_ | ""] ); } template template prefix_ typename senf::console::ParsedAttributeAttributor::next_type senf::console::ParsedAttributeAttributor:: argInfo(ArgumentPack const & args, boost::mpl::false_) const { return argInfo( args[tag::name_ | ""], args[tag::description_ | ""], args[tag::default_value_ | value_type()] ); } template prefix_ typename senf::console::ParsedAttributeAttributor::next_type senf::console::ParsedAttributeAttributor::argInfo(std::string const & name, std::string const & doc) const { this->argName(name); this->argDoc(doc); return next(); } template prefix_ typename senf::console::ParsedAttributeAttributor::next_type senf::console::ParsedAttributeAttributor::argInfo(std::string const & name, std::string const & doc, value_type const & value) const { this->argName(name); this->argDoc(doc); defaultValue(value); return next(); } template prefix_ senf::console::ParsedAttributeAttributor:: ParsedAttributeAttributor(Overload & overload) : ParsedAttributeAttributorBase (overload, index) {} template prefix_ senf::console::ParsedAttributeAttributor senf::console::ParsedAttributeAttributor::next() const { return ParsedAttributeAttributor(this->overload()); } template prefix_ void senf::console::ParsedAttributeAttributor:: defaultValue(value_type const & value) const { ParsedCommandAttributorBase::defaultValue(value); } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedAttributeAttributor template prefix_ senf::console::ParsedAttributeAttributor:: ParsedAttributeAttributor(Overload & overload) : ParsedCommandAttributor (overload, index) {} /////////////////////////////////////////////////////////////////////////// // namespace members namespace { // 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 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(Console/ParsedCommand.mpp), \ 4)) # include BOOST_PP_ITERATE() } template prefix_ senf::console::ParsedAttributeAttributor< senf::console::ParsedCommandOverload< typename senf::console::detail::ParsedCommandTraits::traits> > senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int) { OverloadedCommandNode & cmdNode ( node.hasChild(name) ? dynamic_cast(node(name)) : node.add(name, OverloadedCommandNode::create()) ); typedef detail::ParsedCommandTraits CmdTraits; typedef ParsedCommandOverload Overload; typedef ParsedAttributeAttributor Attributor; return Attributor( cmdNode.add( CreateParsedCommandOverload< typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) ); } template prefix_ senf::console::ParsedAttributeAttributor< senf::console::ParsedCommandOverload< typename senf::console::detail::ParsedCommandTraits::traits> > 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 *) { OverloadedCommandNode & cmdNode ( node.hasChild(name) ? dynamic_cast(node(name)) : node.add(name, OverloadedCommandNode::create()) ); typedef detail::ParsedCommandTraits CmdTraits; typedef ParsedCommandOverload Overload; typedef ParsedAttributeAttributor Attributor; return Attributor( cmdNode.add( CreateParsedCommandOverload< typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create( senf::membind(fn,&owner)) ) ); } ///////////////////////////////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: