rapidyaml  0.16.0
parseandemitYAML,anddoitfast

Implementation utils for serializing scalars in a tree's arena. More...

Functions

template<class T >
csubstr c4::yml::serialize_to_arena_scalar (Tree *tree, T const &scalar)
 Serialize a scalar to the tree's arena. More...
 
csubstr c4::yml::serialize_to_arena_str (Tree *tree, csubstr scalar)
 Serialize a string type (as specified by c4::is_string) to a tree's arena, ensuring that there is an entry for the string in the arena even if the string is empty. More...
 
template<class T >
csubstr c4::yml::serialize_to_arena (Tree *tree, T const &scalar)
 Serialize a scalar to a tree's arena, dispatching to either serialize_to_arena_scalar() or serialize_to_arena_str() when the type is a string according to c4::is_string. More...
 
csubstr c4::yml::serialize_to_arena (Tree *, std::nullptr_t) noexcept
 implementation for null values More...
 

Detailed Description

Implementation utils for serializing scalars in a tree's arena.

These functions are implementation helpers used by Tree::to_arena() and NodeRef::to_arena() to grow the tree arena as needed and serialize scalars in it. There should be no reason to call these functions directly.

They can be overriden by the user to customize behavior for a user type, but generally there should be no reason to do so – if you think there is, please open an issue in github.

Function Documentation

template<class T >
csubstr c4::yml::serialize_to_arena_scalar ( Tree *  tree,
T const &  a 
)

Serialize a scalar to the tree's arena.

Forward to scalar_serialize(), giving it the tree's arena and resizing the arena as needed to fit the result.

This is an implementation helper for serialize_to_arena(), serializing through scalar_serialize().

Definition at line 1754 of file tree.hpp.

1755 {
1756  substr buf = tree->arena_rem(); // buffer: the free part of the tree's arenra.
1757  again:
1758  size_t num = scalar_serialize(buf, a); // try to write into it
1759  if C4_LIKELY(num <= buf.len) // was it enough?
1760  {
1761  buf = buf.first(num); // fit the payload
1762  }
1763  else
1764  {
1765  buf = tree->_grow_arena(num); // does not advance pos
1766  goto again; // NOLINT
1767  }
1768  tree->m_arena_pos += num;
1769  return buf;
1770 }
size_t scalar_serialize(substr buf, T const &a)
Serialize a scalar to the buffer, dispatching to to_chars() or to_chars_float() as appropriate...
basic_substring< char > substr
a mutable string view
Definition: substr.hpp:2355
csubstr c4::yml::serialize_to_arena_str ( Tree *  tree,
csubstr  scalar 
)

Serialize a string type (as specified by c4::is_string) to a tree's arena, ensuring that there is an entry for the string in the arena even if the string is empty.

This is an implementation helper for serialize_to_arena(), serializing through scalar_serialize() and then ensuring that the serialized string will be placed in the arena, even if the string is zero-length.

Definition at line 27 of file tree.cpp.

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

28 {
29  if(scalar.len > 0)
30  {
31  return serialize_to_arena_scalar<csubstr>(tree, scalar);
32  }
33  else
34  {
35  if(scalar.str == nullptr)
36  {
37  return csubstr{};
38  }
39  else if(tree->m_arena.str == nullptr)
40  {
41  // Arena is empty and we want to store a non-null
42  // zero-length string.
43  // Even though the string has zero length, we need
44  // some "memory" to store a non-nullptr string
45  tree->_grow_arena(1);
46  }
47  return tree->_request_span(0);
48  }
49 }
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
template<class T >
csubstr c4::yml::serialize_to_arena ( Tree *  tree,
T const &  scalar 
)
inline

Serialize a scalar to a tree's arena, dispatching to either serialize_to_arena_scalar() or serialize_to_arena_str() when the type is a string according to c4::is_string.

This is the entry point for customizing how a scalar is serialized to a tree's arena. It is never needed for the user to call this function, and generally there is no reason for overriding this function for user types, unless it has specific requirements for the tree's arena, as happens for example with string types. For user string types, defining c4::is_string is enough. For example:

Note
When using a standard older than C++17, if constexpr is not available, and the implementation reverts to SFINAE to achieve the compile-time dispatch.
{c++}
namespace foo {
class MyStringType {...}; // an example of a user-defined string type
// define conversion to/from substrings
c4::yml::csubstr to_csubstr(MyStringType const& s) { return ...; }
c4::yml::substr to_substr(MyStringType & s) { return ...; }
} // namespace foo
// tell ryml to treat this type as a string
template<> struct c4::is_string<foo::MyStringType> : std::true_type {};

Definition at line 109 of file tree.hpp.

110 {
111  if constexpr (_is_string_nocvref<T>::value)
112  return serialize_to_arena_str(tree, to_csubstr(scalar));
113  else
114  return serialize_to_arena_scalar<T>(tree, scalar);
115 }
csubstr serialize_to_arena_scalar(Tree *tree, T const &scalar)
Serialize a scalar to the tree's arena.
Definition: tree.hpp:1754
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition: substr.hpp:2380
csubstr serialize_to_arena_str(Tree *tree, csubstr scalar)
Serialize a string type (as specified by c4::is_string) to a tree's arena, ensuring that there is an ...
Definition: tree.cpp:27
csubstr c4::yml::serialize_to_arena ( Tree *  ,
std::nullptr_t   
)
inlinenoexcept

implementation for null values

Definition at line 136 of file tree.hpp.

Referenced by c4::yml::Tree::to_arena(), c4::yml::write(), and c4::yml::write_key().

137 {
138  return csubstr{};
139 }
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356