rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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
8namespace c4 {
9namespace yml {
10
11/** A lightweight object containing options to be used when emitting.
12 * @ingroup doc_emit
13 */
15{
16public:
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
33private:
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
44public:
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
74public:
75
76 /** @name Nested nodes
77 *
78 * Control emission of non-root (nested) nodes.
79 *
80 * @{ */
81
82 /** When emit starts on a node which is not the root node, emit
83 * the node key as well. This will make the resuling YAML a map
84 * with the 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 node which is not the root node, emit a
89 * leading dash. This will make the resulting YAML a seq with the
90 * node as 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
96public:
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
120public:
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
133public:
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
142private:
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
150public: // 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_ */
Common utilities and infrastructure used by ryml.
#define RYML_EXPORT
Definition export.hpp:18
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:305
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
A lightweight object containing options to be used when emitting.
bool emit_nonroot_key() const noexcept
id_type max_cols() const noexcept
bool json_err_on_tag() const noexcept
bool force_flow_spc() const noexcept
static constexpr const id_type max_cols_default
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
id_type max_depth() const noexcept
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 & max_depth(id_type d) noexcept
bool json_err_on_stream() const noexcept
EmitOptions & json_err_on_stream(bool enabled) noexcept
Whether to trigger an error (or emit as seq) when finding a stream in json mode.
EmitOptions & indent_flow_ml(bool enabled) noexcept
bool indent_flow_ml() const noexcept
Indent the contents of FLOW_ML1 and FLOW_MLN containers.
EmitOptions & emit_nonroot_key(bool enabled) noexcept
When emit starts on a node which is not the root node, emit the node key as well.
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_dash() const noexcept
EmitOptions & emit_nonroot_dash(bool enabled) noexcept
When emit starts on a node which is not the root node, emit a leading dash.
EmitOptions & force_flow_spc(bool enabled) noexcept
Force everywhere a space after comma in flow mode, overriding the FLOW_SPC status of individual conta...
static constexpr const id_type max_depth_default