switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / ParsedCommand.cti
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ParsedCommand inline template implementation */
30
31 #include "ParsedCommand.ih"
32
33 // Custom includes
34 #include <senf/Utils/membind.hh>
35 #include <boost/format.hpp>
36 #include <senf/Utils/parameter.hh>
37
38 #define prefix_ inline
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 // senf::console::detail::ArgumentInfo<ParameterType>
43
44 template <class ParameterType>
45 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
46 senf::console::detail::ArgumentInfo<ParameterType>::create()
47 {
48     return ptr(new ArgumentInfo());
49 }
50
51 template <class ParameterType>
52 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
53     : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description(),
54                          ArgumentTraits<ParameterType>::singleToken ),
55       defaultValue ()
56 {}
57
58 template <class ParameterType>
59 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
60     const
61 {
62     return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
63 }
64
65 //-/////////////////////////////////////////////////////////////////////////////////////////////////
66 // senf::console::ParsedCommandOverloadBase
67
68 template <class Type>
69 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
70 {
71     parameters_.push_back(detail::ArgumentInfo<Type>::create());
72 }
73
74 //-/////////////////////////////////////////////////////////////////////////////////////////////////
75 // senf::console::ParsedCommandOverload<FunctionTraits,n>
76
77 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                       \
78                                          SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp),   \
79                                          2))
80 #include BOOST_PP_ITERATE()
81
82 //-/////////////////////////////////////////////////////////////////////////////////////////////////
83 // senf::console::ParsedCommandAttributor<Overload>
84
85 template <class Overload>
86 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
87     const
88 {
89     return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
90 }
91
92 template <class Overload>
93 prefix_ senf::console::ParsedCommandAttributor<Overload>::
94 ParsedCommandAttributor(typename Overload::ptr overload, unsigned index)
95     : ParsedCommandAttributorBase (overload, index)
96 {}
97
98 template <class Overload>
99 prefix_ senf::console::ParsedCommandAttributor<Overload>::
100 ParsedCommandAttributor(ParsedCommandAttributorBase const & other, unsigned index)
101     : ParsedCommandAttributorBase (other, index)
102 {}
103
104 //-/////////////////////////////////////////////////////////////////////////////////////////////////
105 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
106
107 template <class Overload, class Self, class ReturnType>
108 prefix_ Self
109 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
110 {
111     this->ParsedCommandAttributorBase::nodeDoc(doc);
112     return static_cast<Self const &>(*this);
113 }
114
115 template <class Overload, class Self, class ReturnType>
116 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
117 shortdoc(std::string const & doc)
118 {
119     this->ParsedCommandAttributorBase::shortDoc(doc);
120     return static_cast<Self const &>(*this);
121 }
122
123 template <class Overload, class Self, class ReturnType>
124 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
125 overloadDoc(std::string const & doc)
126 {
127     this->ParsedCommandAttributorBase::overloadDoc(doc);
128     return static_cast<Self const &>(*this);
129 }
130
131 template <class Overload, class Self, class ReturnType>
132 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
133 formatter(typename Overload::Formatter f)
134 {
135     this->overload().formatter(f);
136     return static_cast<Self const &>(*this);
137 }
138
139 template <class Overload, class Self, class ReturnType>
140 prefix_
141 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
142 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
143     : ParsedCommandAttributor<Overload> (overload, index)
144 {}
145
146 template <class Overload, class Self, class ReturnType>
147 prefix_
148 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
149 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
150     : ParsedCommandAttributor<Overload> (other, index)
151 {}
152
153 template <class Overload, class Self>
154 prefix_ Self
155 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
156 {
157     this->ParsedCommandAttributorBase::nodeDoc(doc);
158     return static_cast<Self const &>(*this);
159 }
160
161 template <class Overload, class Self>
162 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
163 shortdoc(std::string const & doc)
164 {
165     this->ParsedCommandAttributorBase::shortDoc(doc);
166     return static_cast<Self const &>(*this);
167 }
168
169 template <class Overload, class Self>
170 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
171 overloadDoc(std::string const & doc)
172 {
173     this->ParsedCommandAttributorBase::overloadDoc(doc);
174     return static_cast<Self const &>(*this);
175 }
176
177 template <class Overload, class Self>
178 prefix_
179 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
180 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
181     : ParsedCommandAttributor<Overload> (overload, index)
182 {}
183
184 template <class Overload, class Self>
185 prefix_
186 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
187 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
188     : ParsedCommandAttributor<Overload> (other, index)
189 {}
190
191 //-/////////////////////////////////////////////////////////////////////////////////////////////////
192 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
193
194 template <class Overload, unsigned index, bool flag>
195 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
196 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
197     const
198 {
199     return next();
200 }
201
202 template <class Overload, unsigned index, bool flag>
203 template <class ArgumentPack>
204 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
205 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
206 argInfo(ArgumentPack const & args)
207 {
208 #   define ProcessArg(tag) \
209         argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
210
211     ProcessArg(name);
212     ProcessArg(description);
213     ProcessArg(default_value);
214     ProcessArg(type_name);
215     ProcessArg(default_doc);
216     ProcessArg(parser);
217
218     return next();
219
220 #   undef ProcessArg
221 }
222
223 template <class Overload, unsigned index, bool flag>
224 template <class Kw, class ArgumentPack>
225 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
226 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
227 {}
228
229 template <class Overload, unsigned index, bool flag>
230 template <class ArgumentPack>
231 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
232 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
233         boost::mpl::true_)
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 {
244     this->argDoc(args[kw::description]);
245 }
246
247 template <class Overload, unsigned index, bool flag>
248 template <class ArgumentPack>
249 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
250 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
251         boost::mpl::true_)
252 {
253     this->defaultValue(args[kw::default_value]);
254 }
255
256 template <class Overload, unsigned index, bool flag>
257 template <class ArgumentPack>
258 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
259 argInfo(boost::parameter::keyword<kw::type::type_name> const &, ArgumentPack const & args,
260         boost::mpl::true_)
261 {
262     this->typeName(args[kw::type_name]);
263 }
264
265 template <class Overload, unsigned index, bool flag>
266 template <class ArgumentPack>
267 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
268 argInfo(boost::parameter::keyword<kw::type::default_doc> const &, ArgumentPack const & args,
269         boost::mpl::true_)
270 {
271     BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
272     this->defaultDoc(args[kw::default_doc]);
273 }
274
275 template <class Overload, unsigned index, bool flag>
276 template <class ArgumentPack>
277 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
278 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
279         boost::mpl::true_)
280 {
281     this->parser(args[kw::parser]);
282 }
283
284 template <class Overload, unsigned index, bool flag>
285 prefix_ senf::console::ParsedArgumentAttributor<Overload,index,flag>::
286 ParsedArgumentAttributor(typename Overload::ptr overload)
287 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
288 {}
289
290 template <class Overload, unsigned index, bool flag>
291 prefix_ senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
293     : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, 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);
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 {
308     this->overload().arg<index>().defaultValue = value;
309     this->overload().arg(index).hasDefault = true;
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 {
316     this->overload().arg<index>().parser = fn;
317 }
318
319 //-/////////////////////////////////////////////////////////////////////////////////////////////////
320 // senf::console::ParsedArgumentAttributor<Overload, index, false>
321
322 template <class Overload, unsigned index>
323 prefix_
324 senf::console::ParsedArgumentAttributor<Overload, index, false>::
325 ParsedArgumentAttributor(typename Overload::ptr overload)
326     : ParsedArgumentAttributorBase<
327           Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
328 {}
329
330 template <class Overload, unsigned index>
331 prefix_
332 senf::console::ParsedArgumentAttributor<Overload, index, false>::
333 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
334     : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, index)
335 {}
336
337 //-/////////////////////////////////////////////////////////////////////////////////////////////////
338 // namespace members
339
340 namespace senf {
341 namespace console {
342 namespace detail {
343
344 #ifndef DOXYGEN
345
346     struct ParsedCommandAddNodeAccess
347     {
348         template <class Attributor, class NodePtr>
349         static Attributor attributor(NodePtr ptr)
350             { return Attributor(ptr); }
351     };
352
353     template <class Signature, class Fn>
354     typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
355     addOverloadedCommandNode(Fn fn)
356     {
357         typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
358         typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
359         typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
360
361         return detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
362             CreateParsedCommandOverload<CmdTraits>::create(fn));
363     }
364
365 #endif
366
367 }}}
368
369 //-/////////////////////////////////////////////////////////////////////////////////////////////////
370
371 template <class Signature>
372 prefix_ senf::console::SimpleOverloadAttributor
373 senf::console::factory::
374 Command(boost::function<Signature> fn,
375         typename boost::enable_if_c<detail::ParsedCommandTraits<Signature>::is_simple>::type *)
376 {
377     return SimpleOverloadAttributor(fn);
378 }
379
380 template <class Function>
381 prefix_ senf::console::SimpleOverloadAttributor
382 senf::console::factory::
383 Command(Function fn,
384         typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_simple>::type *)
385 {
386     return SimpleOverloadAttributor(fn);
387 }
388
389 template <class Owner, class Member>
390 prefix_ senf::console::SimpleOverloadAttributor
391 senf::console::factory::
392 Command(Member memfn, Owner * owner,
393         typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *,
394         typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type *)
395 {
396     return SimpleOverloadAttributor(senf::membind(memfn,owner));
397 }
398
399 template <class Owner, class Member>
400 prefix_ senf::console::SimpleOverloadAttributor
401 senf::console::factory::
402 Command(Member memfn, Owner const * owner,
403         typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *,
404         typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type *)
405 {
406     return SimpleOverloadAttributor(senf::membind(memfn,owner));
407 }
408
409 template <class CastTo, class Signature>
410 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
411 senf::console::factory::
412 Command(boost::function<Signature> fn)
413 {
414     return detail::addOverloadedCommandNode<CastTo>(fn);
415 }
416
417 template <class CastTo, class Function>
418 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
419 senf::console::factory::
420 Command(Function fn,
421         typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
422         typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
423 {
424     return detail::addOverloadedCommandNode<CastTo>(fn);
425 }
426
427 template <class Signature>
428 prefix_ typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
429 senf::console::factory::
430 Command(boost::function<Signature> fn)
431 {
432     return detail::addOverloadedCommandNode<Signature>(fn);
433 }
434
435 template <class Function>
436 prefix_ typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
437 senf::console::factory::
438 Command(Function fn,
439         typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
440         typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
441 {
442     return detail::addOverloadedCommandNode<Function>(fn);
443 }
444
445 template <class Owner, class Member>
446 prefix_ typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
447 senf::console::factory::
448 Command(Member memfn, Owner * owner,
449              typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
450 {
451     return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
452 }
453
454 template <class Owner, class Member>
455 prefix_ typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
456 senf::console::factory::
457 Command(Member memfn, Owner const * owner,
458              typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
459 {
460     return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
461 }
462
463 template <class CastTo, class Owner, class Member>
464 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
465 senf::console::factory::
466 Command(Member memfn, Owner * owner,
467              typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
468 {
469     return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
470 }
471
472 template <class CastTo, class Owner, class Member>
473 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
474 senf::console::factory::
475 Command(Member memfn, Owner const * owner,
476              typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
477 {
478     return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
479 }
480
481 //-/////////////////////////////////////////////////////////////////////////////////////////////////
482 #undef prefix_
483
484 \f
485 // Local Variables:
486 // mode: c++
487 // fill-column: 100
488 // comment-column: 40
489 // c-file-style: "senf"
490 // indent-tabs-mode: nil
491 // ispell-local-dictionary: "american"
492 // compile-command: "scons -u test"
493 // End: