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