e37906c81b4a5a91fa8d2e5b7b4d9517defd406e
[senf.git] / Console / ParsedCommand.cti
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 inline template implementation */
25
26 #include "ParsedCommand.ih"
27
28 // Custom includes
29 #include "../Utils/membind.hh"
30 #include <boost/format.hpp>
31 #include <boost/parameter/binding.hpp>
32
33 #define prefix_ inline
34 ///////////////////////////////cti.p///////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::ArgumentInfoBase
38
39 prefix_ senf::console::detail::ArgumentInfoBase::ArgumentInfoBase(std::string const & type_)
40     : type (type_), name (), hasDefault (false)
41 {}
42
43 ///////////////////////////////////////////////////////////////////////////
44 // senf::console::detail::ArgumentInfo<ParameterType>
45
46 template <class ParameterType>
47 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
48 senf::console::detail::ArgumentInfo<ParameterType>::create()
49 {
50     return ptr(new ArgumentInfo());
51 }
52
53 template <class ParameterType>
54 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
55     : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description() ),
56       defaultValue ()
57 {}
58
59 template <class ParameterType>
60 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
61     const
62 {
63     return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
64 }
65
66 ///////////////////////////////////////////////////////////////////////////
67 // senf::console::ParsedCommandOverloadBase
68
69 template <class Type>
70 prefix_ senf::console::detail::ArgumentInfo<Type> &
71 senf::console::ParsedCommandOverloadBase::arg(unsigned n)
72     const
73 {
74     return dynamic_cast<detail::ArgumentInfo<Type> &>(arg(n));
75 }
76
77 template <class Type>
78 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
79 {
80     parameters_.push_back(detail::ArgumentInfo<Type>::create());
81 }
82
83 ///////////////////////////////////////////////////////////////////////////
84 // senf::console::ParsedCommandOverload<FunctionTraits,n>
85
86 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                       \
87                                          SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp),   \
88                                          2))
89 #include BOOST_PP_ITERATE()
90
91 ///////////////////////////////////////////////////////////////////////////
92 // senf::console::ParsedCommandAttributorBase
93
94 template <class Type>
95 prefix_ void senf::console::ParsedCommandAttributorBase::defaultValue(Type const & value)
96     const
97 {
98     overload().arg<Type>(index_).defaultValue = value;
99     overload().arg(index_).hasDefault = true;
100 }
101
102 ///////////////////////////////////////////////////////////////////////////
103 // senf::console::ParsedCommandAttributor<Overload>
104
105 template <class Overload>
106 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
107     const
108 {
109     return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
110 }
111
112 template <class Overload>
113 prefix_
114 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
115                                                                           unsigned index)
116     : ParsedCommandAttributorBase (overload, index)
117 {}
118
119 ///////////////////////////////////////////////////////////////////////////
120 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
121
122 template <class Overload, class Self>
123 prefix_ Self
124 senf::console::ParsedArgumentAttributorBase<Overload,Self>::doc(std::string const & doc)
125     const
126 {
127     this->ParsedCommandAttributorBase::nodeDoc(doc);
128     return static_cast<Self const &>(*this);
129 }
130
131 template <class Overload, class Self>
132 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self>::
133 overloadDoc(std::string const & doc)
134     const
135 {
136     this->ParsedCommandAttributorBase::overloadDoc(doc);
137     return static_cast<Self const &>(*this);
138 }
139
140 template <class Overload, class Self>
141 prefix_
142 senf::console::ParsedArgumentAttributorBase<Overload,Self>::
143 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
144     : ParsedCommandAttributor<Overload> (overload, index)
145 {}
146
147 ///////////////////////////////////////////////////////////////////////////
148 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
149
150 template <class Overload, unsigned index, bool flag>
151 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
152 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
153     const
154 {
155     return next();
156 }
157
158 template <class Overload, unsigned index, bool flag>
159 template <class ArgumentPack>
160 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
161 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
162 argInfo(ArgumentPack const & args)
163     const
164 {
165 #   define HaveArg(tag) boost::is_same< \
166         typename boost::parameter::binding<ArgumentPack, tag>::type, void >()
167
168     argInfo( kw::name,          args, HaveArg(kw::type::name)          );
169     argInfo( kw::description,   args, HaveArg(kw::type::description)   );
170     argInfo( kw::default_value, args, HaveArg(kw::type::default_value) );
171
172     return next();
173
174 #   undef HaveArg
175 }
176
177 template <class Overload, unsigned index, bool flag>
178 template <class Kw, class ArgumentPack>
179 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
180 argInfo(Kw const &, ArgumentPack const &, boost::mpl::true_)
181     const
182 {}
183
184 template <class Overload, unsigned index, bool flag>
185 template <class ArgumentPack>
186 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
187 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
188         boost::mpl::false_)
189     const
190 {
191     this->argName(args[kw::name]);
192 }
193
194 template <class Overload, unsigned index, bool flag>
195 template <class ArgumentPack>
196 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
197 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
198         boost::mpl::false_)
199     const
200 {
201     this->argDoc(args[kw::description]);
202 }
203
204 template <class Overload, unsigned index, bool flag>
205 template <class ArgumentPack>
206 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
207 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
208         boost::mpl::false_)
209     const
210 {
211     this->defaultValue(args[kw::default_value]);
212 }
213
214 template <class Overload, unsigned index, bool flag>
215 prefix_
216 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
217 ParsedArgumentAttributor(Overload & overload)
218     : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
219 {}
220
221 template <class Overload, unsigned index, bool flag>
222 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
223 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
224     const
225 {
226     return ParsedArgumentAttributor<Overload, index+1>(this->overload());
227 }
228
229 template <class Overload, unsigned index, bool flag>
230 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
231 defaultValue(value_type const & value)
232     const
233 {
234     ParsedCommandAttributorBase::defaultValue<arg_type>(value);
235 }
236
237 ///////////////////////////////////////////////////////////////////////////
238 // senf::console::ParsedArgumentAttributor<Overload, index, false>
239
240 template <class Overload, unsigned index>
241 prefix_
242 senf::console::ParsedArgumentAttributor<Overload, index, false>::
243 ParsedArgumentAttributor(Overload & overload)
244     : ParsedArgumentAttributorBase< Overload, 
245                                      ParsedArgumentAttributor<Overload, index, false> > (overload, index)
246 {}
247
248 ///////////////////////////////////////////////////////////////////////////
249 // namespace members
250
251 namespace {
252
253     // What is THIS about ??
254
255     // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
256     // this argument is given, then the function will be called with the console output stream as
257     // it's first argument.
258     //
259     // This is implemented in the following way: ParsedCommandOverload (the class responsible for
260     // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
261     // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
262     // function will be directly passed to ParsedCommandOverload.
263     //
264     // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
265     // the create member will use boost::bind to DROP the first argument.
266     
267     template <class Traits, bool ignoreOneArg, unsigned arity=Traits::arity>
268     struct CreateParsedCommandOverload
269     {};
270
271     template <class Traits, unsigned arity>
272     struct CreateParsedCommandOverload<Traits, false, arity>
273     {
274         typedef Traits traits;
275         
276         template <class Function>
277         static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn) 
278             { return senf::console::ParsedCommandOverload<traits>::create(fn); };
279     };
280
281 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
282                                             SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
283                                             4))
284 #   include BOOST_PP_ITERATE()
285
286 }
287
288 template <class Function>
289 prefix_ senf::console::ParsedArgumentAttributor<
290     senf::console::ParsedCommandOverload<
291         typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
292 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
293                                      Function fn, int)
294 {
295     OverloadedCommandNode & cmdNode (
296         node.hasChild(name) 
297         ? dynamic_cast<OverloadedCommandNode &>(node(name))
298         : node.add(name, OverloadedCommandNode::create()) );
299
300     typedef detail::ParsedCommandTraits<Function> CmdTraits;
301     typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
302     typedef ParsedArgumentAttributor<Overload> Attributor;
303
304     return Attributor(
305         cmdNode.add( CreateParsedCommandOverload<
306                          typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) );
307 }
308
309 template <class Owner, class Function>
310 prefix_ senf::console::ParsedArgumentAttributor<
311     senf::console::ParsedCommandOverload<
312         typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
313 senf::console::
314 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
315                       Function fn, int,
316                       typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
317 {
318     OverloadedCommandNode & cmdNode (
319         node.hasChild(name) 
320         ? dynamic_cast<OverloadedCommandNode &>(node(name))
321         : node.add(name, OverloadedCommandNode::create()) );
322
323     typedef detail::ParsedCommandTraits<Function> CmdTraits;
324     typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
325     typedef ParsedArgumentAttributor<Overload> Attributor;
326
327     return Attributor(
328         cmdNode.add( CreateParsedCommandOverload<
329                          typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(
330                              senf::membind(fn,&owner)) ) );
331 }
332
333 ///////////////////////////////////////////////////////////////////////////
334 // senf::console::detail::CheckVoidReturn<Type>
335
336 template <class Type>
337 template <class Fn>
338 prefix_ void senf::console::detail::CheckVoidReturn<Type>::call(Fn fn, std::ostream & os)
339 {
340     ReturnValueTraits<Type>::format(fn(),os);
341     os << "\n";
342 }
343
344 template <class Fn>
345 prefix_ void senf::console::detail::CheckVoidReturn<void>::call(Fn fn, std::ostream & os)
346 {
347     fn();
348 }
349
350 ///////////////////////////////cti.e///////////////////////////////////////
351 #undef prefix_
352
353 \f
354 // Local Variables:
355 // mode: c++
356 // fill-column: 100
357 // comment-column: 40
358 // c-file-style: "senf"
359 // indent-tabs-mode: nil
360 // ispell-local-dictionary: "american"
361 // compile-command: "scons -u test"
362 // End: