ParsedCommand.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief ParsedCommand inline template implementation */
16 
17 #include "ParsedCommand.ih"
18 
19 // Custom includes
20 #include <boost/format.hpp>
21 #include <senf/Utils/membind.hh>
22 #include <senf/Utils/parameter.hh>
23 
24 #define prefix_ inline
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::console::detail::ArgumentInfo<ParameterType>
29 
30 template <class ParameterType>
31 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
32 senf::console::detail::ArgumentInfo<ParameterType>::create()
33 {
34  return ptr(new ArgumentInfo());
35 }
36 
37 template <class ParameterType>
38 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
39  : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description(),
40  ArgumentTraits<ParameterType>::singleToken ),
41  defaultValue ()
42 {}
43 
44 template <class ParameterType>
45 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
46  const
47 {
48  return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
49 }
50 
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::console::ParsedCommandOverloadBase
53 
54 template <class Type>
55 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
56 {
57  parameters_.push_back(detail::ArgumentInfo<Type>::create());
58 }
59 
60 //-/////////////////////////////////////////////////////////////////////////////////////////////////
61 // senf::console::ParsedCommandOverload<FunctionTraits,n>
62 
63 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
64  SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
65  2))
66 #include BOOST_PP_ITERATE()
67 
68 //-/////////////////////////////////////////////////////////////////////////////////////////////////
69 // senf::console::ParsedCommandAttributor<Overload>
70 
71 template <class Overload>
72 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
73  const
74 {
75  return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
76 }
77 
78 template <class Overload>
79 prefix_ senf::console::ParsedCommandAttributor<Overload>::
80 ParsedCommandAttributor(typename Overload::ptr overload, unsigned index)
81  : ParsedCommandAttributorBase (overload, index)
82 {}
83 
84 template <class Overload>
85 prefix_ senf::console::ParsedCommandAttributor<Overload>::
86 ParsedCommandAttributor(ParsedCommandAttributorBase const & other, unsigned index)
87  : ParsedCommandAttributorBase (other, index)
88 {}
89 
90 //-/////////////////////////////////////////////////////////////////////////////////////////////////
91 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
92 
93 template <class Overload, class Self, class ReturnType>
94 prefix_ Self
95 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
96 {
97  this->ParsedCommandAttributorBase::nodeDoc(doc);
98  return static_cast<Self const &>(*this);
99 }
100 
101 template <class Overload, class Self, class ReturnType>
102 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
103 shortdoc(std::string const & doc)
104 {
105  this->ParsedCommandAttributorBase::shortDoc(doc);
106  return static_cast<Self const &>(*this);
107 }
108 
109 template <class Overload, class Self, class ReturnType>
110 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
111 overloadDoc(std::string const & doc)
112 {
113  this->ParsedCommandAttributorBase::overloadDoc(doc);
114  return static_cast<Self const &>(*this);
115 }
116 
117 template <class Overload, class Self, class ReturnType>
118 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
119 formatter(typename Overload::Formatter f)
120 {
121  this->overload().formatter(f);
122  return static_cast<Self const &>(*this);
123 }
124 
125 template <class Overload, class Self, class ReturnType>
126 prefix_
127 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
128 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
129  : ParsedCommandAttributor<Overload> (overload, index)
130 {}
131 
132 template <class Overload, class Self, class ReturnType>
133 prefix_
134 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
135 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
136  : ParsedCommandAttributor<Overload> (other, index)
137 {}
138 
139 template <class Overload, class Self>
140 prefix_ Self
141 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
142 {
143  this->ParsedCommandAttributorBase::nodeDoc(doc);
144  return static_cast<Self const &>(*this);
145 }
146 
147 template <class Overload, class Self>
148 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
149 shortdoc(std::string const & doc)
150 {
151  this->ParsedCommandAttributorBase::shortDoc(doc);
152  return static_cast<Self const &>(*this);
153 }
154 
155 template <class Overload, class Self>
156 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
157 overloadDoc(std::string const & doc)
158 {
159  this->ParsedCommandAttributorBase::overloadDoc(doc);
160  return static_cast<Self const &>(*this);
161 }
162 
163 template <class Overload, class Self>
164 prefix_
165 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
166 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
167  : ParsedCommandAttributor<Overload> (overload, index)
168 {}
169 
170 template <class Overload, class Self>
171 prefix_
172 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
173 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
174  : ParsedCommandAttributor<Overload> (other, index)
175 {}
176 
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
178 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
179 
180 template <class Overload, unsigned index, bool flag>
181 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
182 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
183  const
184 {
185  return next();
186 }
187 
188 template <class Overload, unsigned index, bool flag>
189 template <class ArgumentPack>
190 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
191 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
192 argInfo(ArgumentPack const & args)
193 {
194 # define ProcessArg(tag) \
195  argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
196 
197  ProcessArg(name);
198  ProcessArg(description);
199  ProcessArg(default_value);
200  ProcessArg(type_name);
201  ProcessArg(default_doc);
202  ProcessArg(parser);
203 
204  return next();
205 
206 # undef ProcessArg
207 }
208 
209 template <class Overload, unsigned index, bool flag>
210 template <class Kw, class ArgumentPack>
211 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
212 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
213 {}
214 
215 template <class Overload, unsigned index, bool flag>
216 template <class ArgumentPack>
217 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
218 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
219  boost::mpl::true_)
220 {
221  this->argName(args[kw::name]);
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::description> const &, ArgumentPack const & args,
228  boost::mpl::true_)
229 {
230  this->argDoc(args[kw::description]);
231 }
232 
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::default_value> const &, ArgumentPack const & args,
237  boost::mpl::true_)
238 {
239  this->defaultValue(args[kw::default_value]);
240 }
241 
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::type_name> const &, ArgumentPack const & args,
246  boost::mpl::true_)
247 {
248  this->typeName(args[kw::type_name]);
249 }
250 
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::default_doc> const &, ArgumentPack const & args,
255  boost::mpl::true_)
256 {
257  BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
258  this->defaultDoc(args[kw::default_doc]);
259 }
260 
261 template <class Overload, unsigned index, bool flag>
262 template <class ArgumentPack>
263 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
264 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
265  boost::mpl::true_)
266 {
267  this->parser(args[kw::parser]);
268 }
269 
270 template <class Overload, unsigned index, bool flag>
271 prefix_ senf::console::ParsedArgumentAttributor<Overload,index,flag>::
272 ParsedArgumentAttributor(typename Overload::ptr overload)
273 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
274 {}
275 
276 template <class Overload, unsigned index, bool flag>
277 prefix_ senf::console::ParsedArgumentAttributor<Overload,index,flag>::
278 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
279  : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, index)
280 {}
281 
282 template <class Overload, unsigned index, bool flag>
283 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
284 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
285  const
286 {
287  return ParsedArgumentAttributor<Overload, index+1>(*this);
288 }
289 
290 template <class Overload, unsigned index, bool flag>
291 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 defaultValue(value_type const & value)
293 {
294  this->overload().template arg<index>().defaultValue = value;
295  this->overload().arg(index).hasDefault = true;
296 }
297 
298 template <class Overload, unsigned index, bool flag>
299 template <class Fn>
300 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
301 {
302  this->overload().template arg<index>().parser = fn;
303 }
304 
305 //-/////////////////////////////////////////////////////////////////////////////////////////////////
306 // senf::console::ParsedArgumentAttributor<Overload, index, false>
307 
308 template <class Overload, unsigned index>
309 prefix_
310 senf::console::ParsedArgumentAttributor<Overload, index, false>::
311 ParsedArgumentAttributor(typename Overload::ptr overload)
312  : ParsedArgumentAttributorBase<
313  Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
314 {}
315 
316 template <class Overload, unsigned index>
317 prefix_
318 senf::console::ParsedArgumentAttributor<Overload, index, false>::
319 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
320  : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, index)
321 {}
322 
323 //-/////////////////////////////////////////////////////////////////////////////////////////////////
324 // namespace members
325 
326 namespace senf {
327 namespace console {
328 namespace detail {
329 
330 #ifndef DOXYGEN
331 
332  struct ParsedCommandAddNodeAccess
333  {
334  template <class Attributor, class NodePtr>
335  static Attributor attributor(NodePtr ptr)
336  { return Attributor(ptr); }
337  };
338 
339  template <class Signature, class Fn>
340  typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
341  addOverloadedCommandNode(Fn fn)
342  {
343  typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
344  typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
345  typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
346 
347  return detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
348  CreateParsedCommandOverload<CmdTraits>::create(fn));
349  }
350 
351 #endif
352 
353 }}}
354 
355 //-/////////////////////////////////////////////////////////////////////////////////////////////////
356 
357 template <class Signature>
358 prefix_ senf::console::SimpleOverloadAttributor
359 senf::console::factory::
360 Command(boost::function<Signature> fn,
361  typename boost::enable_if_c<detail::ParsedCommandTraits<Signature>::is_simple>::type *)
362 {
363  return SimpleOverloadAttributor(fn);
364 }
365 
366 template <class Function>
367 prefix_ senf::console::SimpleOverloadAttributor
368 senf::console::factory::
369 Command(Function fn,
370  typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_simple>::type *)
371 {
372  return SimpleOverloadAttributor(fn);
373 }
374 
375 template <class Owner, class Member>
376 prefix_ senf::console::SimpleOverloadAttributor
377 senf::console::factory::
378 Command(Member memfn, Owner * owner,
379  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *,
380  typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type *)
381 {
382  return SimpleOverloadAttributor(senf::membind(memfn,owner));
383 }
384 
385 template <class Owner, class Member>
386 prefix_ senf::console::SimpleOverloadAttributor
387 senf::console::factory::
388 Command(Member memfn, Owner const * owner,
389  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *,
390  typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type *)
391 {
392  return SimpleOverloadAttributor(senf::membind(memfn,owner));
393 }
394 
395 template <class CastTo, class Signature>
396 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
397 senf::console::factory::
398 Command(boost::function<Signature> fn)
399 {
400  return detail::addOverloadedCommandNode<CastTo>(fn);
401 }
402 
403 template <class CastTo, class Function>
404 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
405 senf::console::factory::
406 Command(Function fn,
407  typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
408  typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
409 {
410  return detail::addOverloadedCommandNode<CastTo>(fn);
411 }
412 
413 template <class Signature>
414 prefix_ typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
415 senf::console::factory::
416 Command(boost::function<Signature> fn)
417 {
418  return detail::addOverloadedCommandNode<Signature>(fn);
419 }
420 
421 template <class Function>
422 prefix_ typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
423 senf::console::factory::
424 Command(Function fn,
425  typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
426  typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
427 {
428  return detail::addOverloadedCommandNode<Function>(fn);
429 }
430 
431 template <class Owner, class Member>
432 prefix_ typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
433 senf::console::factory::
434 Command(Member memfn, Owner * owner,
435  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
436 {
437  return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
438 }
439 
440 template <class Owner, class Member>
441 prefix_ typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
442 senf::console::factory::
443 Command(Member memfn, Owner const * owner,
444  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
445 {
446  return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
447 }
448 
449 template <class CastTo, class Owner, class Member>
450 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
451 senf::console::factory::
452 Command(Member memfn, Owner * owner,
453  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
454 {
455  return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
456 }
457 
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 const * owner,
462  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
463 {
464  return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
465 }
466 
467 //-/////////////////////////////////////////////////////////////////////////////////////////////////
468 #undef prefix_
469 
470 
471 // Local Variables:
472 // mode: c++
473 // fill-column: 100
474 // comment-column: 40
475 // c-file-style: "senf"
476 // indent-tabs-mode: nil
477 // ispell-local-dictionary: "american"
478 // compile-command: "scons -u test"
479 // End: