4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief ParsedCommand inline template implementation */
26 #include "ParsedCommand.ih"
29 #include <senf/Utils/membind.hh>
30 #include <boost/format.hpp>
31 #include <senf/Utils/parameter.hh>
33 #define prefix_ inline
34 ///////////////////////////////cti.p///////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::ArgumentInfo<ParameterType>
39 template <class ParameterType>
40 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
41 senf::console::detail::ArgumentInfo<ParameterType>::create()
43 return ptr(new ArgumentInfo());
46 template <class ParameterType>
47 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
48 : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description(),
49 ArgumentTraits<ParameterType>::singleToken ),
53 template <class ParameterType>
54 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
57 return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
60 ///////////////////////////////////////////////////////////////////////////
61 // senf::console::ParsedCommandOverloadBase
64 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
66 parameters_.push_back(detail::ArgumentInfo<Type>::create());
69 ///////////////////////////////////////////////////////////////////////////
70 // senf::console::ParsedCommandOverload<FunctionTraits,n>
72 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
73 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
75 #include BOOST_PP_ITERATE()
77 ///////////////////////////////////////////////////////////////////////////
78 // senf::console::ParsedCommandAttributor<Overload>
80 template <class Overload>
81 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
84 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
87 template <class Overload>
88 prefix_ senf::console::ParsedCommandAttributor<Overload>::
89 ParsedCommandAttributor(typename Overload::ptr overload, unsigned index)
90 : ParsedCommandAttributorBase (overload, index)
93 template <class Overload>
94 prefix_ senf::console::ParsedCommandAttributor<Overload>::
95 ParsedCommandAttributor(ParsedCommandAttributorBase const & other, unsigned index)
96 : ParsedCommandAttributorBase (other, index)
99 ///////////////////////////////////////////////////////////////////////////
100 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
102 template <class Overload, class Self, class ReturnType>
104 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
106 this->ParsedCommandAttributorBase::nodeDoc(doc);
107 return static_cast<Self const &>(*this);
110 template <class Overload, class Self, class ReturnType>
111 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
112 shortdoc(std::string const & doc)
114 this->ParsedCommandAttributorBase::shortDoc(doc);
115 return static_cast<Self const &>(*this);
118 template <class Overload, class Self, class ReturnType>
119 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
120 overloadDoc(std::string const & doc)
122 this->ParsedCommandAttributorBase::overloadDoc(doc);
123 return static_cast<Self const &>(*this);
126 template <class Overload, class Self, class ReturnType>
127 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
128 formatter(typename Overload::Formatter f)
130 this->overload().formatter(f);
131 return static_cast<Self const &>(*this);
134 template <class Overload, class Self, class ReturnType>
136 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
137 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
138 : ParsedCommandAttributor<Overload> (overload, index)
141 template <class Overload, class Self, class ReturnType>
143 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
144 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
145 : ParsedCommandAttributor<Overload> (other, index)
148 template <class Overload, class Self>
150 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
152 this->ParsedCommandAttributorBase::nodeDoc(doc);
153 return static_cast<Self const &>(*this);
156 template <class Overload, class Self>
157 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
158 shortdoc(std::string const & doc)
160 this->ParsedCommandAttributorBase::shortDoc(doc);
161 return static_cast<Self const &>(*this);
164 template <class Overload, class Self>
165 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
166 overloadDoc(std::string const & doc)
168 this->ParsedCommandAttributorBase::overloadDoc(doc);
169 return static_cast<Self const &>(*this);
172 template <class Overload, class Self>
174 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
175 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
176 : ParsedCommandAttributor<Overload> (overload, index)
179 template <class Overload, class Self>
181 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
182 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
183 : ParsedCommandAttributor<Overload> (other, index)
186 ///////////////////////////////////////////////////////////////////////////
187 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
189 template <class Overload, unsigned index, bool flag>
190 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
191 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
197 template <class Overload, unsigned index, bool flag>
198 template <class ArgumentPack>
199 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
200 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
201 argInfo(ArgumentPack const & args)
203 # define ProcessArg(tag) \
204 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
207 ProcessArg(description);
208 ProcessArg(default_value);
209 ProcessArg(type_name);
210 ProcessArg(default_doc);
218 template <class Overload, unsigned index, bool flag>
219 template <class Kw, class ArgumentPack>
220 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
221 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
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::name> const &, ArgumentPack const & args,
230 this->argName(args[kw::name]);
233 template <class Overload, unsigned index, bool flag>
234 template <class ArgumentPack>
235 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
236 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
239 this->argDoc(args[kw::description]);
242 template <class Overload, unsigned index, bool flag>
243 template <class ArgumentPack>
244 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
245 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
248 this->defaultValue(args[kw::default_value]);
251 template <class Overload, unsigned index, bool flag>
252 template <class ArgumentPack>
253 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
254 argInfo(boost::parameter::keyword<kw::type::type_name> const &, ArgumentPack const & args,
257 this->typeName(args[kw::type_name]);
260 template <class Overload, unsigned index, bool flag>
261 template <class ArgumentPack>
262 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
263 argInfo(boost::parameter::keyword<kw::type::default_doc> const &, ArgumentPack const & args,
266 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
267 this->defaultDoc(args[kw::default_doc]);
270 template <class Overload, unsigned index, bool flag>
271 template <class ArgumentPack>
272 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
273 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
276 this->parser(args[kw::parser]);
279 template <class Overload, unsigned index, bool flag>
280 prefix_ senf::console::ParsedArgumentAttributor<Overload,index,flag>::
281 ParsedArgumentAttributor(typename Overload::ptr overload)
282 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
285 template <class Overload, unsigned index, bool flag>
286 prefix_ senf::console::ParsedArgumentAttributor<Overload,index,flag>::
287 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
288 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, index)
291 template <class Overload, unsigned index, bool flag>
292 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
293 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
296 return ParsedArgumentAttributor<Overload, index+1>(*this);
299 template <class Overload, unsigned index, bool flag>
300 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
301 defaultValue(value_type const & value)
303 this->overload().arg<index>().defaultValue = value;
304 this->overload().arg(index).hasDefault = true;
307 template <class Overload, unsigned index, bool flag>
309 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
311 this->overload().arg<index>().parser = fn;
314 ///////////////////////////////////////////////////////////////////////////
315 // senf::console::ParsedArgumentAttributor<Overload, index, false>
317 template <class Overload, unsigned index>
319 senf::console::ParsedArgumentAttributor<Overload, index, false>::
320 ParsedArgumentAttributor(typename Overload::ptr overload)
321 : ParsedArgumentAttributorBase<
322 Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
325 template <class Overload, unsigned index>
327 senf::console::ParsedArgumentAttributor<Overload, index, false>::
328 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
329 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, index)
332 ///////////////////////////////////////////////////////////////////////////
341 struct ParsedCommandAddNodeAccess
343 template <class Attributor, class NodePtr>
344 static Attributor attributor(NodePtr ptr)
345 { return Attributor(ptr); }
348 template <class Signature, class Fn>
349 typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
350 addOverloadedCommandNode(Fn fn)
352 typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
353 typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
354 typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
356 return detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
357 CreateParsedCommandOverload<CmdTraits>::create(fn));
364 ///////////////////////////////////////////////////////////////////////////
366 template <class Signature>
367 prefix_ senf::console::SimpleOverloadAttributor
368 senf::console::factory::
369 Command(boost::function<Signature> fn,
370 typename boost::enable_if_c<detail::ParsedCommandTraits<Signature>::is_simple>::type *)
372 return SimpleOverloadAttributor(fn);
375 template <class Function>
376 prefix_ senf::console::SimpleOverloadAttributor
377 senf::console::factory::
379 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_simple>::type *)
381 return SimpleOverloadAttributor(fn);
384 template <class Owner, class Member>
385 prefix_ senf::console::SimpleOverloadAttributor
386 senf::console::factory::
387 Command(Member memfn, Owner * owner,
388 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *,
389 typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type *)
391 return SimpleOverloadAttributor(senf::membind(memfn,owner));
394 template <class Owner, class Member>
395 prefix_ senf::console::SimpleOverloadAttributor
396 senf::console::factory::
397 Command(Member memfn, Owner const * owner,
398 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *,
399 typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type *)
401 return SimpleOverloadAttributor(senf::membind(memfn,owner));
404 template <class CastTo, class Signature>
405 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
406 senf::console::factory::
407 Command(boost::function<Signature> fn)
409 return detail::addOverloadedCommandNode<CastTo>(fn);
412 template <class CastTo, class Function>
413 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
414 senf::console::factory::
416 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
417 typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
419 return detail::addOverloadedCommandNode<CastTo>(fn);
422 template <class Signature>
423 prefix_ typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
424 senf::console::factory::
425 Command(boost::function<Signature> fn)
427 return detail::addOverloadedCommandNode<Signature>(fn);
430 template <class Function>
431 prefix_ typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
432 senf::console::factory::
434 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
435 typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
437 return detail::addOverloadedCommandNode<Function>(fn);
440 template <class Owner, class Member>
441 prefix_ typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
442 senf::console::factory::
443 Command(Member memfn, Owner * owner,
444 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
446 return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
449 template <class Owner, class Member>
450 prefix_ typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
451 senf::console::factory::
452 Command(Member memfn, Owner const * owner,
453 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
455 return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
458 template <class CastTo, class Owner, class Member>
459 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
460 senf::console::factory::
461 Command(Member memfn, Owner * owner,
462 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
464 return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
467 template <class CastTo, class Owner, class Member>
468 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
469 senf::console::factory::
470 Command(Member memfn, Owner const * owner,
471 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
473 return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
476 ///////////////////////////////cti.e///////////////////////////////////////
483 // comment-column: 40
484 // c-file-style: "senf"
485 // indent-tabs-mode: nil
486 // ispell-local-dictionary: "american"
487 // compile-command: "scons -u test"