Utils: Add some type traits in type_traits.hh
[senf.git] / Console / ParsedCommand.mpp
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief ParsedCommand Boost.Preprocesser external iteration include */
25
26 #if !BOOST_PP_IS_ITERATING && !defined(MPP_ParsedCommand_)
27 #define MPP_ParsedCommand_ 1
28
29 // Custom includes
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/bind.hpp>
37
38 // ///////////////////////////mpp.p////////////////////////////////////////
39 #elif BOOST_PP_IS_ITERATING // ////////////////////////////////////////////
40 // ////////////////////////////////////////////////////////////////////////
41 // Local Macros
42
43 #define mpp_ArgTypeN(n) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type)
44 #define mpp_ArgN(n) BOOST_PP_CAT(arg, BOOST_PP_INC(n))
45
46 #define mpp_ArgTypes_(z,n,d) typename traits::mpp_ArgTypeN(n)
47 #define mpp_TrailingArgTypes() BOOST_PP_ENUM_TRAILING( BOOST_PP_ITERATION(), mpp_ArgTypes_, _ )
48
49 #define mpp_Args_(z,n,d) boost::cref(mpp_ArgN(n))
50 #define mpp_TrailingArgs() BOOST_PP_ENUM_TRAILING( BOOST_PP_ITERATION(), mpp_Args_, _ )
51
52 #define mpp_BindArgs_(z,n,d) BOOST_PP_CAT( _, BOOST_PP_INC(BOOST_PP_INC(n)))
53 #define mpp_TrailingBindArgs() BOOST_PP_ENUM_TRAILING( BOOST_PP_ITERATION(), mpp_BindArgs_, _ )
54
55 // ////////////////////////////////////////////////////////////////////////
56 #if BOOST_PP_ITERATION_FLAGS()==1 // //////////////////////////////////////
57 // ////////////////////////////////////////////////////////////////////////
58
59 // Header file (.hh)
60
61 template <class FunctionTraits>
62 class ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >
63     : public ParsedCommandOverloadBase
64 {
65 public:
66     typedef boost::intrusive_ptr<ParsedCommandOverload> ptr;
67     typedef FunctionTraits traits;
68     typedef boost::function<typename traits::result_type(std::ostream &
69                                                          mpp_TrailingArgTypes())> Function;
70
71 #   define mpp_l(z,n,d)                                                                         \
72         typedef typename boost::remove_reference<typename traits::mpp_ArgTypeN(n)>::type        \
73             mpp_ArgTypeN(n);
74     BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ )
75 #   undef mpp_l
76
77     static ptr create(Function fn);
78
79 protected:
80
81 private:
82     ParsedCommandOverload(Function fn);
83
84     virtual void v_execute(std::ostream & os, ParseCommandInfo const & command) const;
85
86     Function function_;
87 };
88
89 // ////////////////////////////////////////////////////////////////////////
90 #elif BOOST_PP_ITERATION_FLAGS()==2 // ////////////////////////////////////
91 // ////////////////////////////////////////////////////////////////////////
92
93 // inline template implementation (.cti)
94
95 template <class FunctionTraits>
96 prefix_ typename senf::console::ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >::ptr
97 senf::console::ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >::create(Function fn)
98 {
99     return ptr(new ParsedCommandOverload(fn));
100 }
101
102 template <class FunctionTraits>
103 prefix_
104 senf::console::ParsedCommandOverload<FunctionTraits,BOOST_PP_ITERATION()>::
105 ParsedCommandOverload(Function fn)
106     : function_ (fn) 
107 {
108 #   define mpp_l(z,n,d) addParameter< mpp_ArgTypeN(n) >();
109     BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ )
110 #   undef mpp_l
111 }
112
113 // ////////////////////////////////////////////////////////////////////////
114 #elif BOOST_PP_ITERATION_FLAGS()==3 // ////////////////////////////////////
115 // ////////////////////////////////////////////////////////////////////////
116
117 // non-inline template implementation (.ct)
118
119 template <class FunctionTraits>
120 prefix_ void senf::console::ParsedCommandOverload<FunctionTraits, BOOST_PP_ITERATION() >::
121 v_execute(std::ostream & os, ParseCommandInfo const & command)
122     const
123 {
124     if ( command.arguments().size() > BOOST_PP_ITERATION()
125          || (command.arguments().size() < BOOST_PP_ITERATION()
126              && ! arg( BOOST_PP_ITERATION()-1 ).hasDefault) )
127         throw SyntaxErrorException("invalid number of arguments");
128
129     // First define local variables argN for the parameters. The variables are initialized to their
130     // default values
131 #   define mpp_l(z,n,d) mpp_ArgTypeN(n) mpp_ArgN(n) (arg< mpp_ArgTypeN(n) >( n ).defaultValue);
132     BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ ) 
133 #   undef mpp_l
134     
135     ParseCommandInfo::argument_iterator i (command.arguments().begin());
136     ParseCommandInfo::argument_iterator const i_end (command.arguments().end());
137
138     // Now parse the arguments which are provided leaving the trailing arguments at their default
139     // value. We have already checked above, whether those default values are valid. Be aware, that
140     // the following cases do NOT have 'break' statements !
141
142     switch (BOOST_PP_ITERATION() - command.arguments().size()) {
143
144 #   define mpp_l(z,n,d)                                                                           \
145         case n :                                                                                  \
146             detail::ParameterTraits< mpp_ArgTypeN(n) >::parse( *(i++), mpp_ArgN(n) );
147     BOOST_PP_REPEAT( BOOST_PP_ITERATION(), mpp_l, _ ) 
148 #   undef mpp_l
149
150     default : // This happens, if ALL arguments are defaulted
151         ;
152     }
153
154     // Now call the function binding the arguments to the values parsed above. callAndWrite is
155     // specialized to ignore a 'void' return value but automatically write all other values to the
156     // output stream.
157     detail::ReturnValueTraits<typename traits::result_type>::callAndWrite(
158         boost::bind(function_, boost::ref(os)
159                     mpp_TrailingArgs()),
160         os );
161 }
162
163 // ////////////////////////////////////////////////////////////////////////
164 #elif BOOST_PP_ITERATION_FLAGS()==4 // ////////////////////////////////////
165 // ////////////////////////////////////////////////////////////////////////
166
167 // CreateParsedCommandOverload
168
169 template <class Traits>
170 struct CreateParsedCommandOverload<Traits, true, BOOST_PP_ITERATION()>
171 {
172     typedef Traits traits;
173
174     template <class Function>
175     static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
176         {
177             return senf::console::ParsedCommandOverload<traits>::create(
178                 boost::bind(fn  mpp_TrailingBindArgs()) );
179         }
180
181 };
182
183 // ////////////////////////////////////////////////////////////////////////
184 #endif // /////////////////////////////////////////////////////////////////
185 // ////////////////////////////////////////////////////////////////////////
186 // Undefine local Macros
187
188 #undef mpp_TrailingArgs
189 #undef mpp_Args_
190
191 #undef mpp_TrailingArgTypes
192 #undef mpp_ArgTypes_
193
194 #undef mpp_ArgN
195 #undef mpp_ArgTypeN
196
197 // ////////////////////////////////////////////////////////////////////////
198 #endif // /////////////////////////////////////////////////////////////////
199 // ///////////////////////////mpp.e////////////////////////////////////////
200
201 \f
202 // Local Variables:
203 // mode: c++
204 // fill-column: 100
205 // comment-column: 40
206 // c-file-style: "senf"
207 // indent-tabs-mode: nil
208 // ispell-local-dictionary: "american"
209 // compile-command: "scons -u test"
210 // End: