4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief ParsedCommand Boost.Preprocesser external iteration include */
26 #if !BOOST_PP_IS_ITERATING && !defined(MPP_ParsedCommand_)
27 #define MPP_ParsedCommand_ 1
30 #include <boost/preprocessor/iteration/iterate.hpp>
31 #include <boost/preprocessor/repetition/enum_trailing.hpp>
32 #include <boost/preprocessor/cat.hpp>
33 #include <boost/preprocessor/arithmetic/inc.hpp>
34 #include <boost/preprocessor/repetition/repeat.hpp>
35 #include <boost/type_traits/remove_reference.hpp>
36 #include <boost/type_traits/remove_const.hpp>
37 #include <boost/bind.hpp>
39 // ///////////////////////////mpp.p////////////////////////////////////////
40 #elif BOOST_PP_IS_ITERATING // ////////////////////////////////////////////
41 // ////////////////////////////////////////////////////////////////////////
44 #define mpp_ArgTypeN(n) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type)
45 #define mpp_ArgN(n) BOOST_PP_CAT(arg, BOOST_PP_INC(n))
47 #define mpp_ArgTypes_(z,n,d) typename traits::mpp_ArgTypeN(n)
48 #define mpp_TrailingArgTypes() BOOST_PP_ENUM_TRAILING( BOOST_PP_ITERATION(), mpp_ArgTypes_, _ )
50 #define mpp_Args_(z,n,d) boost::cref(mpp_ArgN(n))
51 #define mpp_TrailingArgs() BOOST_PP_ENUM_TRAILING( BOOST_PP_ITERATION(), mpp_Args_, _ )
53 #define mpp_BindArgs_(z,n,d) BOOST_PP_CAT( _, BOOST_PP_INC(BOOST_PP_INC(n)))
54 #define mpp_TrailingBindArgs() BOOST_PP_ENUM_TRAILING( BOOST_PP_ITERATION(), mpp_BindArgs_, _ )
56 // ////////////////////////////////////////////////////////////////////////
57 #if BOOST_PP_ITERATION_FLAGS()==1 // //////////////////////////////////////
58 // ////////////////////////////////////////////////////////////////////////
62 template <class FunctionTraits>
63 class ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >
64 : public ParsedCommandOverloadBase
67 typedef boost::intrusive_ptr<ParsedCommandOverload> ptr;
68 typedef FunctionTraits traits;
69 typedef boost::function<typename traits::result_type(std::ostream &
70 mpp_TrailingArgTypes())> Function;
72 # define mpp_l(z,n,d) \
73 typedef typename senf::remove_cvref< typename traits::mpp_ArgTypeN(n) >::type \
75 BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ )
78 static ptr create(Function fn);
83 ParsedCommandOverload(Function fn);
85 virtual void v_execute(std::ostream & os, ParseCommandInfo const & command) const;
90 // ////////////////////////////////////////////////////////////////////////
91 #elif BOOST_PP_ITERATION_FLAGS()==2 // ////////////////////////////////////
92 // ////////////////////////////////////////////////////////////////////////
94 // inline template implementation (.cti)
96 template <class FunctionTraits>
97 prefix_ typename senf::console::ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >::ptr
98 senf::console::ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >::create(Function fn)
100 return ptr(new ParsedCommandOverload(fn));
103 template <class FunctionTraits>
105 senf::console::ParsedCommandOverload<FunctionTraits,BOOST_PP_ITERATION()>::
106 ParsedCommandOverload(Function fn)
109 # define mpp_l(z,n,d) addParameter< mpp_ArgTypeN(n) >();
110 BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ )
114 // ////////////////////////////////////////////////////////////////////////
115 #elif BOOST_PP_ITERATION_FLAGS()==3 // ////////////////////////////////////
116 // ////////////////////////////////////////////////////////////////////////
118 // non-inline template implementation (.ct)
120 template <class FunctionTraits>
121 prefix_ void senf::console::ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >::
122 v_execute(std::ostream & os, ParseCommandInfo const & command)
125 if ( command.arguments().size() > BOOST_PP_ITERATION()
126 || (command.arguments().size() < BOOST_PP_ITERATION()
127 && ! arg( command.arguments().size() ).hasDefault) )
128 throw SyntaxErrorException("invalid number of arguments");
130 ParseCommandInfo::argument_iterator i (command.arguments().begin());
131 ParseCommandInfo::argument_iterator const i_end (command.arguments().end());
133 # define mpp_l(z,n,d) \
134 mpp_ArgTypeN(n) mpp_ArgN(n) (arg< mpp_ArgTypeN(n) >( n ).defaultValue); \
136 detail::ParameterTraits< mpp_ArgTypeN(n) >::parse( *(i++), mpp_ArgN(n) );
137 BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ )
140 // Now call the function binding the arguments to the values parsed above. callAndWrite is
141 // specialized to ignore a 'void' return value but automatically write all other values to the
143 detail::ReturnValueTraits<typename traits::result_type>::callAndWrite(
144 boost::bind(function_, boost::ref(os)
149 // ////////////////////////////////////////////////////////////////////////
150 #elif BOOST_PP_ITERATION_FLAGS()==4 // ////////////////////////////////////
151 // ////////////////////////////////////////////////////////////////////////
153 // CreateParsedCommandOverload
155 template <class Traits>
156 struct CreateParsedCommandOverload<Traits, true, BOOST_PP_ITERATION()>
158 typedef Traits traits;
160 template <class Function>
161 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
163 return senf::console::ParsedCommandOverload<traits>::create(
164 boost::bind(fn mpp_TrailingBindArgs()) );
169 // ////////////////////////////////////////////////////////////////////////
170 #endif // /////////////////////////////////////////////////////////////////
171 // ////////////////////////////////////////////////////////////////////////
172 // Undefine local Macros
174 #undef mpp_TrailingArgs
177 #undef mpp_TrailingArgTypes
183 // ////////////////////////////////////////////////////////////////////////
184 #endif // /////////////////////////////////////////////////////////////////
185 // ///////////////////////////mpp.e////////////////////////////////////////
191 // comment-column: 40
192 // c-file-style: "senf"
193 // indent-tabs-mode: nil
194 // ispell-local-dictionary: "american"
195 // compile-command: "scons -u test"