ParsedCommand.hh
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 
17 #ifndef HH_SENF_Utils_Console_ParsedCommand_
18 #define HH_SENF_Utils_Console_ParsedCommand_ 1
19 
20 // Custom includes
21 
22 #define BOOST_PARAMETER_MAX_ARITY 7
23 
24 #include <vector>
25 #include <boost/type_traits/function_traits.hpp>
26 #include <boost/type_traits/is_member_function_pointer.hpp>
27 #include <boost/mpl/if.hpp>
28 #include <boost/parameter/keyword.hpp>
29 #include <boost/parameter/parameters.hpp>
30 #include <senf/config.hh>
31 #include "OverloadedCommand.hh"
32 #include "Traits.hh"
34 
35 #include "ParsedCommand.ih"
36 #include "ParsedCommand.mpp"
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 
39 namespace senf {
40 namespace console {
41 
42  namespace detail { struct ArgumentInfoBase; }
43 
127  : public CommandOverload
128  {
129  public:
130  typedef boost::intrusive_ptr<ParsedCommandOverloadBase> ptr;
131 
132  detail::ArgumentInfoBase & arg(unsigned n) const;
133 
134  void doc(std::string const & d);
135 
136  protected:
138 
139  template <class Type> void addParameter();
140 
141  private:
142  virtual unsigned v_numArguments() const;
143  virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const;
144  virtual std::string v_doc() const;
145 
146  typedef std::vector<detail::ArgumentInfoBase::ptr> Parameters;
147  Parameters parameters_;
148  std::string doc_;
149  };
150 
165  template <class FunctionTraits, class ReturnType, unsigned arity>
167  {
168  public:
169  typedef boost::intrusive_ptr<ParsedCommandOverload> ptr;
170  };
171 
172 #ifndef DOXYGEN
173 
174 # define BOOST_PP_ITERATION_PARAMS_1 \
175  (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
176  SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
177  1))
178 # include BOOST_PP_ITERATE()
179 
180 #endif
181 
193  : public detail::NodeFactory
194  {
195  public:
198 
199  protected:
201  ParsedCommandAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
202 
203  void argName(std::string const & name);
204  void argDoc(std::string const & doc);
205  void typeName(std::string const & doc);
206  void defaultDoc(std::string const & doc);
207 
208  ParsedCommandOverloadBase & overload() const;
209  void overloadDoc(std::string const & doc);
210  void nodeDoc(std::string const & doc);
211  void shortDoc(std::string const & doc);
212 
213  private:
214  OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
215 
217  unsigned index_;
218  boost::optional<std::string> doc_;
219  boost::optional<std::string> shortdoc_;
220 
222  };
223 
234  template <class Overload>
237  {
238  public:
239  Overload & overload() const;
240 
241  protected:
242  ParsedCommandAttributor(typename Overload::ptr overload, unsigned index);
243  ParsedCommandAttributor(ParsedCommandAttributorBase const & other, unsigned index);
244 
245  private:
246  };
247 
307  namespace kw {
308  BOOST_PARAMETER_KEYWORD(type, name)
309 
310  BOOST_PARAMETER_KEYWORD(type, description)
311 
316  BOOST_PARAMETER_KEYWORD(type, default_value)
317 
357  BOOST_PARAMETER_KEYWORD(type, type_name)
358 
362  BOOST_PARAMETER_KEYWORD(type, default_doc)
363 
369  BOOST_PARAMETER_KEYWORD(type, parser)
370 
387  }
388 
399  template <class Overload, class Self, class ReturnType=typename Overload::traits::result_type>
401  : public ParsedCommandAttributor<Overload>
402  {
403  public:
404  Self doc(std::string const & doc);
405  Self shortdoc(std::string const & doc);
406  Self overloadDoc(std::string const & doc);
407  Self formatter(typename Overload::Formatter formatter);
409 
420  protected:
421  ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index);
422  ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
423 
424  private:
425  };
426 
427 #ifndef DOXYGEN
428 
429  template <class Overload, class Self>
430  class ParsedArgumentAttributorBase<Overload, Self, void>
431  : public ParsedCommandAttributor<Overload>
432  {
433  public:
434  Self doc(std::string const & doc);
435  Self shortdoc(std::string const & doc);
436  Self overloadDoc(std::string const & doc);
437 
438  protected:
439  ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index);
440  ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
441 
442  private:
443  };
444 
445 #endif
446 
457  template <class Overload, unsigned index, bool flag>
459  : public ParsedArgumentAttributorBase< Overload,
460  ParsedArgumentAttributor<Overload, index, flag> >
461  {
462  typedef boost::parameter::parameters<
468  kw::type::parser> arg_params;
469 
470  public:
471  typedef typename senf::function_traits_arg_type<
472  typename Overload::traits, int(index) >::type arg_type;
475 
476  next_type arg() const;
477 
496 #ifndef DOXYGEN
497 # define BOOST_PP_ITERATION_PARAMS_1 \
498  (4, (1, BOOST_PARAMETER_MAX_ARITY, \
499  SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
500  5))
501 # include BOOST_PP_ITERATE()
502 #endif
503 
504  private:
505  explicit ParsedArgumentAttributor(typename Overload::ptr overload);
507 
508  template <class ArgumentPack>
509  next_type argInfo(ArgumentPack const & args);
510  template <class Kw, class ArgumentPack>
511  void argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_);
512 
513  template <class ArgumentPack>
514  void argInfo(boost::parameter::keyword<kw::type::name> const &,
515  ArgumentPack const & args, boost::mpl::true_);
516  template <class ArgumentPack>
517  void argInfo(boost::parameter::keyword<kw::type::description> const &,
518  ArgumentPack const & args, boost::mpl::true_);
519  template <class ArgumentPack>
520  void argInfo(boost::parameter::keyword<kw::type::default_value> const &,
521  ArgumentPack const & args, boost::mpl::true_);
522  template <class ArgumentPack>
523  void argInfo(boost::parameter::keyword<kw::type::type_name> const &,
524  ArgumentPack const & args, boost::mpl::true_);
525  template <class ArgumentPack>
526  void argInfo(boost::parameter::keyword<kw::type::default_doc> const &,
527  ArgumentPack const & args, boost::mpl::true_);
528  template <class ArgumentPack>
529  void argInfo(boost::parameter::keyword<kw::type::parser> const &,
530  ArgumentPack const & args, boost::mpl::true_);
531 
532  next_type next() const;
533 
534  void defaultValue(value_type const & value);
535  template <class Fn> void parser(Fn fn);
536 
537  template <class O, unsigned i, bool f>
539 
540  friend struct detail::ParsedCommandAddNodeAccess;
541  };
542 
543 #ifndef DOXYGEN
544 
545  template <class Overload, unsigned index>
546  class ParsedArgumentAttributor<Overload, index, false>
547  : public ParsedArgumentAttributorBase< Overload,
548  ParsedArgumentAttributor<Overload, index, false> >
549  {
550  public:
552  typedef ParsedArgumentAttributor return_type;
553 
554  private:
555  explicit ParsedArgumentAttributor(typename Overload::ptr overload);
557 
558  template <class O, unsigned i, bool f>
559  friend class ParsedArgumentAttributor;
560 
561  friend struct detail::ParsedCommandAddNodeAccess;
562  };
563 
564 #endif
565 
566 namespace factory {
567 
568 #ifdef DOXYGEN
569 
605  {
606  public:
608  typedef unspecified result_type;
609 
610  Command(unspecified fn);
611  template <class Signature>
612  Command(unspecified fn);
613 
616  Command(member_function_pointer fn, Owner const * owner);
618  template <class Signature>
619  Command(member_function_pointer fn, Owner const * owner);
622 
625  };
626 
627 #else
628 
629  template <class Signature>
631  Command(boost::function<Signature> fn,
632  typename boost::enable_if_c<detail::ParsedCommandTraits<Signature>::is_simple>::type * = 0);
633 
634  template <class Function>
636  Command(Function fn,
637  typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_simple>::type * = 0);
638 
639  template <class Owner, class Member>
641  Command(Member memfn, Owner * owner,
642  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0,
643  typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type * = 0);
644 
645  template <class Owner, class Member>
647  Command(Member memfn, Owner const * owner,
648  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0,
649  typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type * = 0);
650 
651  template <class CastTo, class Signature>
652  typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
653  Command(boost::function<Signature> fn);
654 
655  template <class CastTo, class Function>
656  typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
657  Command(Function fn,
658  typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type * = 0,
659  typename boost::disable_if<boost::is_member_function_pointer<Function> >::type * = 0);
660 
661  template <class Signature>
662  typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
663  Command(boost::function<Signature> fn);
664 
665  template <class Function>
666  typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
667  Command(Function fn,
668  typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type * = 0,
669  typename boost::disable_if<boost::is_member_function_pointer<Function> >::type * = 0);
670 
671  template <class Owner, class Member>
672  typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
673  Command(Member memfn, Owner * owner,
674  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
675 
676  template <class Owner, class Member>
677  typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
678  Command(Member memfn, Owner const * owner,
679  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
680 
681  template <class CastTo, class Owner, class Member>
682  typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
683  Command(Member memfn, Owner * owner,
684  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
685 
686  template <class CastTo, class Owner, class Member>
687  typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
688  Command(Member memfn, Owner const * owner,
689  typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
690 
691 #endif
692 
693 }}}
694 
695 //-/////////////////////////////////////////////////////////////////////////////////////////////////
696 #include "ParsedCommand.cci"
697 #include "ParsedCommand.ct"
698 #include "ParsedCommand.cti"
699 #endif
700 
701 
702 // Local Variables:
703 // mode: c++
704 // fill-column: 100
705 // comment-column: 40
706 // c-file-style: "senf"
707 // indent-tabs-mode: nil
708 // ispell-local-dictionary: "american"
709 // compile-command: "scons -u test"
710 // End:
Derived class dependent ParsedCommandBase attributes.
unspecified_keyword_type default_value
Argument default value.
unspecified_keyword_type default_doc
String rep of default value.
unspecified_keyword_type parser
Argument parser.
Config/console tree directory node.
Definition: Node.hh:406
Non argument dependent ParsedCommandBase attributes.
unspecified_keyword_type type_name
Type name of this arguments type.
OverloadedCommandNode factory.
boost::intrusive_ptr< ParsedCommandOverload > ptr
Command node which allows multiple registered callbacks.
senf::remove_cvref< arg_type >::type value_type
Definition: Config.hh:28
Generic ParsedCommandOverloadBase attributes.
Traits public header.
ParsedArgumentAttributor< Overload, index+1 > next_type
Argument dependent ParsedCommandBase attributes.
OverloadedCommand public header.
CommandOverload implementation with automatic argument parsing.
OverloadedCommandNode node_type
senf::function_traits_arg_type< typename Overload::traits, int(index) >::type arg_type
Base class for command overload of OverloadedCommandNode.
unspecified_keyword_type description
One-line argument description.
boost::intrusive_ptr< ParsedCommandOverloadBase > ptr
unspecified_keyword_type name
Argument name.
Documentation for a single argument.