rapidyaml  0.9.0
parse and emit YAML, and do it fast
Emit to memory buffer

Modules

 Emit to resizeable container
 

Enumerations

enum  c4::yml::EmitOptions::EmitOptionFlags_e : uint32_t { c4::yml::EmitOptions::DEFAULT_FLAGS = 0u , c4::yml::EmitOptions::JSON_ERR_ON_TAG = 1u << 0u , c4::yml::EmitOptions::JSON_ERR_ON_ANCHOR = 1u << 1u , c4::yml::EmitOptions::_JSON_ERR_MASK = JSON_ERR_ON_TAG|JSON_ERR_ON_ANCHOR }
 

Functions

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

Variables

Tree const * c4::yml::as_json::tree
 
size_t c4::yml::as_json::node
 
EmitOptions c4::yml::as_json::options
 
Tree const * c4::yml::as_yaml::tree
 
size_t 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

EmitOptionFlags_e c4::yml::EmitOptions::json_error_flags () const noexcept
 
EmitOptionsc4::yml::EmitOptions::json_error_flags (EmitOptionFlags_e d) noexcept
 

Detailed Description

Enumeration Type Documentation

◆ EmitOptionFlags_e

Enumerator
DEFAULT_FLAGS 
JSON_ERR_ON_TAG 
JSON_ERR_ON_ANCHOR 
_JSON_ERR_MASK 

Definition at line 64 of file emit.hpp.

64  : uint32_t {
65  DEFAULT_FLAGS = 0u,
66  JSON_ERR_ON_TAG = 1u << 0u,
67  JSON_ERR_ON_ANCHOR = 1u << 1u,

Function Documentation

◆ is_set_()

bool c4::yml::detail::is_set_ ( ConstNodeRef  n)
inline

◆ json_error_flags() [1/2]

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

Definition at line 76 of file emit.hpp.

76 { return m_option_flags; }

◆ json_error_flags() [2/2]

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

Definition at line 77 of file emit.hpp.

77 { 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 88 of file emit.hpp.

88 { return m_max_depth; }

◆ max_depth() [2/4]

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

Definition at line 89 of file emit.hpp.

89 { m_max_depth = d; return *this; }

◆ operator==()

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

Definition at line 95 of file emit.hpp.

96  {
97  return m_max_depth == that.m_max_depth &&
98  m_option_flags == that.m_option_flags;
99  }

◆ Emitter() [1/2]

template<class Writer >
template<class ... Args>
c4::yml::Emitter< Writer >::Emitter ( Args &&...  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 125 of file emit.hpp.

125 : Writer(std::forward<Args>(args)...), m_tree(), m_opts(), m_flow(false) {}

◆ Emitter() [2/2]

template<class Writer >
template<class ... Args>
c4::yml::Emitter< Writer >::Emitter ( EmitOptions const &  opts,
Args &&...  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 133 of file emit.hpp.

133 : Writer(std::forward<Args>(args)...), m_tree(), m_opts(opts), m_flow(false) {}

◆ 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
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 17 of file emit.def.hpp.

18 {
19  if(t.empty())
20  {
21  _RYML_CB_ASSERT(t.callbacks(), id == NONE);
22  return {};
23  }
24  if(id == NONE)
25  id = t.root_id();
26  _RYML_CB_CHECK(t.callbacks(), id < t.capacity());
27  m_tree = &t;
28  m_flow = false;
29  if(type == EMIT_YAML)
30  _emit_yaml(id);
31  else if(type == EMIT_JSON)
32  _do_visit_json(id, 0);
33  else
34  _RYML_CB_ERR(m_tree->callbacks(), "unknown emit type");
35  m_tree = nullptr;
36  return this->Writer::_get(error_on_excess);
37 }
Callbacks const & callbacks() const
Definition: tree.hpp:241
@ EMIT_YAML
emit YAML
Definition: emit.hpp:52
@ EMIT_JSON
emit JSON
Definition: emit.hpp:53

References c4::yml::Tree::callbacks(), c4::yml::Tree::capacity(), c4::yml::EMIT_JSON, c4::yml::EMIT_YAML, c4::yml::Tree::empty(), c4::yml::NONE, and c4::yml::Tree::root_id().

Referenced by c4::yml::emit_json(), c4::yml::emit_yaml(), and c4::yml::operator<<().

◆ 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 154 of file emit.hpp.

155  {
156  if(t.empty())
157  return {};
158  return this->emit_as(type, t, t.root_id(), error_on_excess);
159  }
substr emit_as(EmitType_e type, Tree const &t, id_type id, bool error_on_excess)
emit!
Definition: emit.def.hpp:17

References c4::yml::Tree::empty(), and c4::yml::Tree::root_id().

◆ 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 161 of file emit.hpp.

162  {
163  if(!detail::is_set_(n))
164  return {};
165  _RYML_CB_CHECK(n.tree()->callbacks(), n.readable());
166  return this->emit_as(type, *n.tree(), n.id(), error_on_excess);
167  }
bool is_set_(ConstNodeRef n)
Definition: emit.hpp:42

References c4::yml::Tree::callbacks(), c4::yml::ConstNodeRef::id(), c4::yml::detail::is_set_(), c4::yml::ConstNodeRef::readable(), and c4::yml::ConstNodeRef::tree().

◆ options()

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

get the emit options for this object

Definition at line 172 of file emit.hpp.

172 { return m_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 175 of file emit.hpp.

175 { m_opts.max_depth(max_depth); }
id_type max_depth() const noexcept
get the max depth for emitted trees (to prevent a stack overflow)
Definition: emit.hpp:177
id_type max_depth() const noexcept
Definition: emit.hpp:88

◆ 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 177 of file emit.hpp.

177 { 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 397 of file emit.hpp.

397 {}) : tree(&t), node(t.empty() ? NONE : t.root_id()), options(opts) {}
Tree const * tree
Definition: emit.hpp:394
EmitOptions options
Definition: emit.hpp:396
size_t node
Definition: emit.hpp:395

◆ as_json() [2/3]

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

Definition at line 398 of file emit.hpp.

398 {}) : 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 399 of file emit.hpp.

399 {}) : 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 418 of file emit.hpp.

418 {}) : tree(&t), node(t.empty() ? NONE : t.root_id()), options(opts) {}
Tree const * tree
Definition: emit.hpp:415
size_t node
Definition: emit.hpp:416
EmitOptions options
Definition: emit.hpp:417

◆ as_yaml() [2/3]

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

Definition at line 419 of file emit.hpp.

419 {}) : 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 420 of file emit.hpp.

420 {}) : 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 466 of file emit.hpp.

467 {
468  EmitterBuf em(opts, buf);
469  return em.emit_as(EMIT_YAML, t, id, error_on_excess);
470 }
Emitter< WriterBuf > EmitterBuf
Definition: emit.hpp:39

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_YAML.

◆ 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 472 of file emit.hpp.

473 {
474  EmitterBuf em(buf);
475  return em.emit_as(EMIT_YAML, t, id, error_on_excess);
476 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_YAML.

◆ 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 486 of file emit.hpp.

487 {
488  EmitterBuf em(opts, buf);
489  return em.emit_as(EMIT_JSON, t, id, error_on_excess);
490 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_JSON.

◆ 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 492 of file emit.hpp.

493 {
494  EmitterBuf em(buf);
495  return em.emit_as(EMIT_JSON, t, id, error_on_excess);
496 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_JSON.

◆ 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 509 of file emit.hpp.

510 {
511  EmitterBuf em(opts, buf);
512  return em.emit_as(EMIT_YAML, t, error_on_excess);
513 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_YAML.

◆ 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 515 of file emit.hpp.

516 {
517  EmitterBuf em(buf);
518  return em.emit_as(EMIT_YAML, t, error_on_excess);
519 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_YAML.

◆ 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 528 of file emit.hpp.

529 {
530  EmitterBuf em(opts, buf);
531  return em.emit_as(EMIT_JSON, t, error_on_excess);
532 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_JSON.

◆ 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 534 of file emit.hpp.

535 {
536  EmitterBuf em(buf);
537  return em.emit_as(EMIT_JSON, t, error_on_excess);
538 }

References c4::yml::Emitter< Writer >::emit_as(), and c4::yml::EMIT_JSON.

◆ 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 551 of file emit.hpp.

552 {
553  if(!detail::is_set_(r))
554  return {};
555  EmitterBuf em(opts, buf);
556  return em.emit_as(EMIT_YAML, r, error_on_excess);
557 }

References c4::yml::Emitter< Writer >::emit_as(), c4::yml::EMIT_YAML, and c4::yml::detail::is_set_().

◆ 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 559 of file emit.hpp.

560 {
561  if(!detail::is_set_(r))
562  return {};
563  EmitterBuf em(buf);
564  return em.emit_as(EMIT_YAML, r, error_on_excess);
565 }

References c4::yml::Emitter< Writer >::emit_as(), c4::yml::EMIT_YAML, and c4::yml::detail::is_set_().

Referenced by c4::yml::emitrs_yaml().

◆ 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 574 of file emit.hpp.

575 {
576  if(!detail::is_set_(r))
577  return {};
578  EmitterBuf em(opts, buf);
579  return em.emit_as(EMIT_JSON, r, error_on_excess);
580 }

References c4::yml::Emitter< Writer >::emit_as(), c4::yml::EMIT_JSON, and c4::yml::detail::is_set_().

◆ 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 582 of file emit.hpp.

583 {
584  if(!detail::is_set_(r))
585  return {};
586  EmitterBuf em(buf);
587  return em.emit_as(EMIT_JSON, r, error_on_excess);
588 }

References c4::yml::Emitter< Writer >::emit_as(), c4::yml::EMIT_JSON, and c4::yml::detail::is_set_().

Referenced by c4::yml::emitrs_json().

Variable Documentation

◆ max_depth_default

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

Definition at line 90 of file emit.hpp.

◆ tree [1/2]

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

Definition at line 394 of file emit.hpp.

Referenced by c4::yml::operator<<().

◆ node [1/2]

size_t c4::yml::as_json::node

Definition at line 395 of file emit.hpp.

Referenced by c4::yml::operator<<().

◆ options [1/2]

EmitOptions c4::yml::as_json::options

Definition at line 396 of file emit.hpp.

Referenced by c4::yml::operator<<().

◆ tree [2/2]

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

Definition at line 415 of file emit.hpp.

Referenced by c4::yml::operator<<().

◆ node [2/2]

size_t c4::yml::as_yaml::node

Definition at line 416 of file emit.hpp.

Referenced by c4::yml::operator<<().

◆ options [2/2]

EmitOptions c4::yml::as_yaml::options

Definition at line 417 of file emit.hpp.

Referenced by c4::yml::operator<<().