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
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
15 \brief ParsedCommand inline template implementation */
17 #include "ParsedCommand.ih"
20 #include <boost/format.hpp>
21 #include <senf/Utils/membind.hh>
22 #include <senf/Utils/parameter.hh>
24 #define prefix_ inline
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::console::detail::ArgumentInfo<ParameterType>
30 template <class ParameterType>
31 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
32 senf::console::detail::ArgumentInfo<ParameterType>::create()
34 return ptr(new ArgumentInfo());
37 template <class ParameterType>
38 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
39 : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description(),
40 ArgumentTraits<ParameterType>::singleToken ),
44 template <class ParameterType>
45 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
48 return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::console::ParsedCommandOverloadBase
55 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
57 parameters_.push_back(detail::ArgumentInfo<Type>::create());
60 //-/////////////////////////////////////////////////////////////////////////////////////////////////
61 // senf::console::ParsedCommandOverload<FunctionTraits,n>
63 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
64 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
66 #include BOOST_PP_ITERATE()
68 //-/////////////////////////////////////////////////////////////////////////////////////////////////
69 // senf::console::ParsedCommandAttributor<Overload>
71 template <class Overload>
72 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
75 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
78 template <class Overload>
79 prefix_ senf::console::ParsedCommandAttributor<Overload>::
80 ParsedCommandAttributor(typename Overload::ptr overload, unsigned index)
81 : ParsedCommandAttributorBase (overload, index)
84 template <class Overload>
85 prefix_ senf::console::ParsedCommandAttributor<Overload>::
86 ParsedCommandAttributor(ParsedCommandAttributorBase const & other, unsigned index)
87 : ParsedCommandAttributorBase (other, index)
90 //-/////////////////////////////////////////////////////////////////////////////////////////////////
91 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
93 template <class Overload, class Self, class ReturnType>
95 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
97 this->ParsedCommandAttributorBase::nodeDoc(doc);
98 return static_cast<Self const &>(*this);
101 template <class Overload, class Self, class ReturnType>
102 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
103 shortdoc(std::string const & doc)
105 this->ParsedCommandAttributorBase::shortDoc(doc);
106 return static_cast<Self const &>(*this);
109 template <class Overload, class Self, class ReturnType>
110 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
111 overloadDoc(std::string const & doc)
113 this->ParsedCommandAttributorBase::overloadDoc(doc);
114 return static_cast<Self const &>(*this);
117 template <class Overload, class Self, class ReturnType>
118 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
119 formatter(typename Overload::Formatter f)
121 this->overload().formatter(f);
122 return static_cast<Self const &>(*this);
125 template <class Overload, class Self, class ReturnType>
127 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
128 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
129 : ParsedCommandAttributor<Overload> (overload, index)
132 template <class Overload, class Self, class ReturnType>
134 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
135 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
136 : ParsedCommandAttributor<Overload> (other, index)
139 template <class Overload, class Self>
141 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
143 this->ParsedCommandAttributorBase::nodeDoc(doc);
144 return static_cast<Self const &>(*this);
147 template <class Overload, class Self>
148 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
149 shortdoc(std::string const & doc)
151 this->ParsedCommandAttributorBase::shortDoc(doc);
152 return static_cast<Self const &>(*this);
155 template <class Overload, class Self>
156 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
157 overloadDoc(std::string const & doc)
159 this->ParsedCommandAttributorBase::overloadDoc(doc);
160 return static_cast<Self const &>(*this);
163 template <class Overload, class Self>
165 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
166 ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index)
167 : ParsedCommandAttributor<Overload> (overload, index)
170 template <class Overload, class Self>
172 senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
173 ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index)
174 : ParsedCommandAttributor<Overload> (other, index)
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
178 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
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()
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)
194 # define ProcessArg(tag) \
195 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
198 ProcessArg(description);
199 ProcessArg(default_value);
200 ProcessArg(type_name);
201 ProcessArg(default_doc);
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_)
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,
221 this->argName(args[kw::name]);
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,
230 this->argDoc(args[kw::description]);
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,
239 this->defaultValue(args[kw::default_value]);
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,
248 this->typeName(args[kw::type_name]);
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,
257 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
258 this->defaultDoc(args[kw::default_doc]);
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,
267 this->parser(args[kw::parser]);
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)
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)
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()
287 return ParsedArgumentAttributor<Overload, index+1>(*this);
290 template <class Overload, unsigned index, bool flag>
291 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 defaultValue(value_type const & value)
294 this->overload().template arg<index>().defaultValue = value;
295 this->overload().arg(index).hasDefault = true;
298 template <class Overload, unsigned index, bool flag>
300 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
302 this->overload().template arg<index>().parser = fn;
305 //-/////////////////////////////////////////////////////////////////////////////////////////////////
306 // senf::console::ParsedArgumentAttributor<Overload, index, false>
308 template <class Overload, unsigned index>
310 senf::console::ParsedArgumentAttributor<Overload, index, false>::
311 ParsedArgumentAttributor(typename Overload::ptr overload)
312 : ParsedArgumentAttributorBase<
313 Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
316 template <class Overload, unsigned index>
318 senf::console::ParsedArgumentAttributor<Overload, index, false>::
319 ParsedArgumentAttributor(ParsedCommandAttributorBase const & other)
320 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (other, index)
323 //-/////////////////////////////////////////////////////////////////////////////////////////////////
332 struct ParsedCommandAddNodeAccess
334 template <class Attributor, class NodePtr>
335 static Attributor attributor(NodePtr ptr)
336 { return Attributor(ptr); }
339 template <class Signature, class Fn>
340 typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
341 addOverloadedCommandNode(Fn fn)
343 typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
344 typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
345 typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
347 return detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
348 CreateParsedCommandOverload<CmdTraits>::create(fn));
355 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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 *)
363 return SimpleOverloadAttributor(fn);
366 template <class Function>
367 prefix_ senf::console::SimpleOverloadAttributor
368 senf::console::factory::
370 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_simple>::type *)
372 return SimpleOverloadAttributor(fn);
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 *)
382 return SimpleOverloadAttributor(senf::membind(memfn,owner));
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 *)
392 return SimpleOverloadAttributor(senf::membind(memfn,owner));
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)
400 return detail::addOverloadedCommandNode<CastTo>(fn);
403 template <class CastTo, class Function>
404 prefix_ typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
405 senf::console::factory::
407 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
408 typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
410 return detail::addOverloadedCommandNode<CastTo>(fn);
413 template <class Signature>
414 prefix_ typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
415 senf::console::factory::
416 Command(boost::function<Signature> fn)
418 return detail::addOverloadedCommandNode<Signature>(fn);
421 template <class Function>
422 prefix_ typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
423 senf::console::factory::
425 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *,
426 typename boost::disable_if<boost::is_member_function_pointer<Function> >::type *)
428 return detail::addOverloadedCommandNode<Function>(fn);
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 *)
437 return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
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 *)
446 return detail::addOverloadedCommandNode<Member>(senf::membind(memfn,owner));
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 *)
455 return detail::addOverloadedCommandNode<CastTo>(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 const * owner,
462 typename boost::enable_if<boost::is_member_function_pointer<Member> >::type *)
464 return detail::addOverloadedCommandNode<CastTo>(senf::membind(memfn,owner));
467 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"