rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Emit to memory buffer

Topics

 Emit to resizeable container

Functions

bool c4::yml::EmitOptions::operator== (const EmitOptions &that) const noexcept
template<class ... WriterArgs>
 c4::yml::Emitter< Writer >::Emitter (EmitOptions const &opts, WriterArgs &&...args)
 Construct the emitter and its internal Writer state.
template<class ... WriterArgs>
 c4::yml::Emitter< Writer >::Emitter (WriterArgs &&...args)
 Construct the emitter and its internal Writer state, using default emit options.
substr c4::yml::Emitter< Writer >::emit_as (EmitType_e type, Tree const &t, id_type id, bool error_on_excess)
 emit!
substr c4::yml::Emitter< Writer >::emit_as (EmitType_e type, Tree const &t, bool error_on_excess=true)
 emit starting at the root node
substr c4::yml::Emitter< Writer >::emit_as (EmitType_e type, ConstNodeRef const &n, bool error_on_excess=true)
 emit starting at the given node
EmitOptions const & c4::yml::Emitter< Writer >::options () const noexcept
 get the emit options for this object
void c4::yml::Emitter< Writer >::options (EmitOptions opts) noexcept
 set the emit options for this object
void c4::yml::Emitter< Writer >::max_depth (id_type max_depth) noexcept
 set the max depth for emitted trees (to prevent a stack overflow)
id_type c4::yml::Emitter< Writer >::max_depth () const noexcept
 get the max depth for emitted trees (to prevent a stack overflow)
 c4::yml::as_json::as_json (Tree const &t, EmitOptions const &opts={})
 c4::yml::as_json::as_json (Tree const &t, id_type id, EmitOptions const &opts={})
 c4::yml::as_json::as_json (ConstNodeRef const &n, EmitOptions const &opts={})
 c4::yml::as_yaml::as_yaml (Tree const &t, EmitOptions const &opts={})
 c4::yml::as_yaml::as_yaml (Tree const &t, id_type id, EmitOptions const &opts={})
 c4::yml::as_yaml::as_yaml (ConstNodeRef const &n, EmitOptions const &opts={})
substr c4::yml::emit_yaml (Tree const &t, id_type id, EmitOptions const &opts, substr buf, bool error_on_excess=true)
 (1) emit YAML to the given buffer.
substr c4::yml::emit_yaml (Tree const &t, id_type id, substr buf, bool error_on_excess=true)
 (2) like (1), but use default emit options
substr c4::yml::emit_json (Tree const &t, id_type id, EmitOptions const &opts, substr buf, bool error_on_excess=true)
 (1) emit JSON to the given buffer.
substr c4::yml::emit_json (Tree const &t, id_type id, substr buf, bool error_on_excess=true)
 (2) like (1), but use default emit options
substr c4::yml::emit_yaml (Tree const &t, EmitOptions const &opts, substr buf, bool error_on_excess=true)
 (1) emit YAML to the given buffer.
substr c4::yml::emit_yaml (Tree const &t, substr buf, bool error_on_excess=true)
 (2) like (1), but use default emit options
substr c4::yml::emit_json (Tree const &t, EmitOptions const &opts, substr buf, bool error_on_excess=true)
 (1) emit JSON to the given buffer.
substr c4::yml::emit_json (Tree const &t, substr buf, bool error_on_excess=true)
 (2) like (1), but use default emit options
substr c4::yml::emit_yaml (ConstNodeRef const &r, EmitOptions const &opts, substr buf, bool error_on_excess=true)
 (1) emit YAML to the given buffer.
substr c4::yml::emit_yaml (ConstNodeRef const &r, substr buf, bool error_on_excess=true)
 (2) like (1), but use default emit options
substr c4::yml::emit_json (ConstNodeRef const &r, EmitOptions const &opts, substr buf, bool error_on_excess=true)
 (1) emit JSON to the given buffer.
substr c4::yml::emit_json (ConstNodeRef const &r, substr buf, bool error_on_excess=true)
 (2) like (1), but use default emit options

Variables

Tree const * c4::yml::as_json::tree
id_type c4::yml::as_json::node
EmitOptions c4::yml::as_json::options
Tree const * c4::yml::as_yaml::tree
id_type c4::yml::as_yaml::node
EmitOptions c4::yml::as_yaml::options

max depth for the emitted tree

This makes the emitter fail when emitting trees exceeding the max_depth.

static constexpr const id_type c4::yml::EmitOptions::max_depth_default = 64
id_type c4::yml::EmitOptions::max_depth () const noexcept
EmitOptionsc4::yml::EmitOptions::max_depth (id_type d) noexcept

option flags

bool c4::yml::EmitOptions::emit_nonroot_key () const noexcept
EmitOptionsc4::yml::EmitOptions::emit_nonroot_key (bool enabled) noexcept
bool c4::yml::EmitOptions::emit_nonroot_dash () const noexcept
EmitOptionsc4::yml::EmitOptions::emit_nonroot_dash (bool enabled) noexcept
bool c4::yml::EmitOptions::indent_flow_ml () const noexcept
EmitOptionsc4::yml::EmitOptions::indent_flow_ml (bool enabled) noexcept
EmitOptionFlags_e c4::yml::EmitOptions::json_error_flags () const noexcept
EmitOptionsc4::yml::EmitOptions::json_error_flags (EmitOptionFlags_e d) noexcept

Detailed Description

Function Documentation

◆ emit_nonroot_key() [1/2]

bool c4::yml::EmitOptions::emit_nonroot_key ( ) const
inlinenoexcept

Definition at line 82 of file emit.hpp.

82{ return (m_option_flags & EMIT_NONROOT_KEY) != 0; }

◆ emit_nonroot_key() [2/2]

EmitOptions & c4::yml::EmitOptions::emit_nonroot_key ( bool enabled)
inlinenoexcept

Definition at line 83 of file emit.hpp.

83{ m_option_flags = (EmitOptionFlags_e)(enabled ? (m_option_flags | EMIT_NONROOT_KEY) : (m_option_flags & ~EMIT_NONROOT_KEY)); return *this; }

◆ emit_nonroot_dash() [1/2]

bool c4::yml::EmitOptions::emit_nonroot_dash ( ) const
inlinenoexcept

Definition at line 85 of file emit.hpp.

85{ return (m_option_flags & EMIT_NONROOT_DASH) != 0; }

◆ emit_nonroot_dash() [2/2]

EmitOptions & c4::yml::EmitOptions::emit_nonroot_dash ( bool enabled)
inlinenoexcept

Definition at line 86 of file emit.hpp.

86{ m_option_flags = (EmitOptionFlags_e)(enabled ? (m_option_flags | EMIT_NONROOT_DASH) : (m_option_flags & ~EMIT_NONROOT_DASH)); return *this; }

◆ indent_flow_ml() [1/2]

bool c4::yml::EmitOptions::indent_flow_ml ( ) const
inlinenoexcept

Definition at line 88 of file emit.hpp.

88{ return (m_option_flags & INDENT_FLOW_ML) != 0; }

◆ indent_flow_ml() [2/2]

EmitOptions & c4::yml::EmitOptions::indent_flow_ml ( bool enabled)
inlinenoexcept

Definition at line 89 of file emit.hpp.

89{ m_option_flags = (EmitOptionFlags_e)(enabled ? (m_option_flags | INDENT_FLOW_ML) : (m_option_flags & ~INDENT_FLOW_ML)); return *this; }

◆ json_error_flags() [1/2]

EmitOptionFlags_e c4::yml::EmitOptions::json_error_flags ( ) const
inlinenoexcept

Definition at line 91 of file emit.hpp.

91{ return (EmitOptionFlags_e)(m_option_flags & _JSON_ERR_MASK); }

◆ json_error_flags() [2/2]

EmitOptions & c4::yml::EmitOptions::json_error_flags ( EmitOptionFlags_e d)
inlinenoexcept

Definition at line 92 of file emit.hpp.

92{ m_option_flags = (EmitOptionFlags_e)(d & _JSON_ERR_MASK); return *this; }

◆ max_depth() [1/4]

id_type c4::yml::EmitOptions::max_depth ( ) const
inlinenoexcept

Definition at line 104 of file emit.hpp.

104{ return m_max_depth; }

◆ max_depth() [2/4]

EmitOptions & c4::yml::EmitOptions::max_depth ( id_type d)
inlinenoexcept

Definition at line 105 of file emit.hpp.

105{ m_max_depth = d; return *this; }

◆ operator==()

bool c4::yml::EmitOptions::operator== ( const EmitOptions & that) const
inlinenoexcept

Definition at line 111 of file emit.hpp.

112 {
113 return m_max_depth == that.m_max_depth &&
114 m_option_flags == that.m_option_flags;
115 }

◆ Emitter() [1/2]

template<class Writer>
template<class ... WriterArgs>
c4::yml::Emitter< Writer >::Emitter ( EmitOptions const & opts,
WriterArgs &&... args )
inline

Construct the emitter and its internal Writer state.

Parameters
optsEmitOptions
argsarguments to be forwarded to the constructor of the writer.

Definition at line 143 of file emit.hpp.

145 , m_tree()
146 , m_opts(opts)
147 , m_col()
148 , m_depth()
149 , m_ilevel()
150 , m_pws()
151 {}
A stateful emitter, for use with a writer such as WriterBuf, WriterFile, or WriterOStream.
Definition emit.hpp:134

◆ Emitter() [2/2]

template<class Writer>
template<class ... WriterArgs>
c4::yml::Emitter< Writer >::Emitter ( WriterArgs &&... args)
inline

Construct the emitter and its internal Writer state, using default emit options.

Parameters
argsarguments to be forwarded to the constructor of the writer.

Definition at line 157 of file emit.hpp.

159 , m_tree()
160 , m_opts()
161 , m_col()
162 , m_depth()
163 , m_ilevel()
164 , m_pws()
165 {}

◆ emit_as() [1/3]

template<class Writer>
substr c4::yml::Emitter< Writer >::emit_as ( EmitType_e type,
Tree const & t,
id_type id,
bool error_on_excess )

emit!

When writing to a buffer, returns a substr of the emitted YAML. If the given buffer has insufficient space, the returned substr will be null and its size will be the needed space. Whatever the size of the buffer, it is guaranteed that no writes are done past its end.

When writing to a file, the returned substr will be null, but its length will be set to the number of bytes written.

Parameters
typespecify what to emit (YAML or JSON)
tthe tree to emit
idthe id of the node to emit
error_on_excesswhen true, an error is raised when the output buffer is too small for the emitted YAML/JSON

Definition at line 20 of file emit.def.hpp.

21{
22 if(tree.empty())
23 {
24 _RYML_ASSERT_BASIC_(tree.callbacks(), id == NONE);
25 return {};
26 }
27 if(id == NONE)
28 id = tree.root_id();
29 _RYML_CHECK_VISIT_(tree.callbacks(), id < tree.capacity(), &tree, id);
30 m_tree = &tree;
31 m_col = 0;
32 m_depth = 0;
33 m_ilevel = 0;
35 if(type == EMIT_YAML)
36 _emit_yaml(id);
37 else if(type == EMIT_JSON)
38 _json_emit(id);
39 else
40 _RYML_ERR_BASIC_(m_tree->callbacks(), "unknown emit type"); // LCOV_EXCL_LINE
41 m_tree = nullptr;
42 return this->Writer::_get(error_on_excess);
43}

◆ emit_as() [2/3]

template<class Writer>
substr c4::yml::Emitter< Writer >::emit_as ( EmitType_e type,
Tree const & t,
bool error_on_excess = true )
inline

emit starting at the root node

Definition at line 188 of file emit.hpp.

189 {
190 if(t.empty())
191 return {};
192 return this->emit_as(type, t, t.root_id(), error_on_excess);
193 }
substr emit_as(EmitType_e type, Tree const &t, id_type id, bool error_on_excess)
emit!
Definition emit.def.hpp:20

◆ emit_as() [3/3]

template<class Writer>
substr c4::yml::Emitter< Writer >::emit_as ( EmitType_e type,
ConstNodeRef const & n,
bool error_on_excess = true )
inline

emit starting at the given node

Definition at line 195 of file emit.hpp.

196 {
197 if(!n.readable())
198 return {};
199 return this->emit_as(type, *n.tree(), n.id(), error_on_excess);
200 }

◆ options() [1/2]

template<class Writer>
EmitOptions const & c4::yml::Emitter< Writer >::options ( ) const
inlinenoexcept

get the emit options for this object

Definition at line 205 of file emit.hpp.

205{ return m_opts; }

◆ options() [2/2]

template<class Writer>
void c4::yml::Emitter< Writer >::options ( EmitOptions opts)
inlinenoexcept

set the emit options for this object

Definition at line 207 of file emit.hpp.

207{ m_opts = opts; }

◆ max_depth() [3/4]

template<class Writer>
void c4::yml::Emitter< Writer >::max_depth ( id_type max_depth)
inlinenoexcept

set the max depth for emitted trees (to prevent a stack overflow)

Definition at line 210 of file emit.hpp.

void max_depth(id_type max_depth) noexcept
set the max depth for emitted trees (to prevent a stack overflow)
Definition emit.hpp:210

◆ max_depth() [4/4]

template<class Writer>
id_type c4::yml::Emitter< Writer >::max_depth ( ) const
inlinenoexcept

get the max depth for emitted trees (to prevent a stack overflow)

Definition at line 212 of file emit.hpp.

212{ return m_opts.max_depth(); }

◆ as_json() [1/3]

c4::yml::as_json::as_json ( Tree const & t,
EmitOptions const & opts = {} )
inline

Definition at line 556 of file emit.hpp.

556{}) : tree(&t), node(t.empty() ? NONE : t.root_id()), options(opts) {}
Tree const * tree
Definition emit.hpp:553
EmitOptions options
Definition emit.hpp:555
id_type node
Definition emit.hpp:554
@ NONE
an index to none
Definition common.hpp:256

◆ as_json() [2/3]

c4::yml::as_json::as_json ( Tree const & t,
id_type id,
EmitOptions const & opts = {} )
inline

Definition at line 557 of file emit.hpp.

557{}) : tree(&t), node(id), options(opts) {}

◆ as_json() [3/3]

c4::yml::as_json::as_json ( ConstNodeRef const & n,
EmitOptions const & opts = {} )
inline

Definition at line 558 of file emit.hpp.

558{}) : tree(n.tree()), node(n.id()), options(opts) {}

◆ as_yaml() [1/3]

c4::yml::as_yaml::as_yaml ( Tree const & t,
EmitOptions const & opts = {} )
inline

Definition at line 577 of file emit.hpp.

577{}) : tree(&t), node(t.empty() ? NONE : t.root_id()), options(opts) {}
Tree const * tree
Definition emit.hpp:574
EmitOptions options
Definition emit.hpp:576
id_type node
Definition emit.hpp:575

◆ as_yaml() [2/3]

c4::yml::as_yaml::as_yaml ( Tree const & t,
id_type id,
EmitOptions const & opts = {} )
inline

Definition at line 578 of file emit.hpp.

578{}) : tree(&t), node(id), options(opts) {}

◆ as_yaml() [3/3]

c4::yml::as_yaml::as_yaml ( ConstNodeRef const & n,
EmitOptions const & opts = {} )
inline

Definition at line 579 of file emit.hpp.

579{}) : tree(n.tree()), node(n.id()), options(opts) {}

◆ emit_yaml() [1/6]

substr c4::yml::emit_yaml ( Tree const & t,
id_type id,
EmitOptions const & opts,
substr buf,
bool error_on_excess = true )
inline

(1) emit YAML to the given buffer.

Return a substr trimmed to the emitted YAML.

Parameters
tthe tree to emit.
idthe node where to start emitting.
optsemit options.
bufthe output buffer.
error_on_excessRaise an error if the space in the buffer is insufficient.
Returns
a substr trimmed to the result in the output buffer. If the buffer is insufficient (when error_on_excess is false), the string pointer of the result will be set to null, and the length will report the required buffer size.

Definition at line 625 of file emit.hpp.

626{
627 EmitterBuf em(opts, buf);
628 return em.emit_as(EMIT_YAML, t, id, error_on_excess);
629}
Emitter< WriterBuf > EmitterBuf
Definition emit.hpp:40
@ EMIT_YAML
emit YAML
Definition emit.hpp:49

◆ emit_yaml() [2/6]

substr c4::yml::emit_yaml ( Tree const & t,
id_type id,
substr buf,
bool error_on_excess = true )
inline

(2) like (1), but use default emit options

Definition at line 631 of file emit.hpp.

632{
633 EmitterBuf em(buf);
634 return em.emit_as(EMIT_YAML, t, id, error_on_excess);
635}

◆ emit_json() [1/6]

substr c4::yml::emit_json ( Tree const & t,
id_type id,
EmitOptions const & opts,
substr buf,
bool error_on_excess = true )
inline

(1) emit JSON to the given buffer.

Return a substr trimmed to the emitted JSON.

Parameters
tthe tree to emit.
idthe node where to start emitting.
optsemit options.
bufthe output buffer.
error_on_excessRaise an error if the space in the buffer is insufficient.
Returns
a substr trimmed to the result in the output buffer. If the buffer is insufficient (when error_on_excess is false), the string pointer of the result will be set to null, and the length will report the required buffer size.

Definition at line 645 of file emit.hpp.

646{
647 EmitterBuf em(opts, buf);
648 return em.emit_as(EMIT_JSON, t, id, error_on_excess);
649}
@ EMIT_JSON
emit JSON
Definition emit.hpp:50

◆ emit_json() [2/6]

substr c4::yml::emit_json ( Tree const & t,
id_type id,
substr buf,
bool error_on_excess = true )
inline

(2) like (1), but use default emit options

Definition at line 651 of file emit.hpp.

652{
653 EmitterBuf em(buf);
654 return em.emit_as(EMIT_JSON, t, id, error_on_excess);
655}

◆ emit_yaml() [3/6]

substr c4::yml::emit_yaml ( Tree const & t,
EmitOptions const & opts,
substr buf,
bool error_on_excess = true )
inline

(1) emit YAML to the given buffer.

Return a substr trimmed to the emitted YAML.

Parameters
tthe tree; will be emitted from the root node.
optsemit options.
bufthe output buffer.
error_on_excessRaise an error if the space in the buffer is insufficient.
Returns
a substr trimmed to the result in the output buffer. If the buffer is insufficient (when error_on_excess is false), the string pointer of the result will be set to null, and the length will report the required buffer size.

Definition at line 668 of file emit.hpp.

669{
670 EmitterBuf em(opts, buf);
671 return em.emit_as(EMIT_YAML, t, error_on_excess);
672}

◆ emit_yaml() [4/6]

substr c4::yml::emit_yaml ( Tree const & t,
substr buf,
bool error_on_excess = true )
inline

(2) like (1), but use default emit options

Definition at line 674 of file emit.hpp.

675{
676 EmitterBuf em(buf);
677 return em.emit_as(EMIT_YAML, t, error_on_excess);
678}

◆ emit_json() [3/6]

substr c4::yml::emit_json ( Tree const & t,
EmitOptions const & opts,
substr buf,
bool error_on_excess = true )
inline

(1) emit JSON to the given buffer.

Return a substr trimmed to the emitted JSON.

Parameters
tthe tree; will be emitted from the root node.
optsemit options.
bufthe output buffer.
error_on_excessRaise an error if the space in the buffer is insufficient.
Returns
a substr trimmed to the result in the output buffer. If the buffer is insufficient (when error_on_excess is false), the string pointer of the result will be set to null, and the length will report the required buffer size.

Definition at line 687 of file emit.hpp.

688{
689 EmitterBuf em(opts, buf);
690 return em.emit_as(EMIT_JSON, t, error_on_excess);
691}

◆ emit_json() [4/6]

substr c4::yml::emit_json ( Tree const & t,
substr buf,
bool error_on_excess = true )
inline

(2) like (1), but use default emit options

Definition at line 693 of file emit.hpp.

694{
695 EmitterBuf em(buf);
696 return em.emit_as(EMIT_JSON, t, error_on_excess);
697}

◆ emit_yaml() [5/6]

substr c4::yml::emit_yaml ( ConstNodeRef const & r,
EmitOptions const & opts,
substr buf,
bool error_on_excess = true )
inline

(1) emit YAML to the given buffer.

Return a substr trimmed to the emitted YAML.

Parameters
rthe starting node.
bufthe output buffer.
optsemit options.
error_on_excessRaise an error if the space in the buffer is insufficient.
Returns
a substr trimmed to the result in the output buffer. If the buffer is insufficient (when error_on_excess is false), the string pointer of the result will be set to null, and the length will report the required buffer size.

Definition at line 710 of file emit.hpp.

711{
712 if(!r.readable())
713 return {};
714 EmitterBuf em(opts, buf);
715 return em.emit_as(EMIT_YAML, r, error_on_excess);
716}

◆ emit_yaml() [6/6]

substr c4::yml::emit_yaml ( ConstNodeRef const & r,
substr buf,
bool error_on_excess = true )
inline

(2) like (1), but use default emit options

Definition at line 718 of file emit.hpp.

719{
720 if(!r.readable())
721 return {};
722 EmitterBuf em(buf);
723 return em.emit_as(EMIT_YAML, r, error_on_excess);
724}

◆ emit_json() [5/6]

substr c4::yml::emit_json ( ConstNodeRef const & r,
EmitOptions const & opts,
substr buf,
bool error_on_excess = true )
inline

(1) emit JSON to the given buffer.

Return a substr trimmed to the emitted JSON.

Parameters
rthe starting node.
bufthe output buffer.
optsemit options.
error_on_excessRaise an error if the space in the buffer is insufficient.
Returns
a substr trimmed to the result in the output buffer. If the buffer is insufficient (when error_on_excess is false), the string pointer of the result will be set to null, and the length will report the required buffer size.

Definition at line 733 of file emit.hpp.

734{
735 if(!r.readable())
736 return {};
737 EmitterBuf em(opts, buf);
738 return em.emit_as(EMIT_JSON, r, error_on_excess);
739}

◆ emit_json() [6/6]

substr c4::yml::emit_json ( ConstNodeRef const & r,
substr buf,
bool error_on_excess = true )
inline

(2) like (1), but use default emit options

Definition at line 741 of file emit.hpp.

742{
743 if(!r.readable())
744 return {};
745 EmitterBuf em(buf);
746 return em.emit_as(EMIT_JSON, r, error_on_excess);
747}

Variable Documentation

◆ max_depth_default

const id_type c4::yml::EmitOptions::max_depth_default = 64
staticconstexpr

Definition at line 106 of file emit.hpp.

◆ tree [1/2]

Tree const* c4::yml::as_json::tree

Definition at line 553 of file emit.hpp.

◆ node [1/2]

id_type c4::yml::as_json::node

Definition at line 554 of file emit.hpp.

◆ options [1/2]

EmitOptions c4::yml::as_json::options

Definition at line 555 of file emit.hpp.

◆ tree [2/2]

Tree const* c4::yml::as_yaml::tree

Definition at line 574 of file emit.hpp.

◆ node [2/2]

id_type c4::yml::as_yaml::node

Definition at line 575 of file emit.hpp.

◆ options [2/2]

EmitOptions c4::yml::as_yaml::options

Definition at line 576 of file emit.hpp.