libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
print.hpp
Go to the documentation of this file.
1 /*
2 ** Copyright (C) 2017 SoftBank Robotics
3 ** See COPYING for the license
4 */
5 
6 #ifndef QI_PRINT_HPP
7 #define QI_PRINT_HPP
8 
9 #include <qi/api.hpp>
10 #include <qi/iocolor.hpp>
11 #include <qi/flags.hpp>
12 #include <boost/variant.hpp>
13 #include <boost/utility/string_ref.hpp>
14 #include <ostream>
15 #include <memory>
16 
19 
20 namespace qi
21 {
22  class MetaObject;
23  class MetaMethod;
24  class MetaSignal;
25  class MetaProperty;
26  class StructTypeInterface;
27  class ListTypeInterface;
28  class MapTypeInterface;
29  class TypeInterface;
30  class Signature;
31 
32 namespace detail
33 {
34 
35  enum class DisplayHiddenMembers : bool
36  {
37  Hide = false,
38  Display = true,
39  };
40 
64  {
65  public:
66  enum class Option
67  {
68  Colorized = 1 << 0,
69  Documentation = 1 << 1,
70  RawSignatures = 1 << 2
71  };
73 
74  enum class RecurseOption : bool
75  {
76  DoNotRecurse = false,
77  Recurse = true,
78  };
79 
82  {
83  friend PrettyPrintStream;
85 
86  public:
87  ~IndentLevel();
88 
89  // deleted copy and assignement
90  IndentLevel(const IndentLevel&) = delete;
91  IndentLevel& operator=(const IndentLevel&) = delete;
92 
93  private:
94  PrettyPrintStream& _printer;
95  };
96  friend IndentLevel;
97  using IndentLevelPtr = std::unique_ptr<IndentLevel>;
98 
100  struct QI_API Column
101  {
102  using ValueType = boost::variant<int, std::string>;
103  enum class Alignment
104  {
105  Left = 0,
106  Right = 1,
107  Internal = 2,
108  };
109  enum class Option
110  {
111  DelimitWithSpace = 1 << 0,
112  };
114 
115  explicit Column(ValueType value,
117  Alignment alignment = Alignment::Left,
118  Options opts = Options{Option::DelimitWithSpace},
119  int width = 0,
120  char fillChar = ' ');
121 
122  // Regular:
125  , alignment(Alignment::Left)
126  , width(0)
127  , fillChar(0)
128  {
129  }
130 
131  KA_GENERATE_FRIEND_REGULAR_OPS_6(Column, value, color, alignment, opts, width, fillChar)
132 
133  ValueType value;
135  Alignment alignment;
136  Options opts;
137  int width;
138  char fillChar;
139  };
140  using Columns = std::vector<Column>;
141 
144  struct QI_API Line
145  {
146  enum class Option
147  {
148  NewLine = 1 << 0,
149  Indent = 1 << 1,
150  };
152 
153  explicit Line(const std::initializer_list<Column>& columns);
154  explicit Line(const Columns& columns, Options opts = Options{Option::NewLine, Option::Indent});
155 
156  // Regular:
157  Line() = default;
158 
159  KA_GENERATE_FRIEND_REGULAR_OPS_2(Line, columns, opts)
160 
161  Columns columns;
162  Options opts;
163  };
164 
165  using string_ref = boost::string_ref;
166  static const string_ref infoLabel;
167  static const string_ref methodsLabel;
168  static const string_ref signalsLabel;
169  static const string_ref propertiesLabel;
170  static const string_ref membersLabel;
171  static const string_ref returnTypeLabel;
172  static const string_ref returnDescrLabel;
173  static const string_ref elementTypeLabel;
174  static const string_ref keyTypeLabel;
175  static const string_ref parametersLabel;
176  static const string_ref signalTypesLabel;
177 
178  explicit PrettyPrintStream(std::ostream& stream,
180  Options flags = Options{ Option::Colorized },
181  int indentLevel = 0);
182 
183  PrettyPrintStream(const PrettyPrintStream&) = delete;
184  PrettyPrintStream& operator=(const PrettyPrintStream&) = delete;
185 
186  // TODO: Make this constructor = default when we get rid of VS2013
188  : _stream(std::move(o._stream))
189  , _displayHidden(std::move(o._displayHidden))
190  , _options(std::move(o._options))
191  , _indentLevel(std::move(o._indentLevel))
192  {
193  o._stream = nullptr;
194  }
195 
196  // TODO: Make this operator = default when we get rid of VS2013
198  {
199  _stream = std::move(o._stream);
200  _displayHidden = std::move(o._displayHidden);
201  _options = std::move(o._options);
202  _indentLevel = std::move(o._indentLevel);
203  o._stream = nullptr;
204  return *this;
205  }
206 
207  // Custom:
208  static Line makeSectionHeader(const Line& line);
209  static Line makeSubSectionHeader(const Line& line);
210 
211  IndentLevelPtr makeIndentLevel();
212 
213  void print(const Line& line);
214  void print(const MetaObject& mobj);
215  void print(const MetaMethod& method, int offsetLabel = 0, RecurseOption recurse = RecurseOption::Recurse);
216  void print(const MetaSignal& signal, int offsetLabel = 0, RecurseOption recurse = RecurseOption::Recurse);
217  void print(const MetaProperty& property, int offsetLabel = 0, RecurseOption recurse = RecurseOption::Recurse);
218  void print(TypeInterface* type);
219  void print(StructTypeInterface& structType);
220  void print(ListTypeInterface& listType);
221  void print(MapTypeInterface& mapType);
222 
225  template <typename P>
227  {
228  print(std::forward<P>(printable));
229  return *this;
230  }
231 
232  private:
233  inline int indentWidth() const
234  {
235  return _indentLevel * indentFactor;
236  }
237 
238  void increaseIndent();
239  void decreaseIndent();
240 
241  StreamColor colorIfEnabled(StreamColor color) const;
242 
244  std::string stringify(const qi::Signature& signature) const;
245 
246  void print(const Column& column) const;
247 
248  void printParameters(const std::vector<qi::Signature>& paramsSignatures,
249  const std::string& label,
250  RecurseOption recurse);
251 
253  template <typename Proc>
254  void printParameters(const std::vector<qi::Signature>& paramsSignatures,
255  const std::string& label,
256  RecurseOption recurse,
257  Proc makeExtraLine);
258 
260  template <typename Proc>
261  void print(TypeInterface* type, Proc makeHeaderLine);
262 
263  void printDetails(TypeInterface& type);
264 
266  template <typename L>
267  void printMetaObjectMembers(const L& members, string_ref headerLabel);
268 
269  static const int indentFactor;
270  static const int maxOffset;
271  static const int idColumnWidth;
272  std::ostream* _stream;
273  DisplayHiddenMembers _displayHidden;
274  Options _options;
275  int _indentLevel;
276  };
277 
293  {
294  public:
295  explicit ParseablePrintStream(std::ostream& stream,
297 
299  ParseablePrintStream& operator=(const ParseablePrintStream&) = delete;
300 
301  // TODO: Make this constructor = default when we get rid of VS2013
303  : _stream(std::move(o._stream))
304  , _displayHidden(std::move(o._displayHidden))
305  {
306  o._stream = nullptr;
307  }
308 
309  // TODO: Make this operator = default when we get rid of VS2013
311  {
312  _stream = std::move(o._stream);
313  _displayHidden = std::move(o._displayHidden);
314  o._stream = nullptr;
315  return *this;
316  }
317 
318  // Custom:
320  template <typename S>
322  {
323  *_stream << std::forward<S>(streamable);
324  return *this;
325  }
326 
328  {
329  print(obj);
330  return *this;
331  }
332 
333  void print(const MetaObject& mobj);
334 
335  private:
337  template <typename L>
338  void printMetaObjectMembers(const L& members);
339 
340  std::ostream* _stream;
341  DisplayHiddenMembers _displayHidden;
342  };
343 
344 }
345 }
346 
347 #endif // QI_PRINT_HPP
Contains the data and options needed to fill and format one column of a line.
Definition: print.hpp:100
#define QI_API
Definition: api.hpp:33
Representation of a method in an GenericObject.
Definition: metamethod.hpp:42
boost::string_ref string_ref
Definition: print.hpp:165
PrettyPrintStream & operator<<(P &&printable)
Definition: print.hpp:226
ParseablePrintStream & operator=(ParseablePrintStream &&o) BOOST_NOEXCEPT
Definition: print.hpp:310
dll import/export and compiler message
Representation of a Signal in an GenericObject.
Definition: metasignal.hpp:20
StreamColor
The Stream Color enum.
Definition: iocolor.hpp:19
ParseablePrintStream & operator<<(const MetaObject &obj)
Definition: print.hpp:327
ParseablePrintStream(ParseablePrintStream &&o) BOOST_NOEXCEPT
Definition: print.hpp:302
Encapsulates in a RAII style a shift or an indentation level of a PrettyPrintStream instance...
Definition: print.hpp:81
boost::variant< int, std::string > ValueType
Definition: print.hpp:102
std::vector< Column > Columns
Definition: print.hpp:140
PrettyPrintStream & operator=(PrettyPrintStream &&o) BOOST_NOEXCEPT
Definition: print.hpp:197
std::unique_ptr< IndentLevel > IndentLevelPtr
Definition: print.hpp:97
DisplayHiddenMembers
Definition: print.hpp:35
ParseablePrintStream & operator<<(S &&streamable)
OutputStreamable S.
Definition: print.hpp:321
LogColor color()
Get log color.
PrettyPrintStream(PrettyPrintStream &&o) BOOST_NOEXCEPT
Definition: print.hpp:187
Description of the signals and methods accessible on an ObjectTypeInterface.
Definition: metaobject.hpp:25