OverloadedCommand.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_OverloadedCommand_
18 #define HH_SENF_Utils_Console_OverloadedCommand_ 1
19 
20 // Custom includes
21 #include "Node.hh"
22 #include <boost/intrusive_ptr.hpp>
23 #include <boost/range/iterator_range.hpp>
25 #include <boost/optional.hpp>
26 
27 //#include "OverloadedCommand.mpp"
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 
30 namespace senf {
31 namespace console {
32 
33  class OverloadedCommandNode;
34 
39  struct ArgumentDoc {
40  std::string name;
41  std::string type;
42  std::string defaultValue;
43  std::string doc;
44  bool singleToken;
45  };
46 
54  {
55  public:
56  //-////////////////////////////////////////////////////////////////////////
57  // Types
58 
59  typedef boost::intrusive_ptr<CommandOverload> ptr;
60  typedef boost::intrusive_ptr<CommandOverload const> cptr;
61 
62  //-////////////////////////////////////////////////////////////////////////
63 
64  virtual ~CommandOverload();
65 
66  void execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command);
68 
72  void operator()(boost::any & rv, std::ostream & os, ParseCommandInfo const & command);
74 
78  unsigned numArguments() const;
79  void argumentDoc(unsigned index, ArgumentDoc & doc) const;
81 
87  std::string doc() const;
88 
89  OverloadedCommandNode & node() const;
90 
93  unsigned overloadIndex() const;
94 
95  protected:
97 
98 #ifndef DOXYGEN
99  private:
100 #endif
101  virtual unsigned v_numArguments() const = 0;
103 
106  virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const = 0;
108 
112  virtual std::string v_doc() const = 0;
114 
117  virtual void v_execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command)
118  const = 0;
120 
123  private:
124  OverloadedCommandNode * node_;
125 
126  friend class OverloadedCommandNode;
127  };
128 
154  : public CommandNode
155  {
156  typedef std::vector<CommandOverload::ptr> Overloads;
157 
158  public:
159  //-////////////////////////////////////////////////////////////////////////
160  // Types
161 
162  typedef boost::shared_ptr<OverloadedCommandNode> ptr;
163  typedef boost::shared_ptr<OverloadedCommandNode const> cptr;
164  typedef boost::weak_ptr<OverloadedCommandNode> weak_ptr;
165 
168 
169  typedef boost::iterator_range<Overloads::const_iterator> OverloadsRange;
170 
171  //-////////////////////////////////////////////////////////////////////////
173  //\{
174 
175  static ptr create();
176 
177  //\}
178  //-////////////////////////////////////////////////////////////////////////
179 
180  template <class Command>
181  Command & add(boost::intrusive_ptr<Command> overload);
182 
183  OverloadedCommandNode & doc(std::string const & doc);
185  OverloadedCommandNode & shortdoc(std::string const & doc);
187 
188  unsigned overloadIndex(CommandOverload const & overload);
190 
193  OverloadsRange overloads() const;
194 
195  ptr thisptr();
196  cptr thisptr() const;
197 
198  static OverloadedCommandNode & insertOverload(DirectoryNode & dir, std::string const & name,
199  CommandOverload::ptr overload);
200 
201  private:
203 
204  virtual void v_help(std::ostream & output) const;
205  virtual std::string v_shorthelp() const;
206  virtual void v_execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command)
207  const;
208 
209  Overloads overloads_;
210  std::string doc_;
211  std::string shortdoc_;
212  };
213 
221  : public CommandOverload
222  {
223  public:
224  //-////////////////////////////////////////////////////////////////////////
225  // Types
226 
227  typedef boost::intrusive_ptr<SimpleCommandOverload> ptr;
228  typedef boost::function<void (std::ostream &, ParseCommandInfo const &)> Function;
229 
230  //-////////////////////////////////////////////////////////////////////////
232  //\{
233 
234  static SimpleCommandOverload::ptr create(Function fn);
236 
238  //\}
239  //-////////////////////////////////////////////////////////////////////////
240 
241  SimpleCommandOverload & doc(std::string const & doc);
243 
244  private:
245  explicit SimpleCommandOverload(Function fn);
246 
247  virtual unsigned v_numArguments() const;
248  virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const;
249  virtual std::string v_doc() const;
250  virtual void v_execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command)
251  const;
252 
253  Function fn_;
254  std::string doc_;
255  };
256 
258  : public detail::NodeFactory
259  {
260  public:
263 
265 
266  SimpleOverloadAttributor const & doc(std::string const & doc) const;
267  SimpleOverloadAttributor const & shortdoc(std::string const & doc) const;
268  SimpleOverloadAttributor const & overloadDoc(std::string const & doc) const;
269 
270  OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
271 
272  private:
273  SimpleCommandOverload::ptr overload_;
274  mutable boost::optional<std::string> doc_;
275  mutable boost::optional<std::string> shortdoc_;
276  };
277 
278 }}
279 
280 //-/////////////////////////////////////////////////////////////////////////////////////////////////
281 #include "OverloadedCommand.cci"
282 //#include "OverloadedCommand.ct"
283 #include "OverloadedCommand.cti"
284 #endif
285 
286 
287 // Local Variables:
288 // mode: c++
289 // fill-column: 100
290 // comment-column: 40
291 // c-file-style: "senf"
292 // indent-tabs-mode: nil
293 // ispell-local-dictionary: "american"
294 // compile-command: "scons -u test"
295 // End:
bool singleToken
true, if argument is parsed from single token
std::string type
Argument type (string representation)
std::string doc
Documentation for this argument.
boost::intrusive_ptr< CommandOverload const > cptr
boost::shared_ptr< OverloadedCommandNode const > cptr
boost::intrusive_ptr< CommandOverload > ptr
Config/console tree directory node.
Definition: Node.hh:406
Node public header.
boost::intrusive_ptr< SimpleCommandOverload > ptr
Command node which allows multiple registered callbacks.
Definition: Config.hh:28
Single parsed console command.
Definition: Parse.hh:363
std::string defaultValue
Default value (string representation) or empty string.
boost::function< void(std::ostream &, ParseCommandInfo const &)> Function
Base class for command overload of OverloadedCommandNode.
boost::iterator_range< Overloads::const_iterator > OverloadsRange
Config/console tree command node.
Definition: Node.hh:563
std::string name
Argument name.
boost::weak_ptr< OverloadedCommandNode > weak_ptr
Documentation for a single argument.
boost::shared_ptr< OverloadedCommandNode > ptr