// $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::detail::ArgumentInfoBase prefix_ senf::console::detail::ArgumentInfoBase::ArgumentInfoBase(std::string const & type_) : type (type_), name (), hasDefault (false) {} /////////////////////////////////////////////////////////////////////////// // 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() ), defaultValue () {} template prefix_ std::string senf::console::detail::ArgumentInfo::defaultValueStr() const { return hasDefault ? ArgumentTraits::str(defaultValue) : ""; } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedCommandOverloadBase template prefix_ senf::console::detail::ArgumentInfo & senf::console::ParsedCommandOverloadBase::arg(unsigned n) const { return dynamic_cast &>(arg(n)); } 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(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::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:: 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 HaveArg(tag) boost::is_same< \ typename boost::parameter::binding::type, void >() argInfo( kw::name, args, HaveArg(kw::type::name) ); argInfo( kw::description, args, HaveArg(kw::type::description) ); argInfo( kw::default_value, args, HaveArg(kw::type::default_value) ); return next(); # undef HaveArg } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(Kw const &, ArgumentPack const &, boost::mpl::true_) const {} template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::false_) const { this->argName(args[kw::name]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::false_) const { this->argDoc(args[kw::description]); } template template prefix_ void senf::console::ParsedArgumentAttributor:: argInfo(boost::parameter::keyword const &, ArgumentPack const & args, boost::mpl::false_) const { this->defaultValue(args[kw::default_value]); } 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 { ParsedCommandAttributorBase::defaultValue(value); } /////////////////////////////////////////////////////////////////////////// // senf::console::ParsedArgumentAttributor template prefix_ senf::console::ParsedArgumentAttributor:: ParsedArgumentAttributor(Overload & overload) : ParsedArgumentAttributorBase< Overload, ParsedArgumentAttributor > (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::ParsedArgumentAttributor< 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 ParsedArgumentAttributor Attributor; return Attributor( cmdNode.add( CreateParsedCommandOverload< typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) ); } template prefix_ senf::console::ParsedArgumentAttributor< 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 ParsedArgumentAttributor Attributor; return Attributor( cmdNode.add( CreateParsedCommandOverload< typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create( senf::membind(fn,&owner)) ) ); } /////////////////////////////////////////////////////////////////////////// // senf::console::detail::CheckVoidReturn template template prefix_ void senf::console::detail::CheckVoidReturn::call(Fn fn, std::ostream & os) { ReturnValueTraits::format(fn(),os); os << "\n"; } template prefix_ void senf::console::detail::CheckVoidReturn::call(Fn fn, std::ostream & os) { fn(); } ///////////////////////////////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: