Console: Implement custom return-value formatter support
[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 "../Utils/parameter.hh"
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 template <class Type, class Fn>
103 prefix_ void senf::console::ParsedCommandAttributorBase::parser(Fn fn)
104     const
105 {
106     overload().arg<Type>(index_).parser = fn;
107 }
108
109 ///////////////////////////////////////////////////////////////////////////
110 // senf::console::ParsedCommandAttributor<Overload>
111
112 template <class Overload>
113 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
114     const
115 {
116     return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
117 }
118
119 template <class Overload>
120 prefix_
121 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
122                                                                           unsigned index)
123     : ParsedCommandAttributorBase (overload, index)
124 {}
125
126 ///////////////////////////////////////////////////////////////////////////
127 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
128
129 template <class Overload, class Self, class ReturnType>
130 prefix_ Self
131 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
132     const
133 {
134     this->ParsedCommandAttributorBase::nodeDoc(doc);
135     return static_cast<Self const &>(*this);
136 }
137
138 template <class Overload, class Self, class ReturnType>
139 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
140 overloadDoc(std::string const & doc)
141     const
142 {
143     this->ParsedCommandAttributorBase::overloadDoc(doc);
144     return static_cast<Self const &>(*this);
145 }
146
147 template <class Overload, class Self, class ReturnType>
148 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
149 formatter(typename Overload::Formatter f)
150     const
151 {
152     this->overload().formatter(f);
153     return static_cast<Self const &>(*this);
154 }
155
156 template <class Overload, class Self, class ReturnType>
157 prefix_
158 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
159 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
160     : ParsedCommandAttributor<Overload> (overload, index)
161 {}
162
163 template <class Overload, class Self>
164 prefix_ Self
165 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
166     const
167 {
168     this->ParsedCommandAttributorBase::nodeDoc(doc);
169     return static_cast<Self const &>(*this);
170 }
171
172 template <class Overload, class Self>
173 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
174 overloadDoc(std::string const & doc)
175     const
176 {
177     this->ParsedCommandAttributorBase::overloadDoc(doc);
178     return static_cast<Self const &>(*this);
179 }
180
181 template <class Overload, class Self>
182 prefix_
183 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
184 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
185     : ParsedCommandAttributor<Overload> (overload, index)
186 {}
187
188 ///////////////////////////////////////////////////////////////////////////
189 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
190
191 template <class Overload, unsigned index, bool flag>
192 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
193 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
194     const
195 {
196     return next();
197 }
198
199 template <class Overload, unsigned index, bool flag>
200 template <class ArgumentPack>
201 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
202 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
203 argInfo(ArgumentPack const & args)
204     const
205 {
206 #   define ProcessArg(tag) \
207         argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
208
209     ProcessArg(name);
210     ProcessArg(description);
211     ProcessArg(default_value);
212     ProcessArg(type_name);
213     ProcessArg(default_doc);
214     ProcessArg(parser);
215
216     return next();
217
218 #   undef HaveArg
219 }
220
221 template <class Overload, unsigned index, bool flag>
222 template <class Kw, class ArgumentPack>
223 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
224 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
225     const
226 {}
227
228 template <class Overload, unsigned index, bool flag>
229 template <class ArgumentPack>
230 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
231 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
232         boost::mpl::true_)
233     const
234 {
235     this->argName(args[kw::name]);
236 }
237
238 template <class Overload, unsigned index, bool flag>
239 template <class ArgumentPack>
240 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
241 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
242         boost::mpl::true_)
243     const
244 {
245     this->argDoc(args[kw::description]);
246 }
247
248 template <class Overload, unsigned index, bool flag>
249 template <class ArgumentPack>
250 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
251 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
252         boost::mpl::true_)
253     const
254 {
255     this->defaultValue(args[kw::default_value]);
256 }
257
258 template <class Overload, unsigned index, bool flag>
259 template <class ArgumentPack>
260 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
261 argInfo(boost::parameter::keyword<kw::type::type_name> const &, ArgumentPack const & args,
262         boost::mpl::true_)
263     const
264 {
265     this->typeName(args[kw::type_name]);
266 }
267
268 template <class Overload, unsigned index, bool flag>
269 template <class ArgumentPack>
270 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
271 argInfo(boost::parameter::keyword<kw::type::default_doc> const &, ArgumentPack const & args,
272         boost::mpl::true_)
273     const
274 {
275     BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
276     this->defaultDoc(args[kw::default_doc]);
277 }
278
279 template <class Overload, unsigned index, bool flag>
280 template <class ArgumentPack>
281 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
282 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
283         boost::mpl::true_)
284     const
285 {
286     this->parser(args[kw::parser]);
287 }
288
289 template <class Overload, unsigned index, bool flag>
290 prefix_
291 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 ParsedArgumentAttributor(Overload & overload)
293     : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
294 {}
295
296 template <class Overload, unsigned index, bool flag>
297 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
298 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
299     const
300 {
301     return ParsedArgumentAttributor<Overload, index+1>(this->overload());
302 }
303
304 template <class Overload, unsigned index, bool flag>
305 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
306 defaultValue(value_type const & value)
307     const
308 {
309     ParsedCommandAttributorBase::defaultValue<value_type>(value);
310 }
311
312 template <class Overload, unsigned index, bool flag>
313 template <class Fn>
314 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
315     const
316 {
317     ParsedCommandAttributorBase::parser<value_type>(fn);
318 }
319
320 ///////////////////////////////////////////////////////////////////////////
321 // senf::console::ParsedArgumentAttributor<Overload, index, false>
322
323 template <class Overload, unsigned index>
324 prefix_
325 senf::console::ParsedArgumentAttributor<Overload, index, false>::
326 ParsedArgumentAttributor(Overload & overload)
327     : ParsedArgumentAttributorBase< Overload, 
328                                      ParsedArgumentAttributor<Overload, index, false> > (overload, index)
329 {}
330
331 ///////////////////////////////////////////////////////////////////////////
332 // namespace members
333
334 namespace {
335
336     // What is THIS about ??
337
338     // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
339     // this argument is given, then the function will be called with the console output stream as
340     // it's first argument.
341     //
342     // This is implemented in the following way: ParsedCommandOverload (the class responsible for
343     // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
344     // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
345     // function will be directly passed to ParsedCommandOverload.
346     //
347     // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
348     // the create member will use boost::bind to DROP the first argument.
349     
350     template <class Traits, bool ignoreOneArg, unsigned arity=Traits::arity>
351     struct CreateParsedCommandOverload
352     {};
353
354     template <class Traits, unsigned arity>
355     struct CreateParsedCommandOverload<Traits, false, arity>
356     {
357         typedef Traits traits;
358         
359         template <class Function>
360         static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn) 
361             { return senf::console::ParsedCommandOverload<traits>::create(fn); };
362     };
363
364 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
365                                             SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
366                                             4))
367 #   include BOOST_PP_ITERATE()
368
369 }
370
371 template <class Function>
372 prefix_ senf::console::ParsedArgumentAttributor<
373     senf::console::ParsedCommandOverload<
374         typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
375 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
376                                      Function fn, int)
377 {
378     OverloadedCommandNode & cmdNode (
379         node.hasChild(name) 
380         ? dynamic_cast<OverloadedCommandNode &>(node(name))
381         : node.add(name, OverloadedCommandNode::create()) );
382
383     typedef detail::ParsedCommandTraits<Function> CmdTraits;
384     typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
385     typedef ParsedArgumentAttributor<Overload> Attributor;
386
387     return Attributor(
388         cmdNode.add( CreateParsedCommandOverload<
389                          typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) );
390 }
391
392 template <class Owner, class Function>
393 prefix_ senf::console::ParsedArgumentAttributor<
394     senf::console::ParsedCommandOverload<
395         typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
396 senf::console::
397 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
398                       Function fn, int,
399                       typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
400 {
401     OverloadedCommandNode & cmdNode (
402         node.hasChild(name) 
403         ? dynamic_cast<OverloadedCommandNode &>(node(name))
404         : node.add(name, OverloadedCommandNode::create()) );
405
406     typedef detail::ParsedCommandTraits<Function> CmdTraits;
407     typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
408     typedef ParsedArgumentAttributor<Overload> Attributor;
409
410     return Attributor(
411         cmdNode.add( CreateParsedCommandOverload<
412                          typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(
413                              senf::membind(fn,&owner)) ) );
414 }
415
416 ///////////////////////////////cti.e///////////////////////////////////////
417 #undef prefix_
418
419 \f
420 // Local Variables:
421 // mode: c++
422 // fill-column: 100
423 // comment-column: 40
424 // c-file-style: "senf"
425 // indent-tabs-mode: nil
426 // ispell-local-dictionary: "american"
427 // compile-command: "scons -u test"
428 // End: