rapidyaml  0.16.0
parseandemitYAML,anddoitfast
emit_options.hpp
Go to the documentation of this file.
1 #ifndef C4_YML_EMIT_OPTIONS_HPP_
2 #define C4_YML_EMIT_OPTIONS_HPP_
3 
4 #ifndef C4_YML_COMMON_HPP_
5 #include <c4/yml/common.hpp>
6 #endif
7 
8 namespace c4 {
9 namespace yml {
10 
11 /** A lightweight object containing options to be used when emitting.
12  * @ingroup doc_emit
13  */
15 {
16 public:
17 
18  /** @cond dev */
19  typedef enum : uint32_t { // NOLINT
20  INDENT_FLOW_ML = 1u << 0u,
21  FORCE_FLOW_SPC = 1u << 1u,
22  EMIT_NONROOT_KEY = 1u << 2u,
23  EMIT_NONROOT_DASH = 1u << 3u,
24  EMIT_NONROOT_MARKUP = EMIT_NONROOT_KEY|EMIT_NONROOT_DASH,
25  JSON_ERR_ON_STREAM = 1u << 4u,
26  JSON_ERR_ON_TAG = 1u << 5u,
27  JSON_ERR_ON_ANCHOR = 1u << 6u,
28  JSON_ERR_MASK_ = JSON_ERR_ON_TAG|JSON_ERR_ON_ANCHOR,
29  DEFAULT_FLAGS = EMIT_NONROOT_KEY|INDENT_FLOW_ML,
30  } Flags_e;
31  /** @endcond */
32 
33 private:
34 
35  EmitOptions& set_flags_(bool enabled, Flags_e f)
36  {
37  if(enabled)
38  m_flags |= f;
39  else
40  m_flags &= ~f;
41  return *this;
42  }
43 
44 public:
45 
46  /** @name YAML flow customization
47  *
48  * Control formatting of YAML flow containers.
49  *
50  * @{ */
51 
52  /** Indent the contents of @ref FLOW_ML1 and @ref FLOW_MLN
53  * containers. Enabled by default. */
54  C4_ALWAYS_INLINE bool indent_flow_ml() const noexcept { return (m_flags & INDENT_FLOW_ML) != 0; }
55  EmitOptions& indent_flow_ml(bool enabled) noexcept { return set_flags_(enabled, INDENT_FLOW_ML); }
56 
57  /** Force everywhere a space after comma in flow mode, overriding
58  * the @ref FLOW_SPC status of individual containers. This only
59  * applies to @ref FLOW_ML1 or @ref FLOW_MLN containers. Disabled
60  * by default. */
61  EmitOptions& force_flow_spc(bool enabled) noexcept { return set_flags_(enabled, FORCE_FLOW_SPC); }
62  C4_ALWAYS_INLINE bool force_flow_spc() const noexcept { return (m_flags & FORCE_FLOW_SPC) != 0; }
63 
64  /** Set max columns for the emitted YAML in @ref FLOW_MLN
65  * mode. This will make the emitted YAML wrap around when the line
66  * reaches max cols, but only in containers with @ref FLOW_MLN
67  * mode. Defaults to 80 columns. */
68  EmitOptions& max_cols(id_type cols) noexcept { m_max_cols = cols; return *this; }
69  C4_ALWAYS_INLINE id_type max_cols() const noexcept { return m_max_cols; }
70  static constexpr const id_type max_cols_default = 80;
71 
72  /** @} */
73 
74 public:
75 
76  /** @name Nested nodes
77  *
78  * Control emission of nested (non-root) nodes.
79  *
80  * @{ */
81 
82  /** When emit starts on a nested (non-root) node, emit the node's
83  * key as well. This will maek the resulting YAML a map with the
84  * node as its single element. Enabled by default. */
85  EmitOptions& emit_nonroot_key(bool enabled) noexcept { return set_flags_(enabled, EMIT_NONROOT_KEY); }
86  C4_ALWAYS_INLINE bool emit_nonroot_key() const noexcept { return (m_flags & EMIT_NONROOT_KEY) != 0; }
87 
88  /** When emit starts on a nested (non-root) node, emit a leading
89  * dash. This will make the resulting YAML a seq with the node as
90  * its single element. Disabled by default. */
91  EmitOptions& emit_nonroot_dash(bool enabled) noexcept { return set_flags_(enabled, EMIT_NONROOT_DASH); }
92  C4_ALWAYS_INLINE bool emit_nonroot_dash() const noexcept { return (m_flags & EMIT_NONROOT_DASH) != 0; }
93 
94  /** @} */
95 
96 public:
97 
98  /** @name JSON behavior
99  *
100  * Controls the behavior when emitting JSON.
101  * @{ */
102 
103  /** Whether to trigger an error (or emit as seq) when finding a
104  * stream in json mode. Disabled by default. */
105  EmitOptions& json_err_on_stream(bool enabled) noexcept { return set_flags_(enabled, JSON_ERR_ON_STREAM); }
106  C4_ALWAYS_INLINE bool json_err_on_stream() const noexcept { return (m_flags & JSON_ERR_ON_STREAM) != 0; }
107 
108  /** Whether to trigger an error (or ignore the tag) when finding a
109  * tag in json mode. Disabled by default. */
110  EmitOptions& json_err_on_tag(bool enabled) noexcept { return set_flags_(enabled, JSON_ERR_ON_TAG); }
111  C4_ALWAYS_INLINE bool json_err_on_tag() const noexcept { return (m_flags & JSON_ERR_ON_TAG) != 0; }
112 
113  /** Whether to trigger an error (or ignore the anchor) when
114  * finding an anchor in json mode. Disabled by default. */
115  EmitOptions& json_err_on_anchor(bool enabled) noexcept { return set_flags_(enabled, JSON_ERR_ON_ANCHOR); }
116  C4_ALWAYS_INLINE bool json_err_on_anchor() const noexcept { return (m_flags & JSON_ERR_ON_ANCHOR) != 0; }
117 
118  /** @} */
119 
120 public:
121 
122  /** @name maximum depth for the emitted tree
123  *
124  * This prevents stack overflows by making the emitter fail when
125  * the tree exceeds the maximum depth.
126  *
127  * @{ */
128  C4_ALWAYS_INLINE id_type max_depth() const noexcept { return m_max_depth; }
129  EmitOptions& max_depth(id_type d) noexcept { m_max_depth = d; return *this; }
130  static constexpr const id_type max_depth_default = 64;
131  /** @} */
132 
133 public:
134 
135  bool operator== (const EmitOptions& that) const noexcept
136  {
137  return m_max_depth == that.m_max_depth
138  && m_max_cols == that.m_max_cols
139  && m_flags == that.m_flags;
140  }
141 
142 private:
143 
144  /** @cond dev */
145  id_type m_max_depth{max_depth_default};
146  id_type m_max_cols{max_cols_default};
147  uint32_t m_flags{DEFAULT_FLAGS};
148  /** @endcond */
149 
150 public: // deprecated methods
151 
152  /** @cond dev */
153  RYML_DEPRECATED("use .json_err_on_{tag,anchor}()") C4_ALWAYS_INLINE Flags_e json_error_flags() const noexcept { return static_cast<Flags_e>(m_flags & JSON_ERR_MASK_); }
154  RYML_DEPRECATED("use .json_err_on_{tag,anchor}()") EmitOptions& json_error_flags(Flags_e d) noexcept { m_flags = (d & JSON_ERR_MASK_); return *this; }
155  /** @endcond */
156 
157 };
158 
159 } // namespace yml
160 } // namespace c4
161 
162 #endif /* C4_YML_EMIT_HPP_ */
bool emit_nonroot_dash() const noexcept
bool indent_flow_ml() const noexcept
Indent the contents of FLOW_ML1 and FLOW_MLN containers.
bool force_flow_spc() const noexcept
bool json_err_on_tag() const noexcept
Common utilities and infrastructure used by ryml.
bool json_err_on_stream() const noexcept
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:124
EmitOptions & json_err_on_tag(bool enabled) noexcept
Whether to trigger an error (or ignore the tag) when finding a tag in json mode.
EmitOptions & json_err_on_anchor(bool enabled) noexcept
Whether to trigger an error (or ignore the anchor) when finding an anchor in json mode...
bool emit_nonroot_key() const noexcept
EmitOptions & emit_nonroot_dash(bool enabled) noexcept
When emit starts on a nested (non-root) node, emit a leading dash.
EmitOptions & indent_flow_ml(bool enabled) noexcept
EmitOptions & force_flow_spc(bool enabled) noexcept
Force everywhere a space after comma in flow mode, overriding the FLOW_SPC status of individual conta...
EmitOptions & max_cols(id_type cols) noexcept
Set max columns for the emitted YAML in FLOW_MLN mode.
bool json_err_on_anchor() const noexcept
A lightweight object containing options to be used when emitting.
EmitOptions & max_depth(id_type d) noexcept
id_type max_cols() const noexcept
EmitOptions & emit_nonroot_key(bool enabled) noexcept
When emit starts on a nested (non-root) node, emit the node's key as well.
#define RYML_EXPORT
Definition: export.hpp:18
EmitOptions & json_err_on_stream(bool enabled) noexcept
Whether to trigger an error (or emit as seq) when finding a stream in json mode.
bool operator==(const char c, basic_substring< C > const that) noexcept
Definition: substr.hpp:2438
id_type max_depth() const noexcept