rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches

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.
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.
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.
csubstr c4::yml::serialize_to_arena (Tree *, std::nullptr_t) noexcept
 implementation for null values

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

◆ serialize_to_arena_scalar()

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

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 m_arena_pos
Definition tree.hpp:1684
csubstr arena_rem() const
get the free space at the end of the arena
Definition tree.hpp:1322
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
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
Definition substr.hpp:529

Referenced by serialize_to_arena(), serialize_to_arena_scalar(), and serialize_to_arena_str().

◆ serialize_to_arena_str()

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.

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}
substr m_arena
Definition tree.hpp:1683
csubstr serialize_to_arena_scalar(Tree *tree, T const &scalar)
Serialize a scalar to the tree's arena.
Definition tree.hpp:1754
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

Referenced by serialize_to_arena(), and serialize_to_arena_str().

◆ serialize_to_arena() [1/2]

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.
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 {};
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2380

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_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

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

◆ serialize_to_arena() [2/2]

csubstr c4::yml::serialize_to_arena ( Tree * ,
std::nullptr_t  )
inlinenoexcept

implementation for null values

Definition at line 136 of file tree.hpp.

137{
138 return csubstr{};
139}