rapidyaml 0.15.1
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Serialization helpers

Classes

struct  c4::yml::Key< K >

Functions

template<class K>
Key< K > c4::yml::key (K &&k)
template<class T>
void c4::yml::write (NodeRef *n, T const &v)
template<class T>
bool c4::yml::read (ConstNodeRef const &n, T *v)
template<class T>
bool c4::yml::read (ConstNodeRef const &n, T const &wrapper)
template<class T>
bool c4::yml::read (NodeRef const &n, T *v)
template<class T>
bool c4::yml::read (NodeRef const &n, T const &wrapper)
template<class T>
bool c4::yml::readkey (ConstNodeRef const &n, T *v)
template<class T>
bool c4::yml::readkey (ConstNodeRef const &n, T const &wrapper)
template<class T>
bool c4::yml::readkey (NodeRef const &n, T *v)
template<class T>
bool c4::yml::readkey (NodeRef const &n, T const &wrapper)
template<class T>
bool c4::yml::read (Tree const *tree, id_type id, T const &wrapper)
template<class T>
bool c4::yml::readkey (Tree const *tree, id_type id, T const &wrapper)
template<class T>
auto c4::yml::read (Tree const *tree, id_type id, T *v) -> typename std::enable_if<!std::is_arithmetic< T >::value, bool >::type
 convert the val of a scalar node to a particular non-arithmetic non-float type, by forwarding its val to from_chars<T>().
template<class T>
auto c4::yml::readkey (Tree const *tree, id_type id, T *v) -> typename std::enable_if<!std::is_arithmetic< T >::value, bool >::type
 convert the key of a node to a particular non-arithmetic non-float type, by forwarding its key to from_chars<T>().
template<class T>
auto c4::yml::read (Tree const *tree, id_type id, T *v) -> typename std::enable_if< std::is_arithmetic< T >::value &&!std::is_floating_point< T >::value, bool >::type
 convert the val of a scalar node to a particular arithmetic integral non-float type, by forwarding its val to from_chars<T>().
template<class T>
auto c4::yml::readkey (Tree const *tree, id_type id, T *v) -> typename std::enable_if< std::is_arithmetic< T >::value &&!std::is_floating_point< T >::value, bool >::type
 convert the key of a node to a particular arithmetic integral non-float type, by forwarding its val to from_chars<T>().
template<class T>
size_t c4::yml::to_chars_float (substr buf, T val)
 encode a floating point value to a string.
template<class T>
bool c4::yml::from_chars_float (csubstr buf, T *val)
 decode a floating point from string.
template<class T>
std::enable_if< std::is_floating_point< T >::value, bool >::type c4::yml::read (Tree const *tree, id_type id, T *v)
 convert the val of a scalar node to a floating point type, by forwarding its val to from_chars_float<T>().
template<class T>
std::enable_if< std::is_floating_point< T >::value, bool >::type c4::yml::readkey (Tree const *tree, id_type id, T *v)
 convert the key of a scalar node to a floating point type, by forwarding its key to from_chars_float<T>().
template<class T>
csubstr c4::yml::serialize_to_arena (Tree *tree, T const &a)

Detailed Description

Function Documentation

◆ key()

template<class K>
Key< K > c4::yml::key ( K && k)
inline

Definition at line 43 of file node.hpp.

43{ return Key<K>{std::forward<K>(k)}; }

◆ write()

template<class T>
void c4::yml::write ( NodeRef * n,
T const & v )
inline

Definition at line 1588 of file node.hpp.

1589{
1590 n->set_val_serialized(v);
1591}
size_t set_val_serialized(T const &v)
Definition node.hpp:1239

◆ read() [1/8]

template<class T>
bool c4::yml::read ( ConstNodeRef const & n,
T * v )
inline

Definition at line 1594 of file node.hpp.

1595{
1596 return read(n.m_tree, n.m_id, v);
1597}
bool read(ConstNodeRef const &n, T *v)
Definition node.hpp:1594

◆ read() [2/8]

template<class T>
bool c4::yml::read ( ConstNodeRef const & n,
T const & wrapper )
inline

Definition at line 1599 of file node.hpp.

1600{
1601 return read(n.m_tree, n.m_id, wrapper);
1602}

◆ read() [3/8]

template<class T>
bool c4::yml::read ( NodeRef const & n,
T * v )
inline

Definition at line 1605 of file node.hpp.

1606{
1607 return read(n.tree(), n.id(), v);
1608}

◆ read() [4/8]

template<class T>
bool c4::yml::read ( NodeRef const & n,
T const & wrapper )
inline

Definition at line 1610 of file node.hpp.

1611{
1612 return read(n.tree(), n.id(), wrapper);
1613}

◆ readkey() [1/8]

template<class T>
bool c4::yml::readkey ( ConstNodeRef const & n,
T * v )
inline

Definition at line 1616 of file node.hpp.

1617{
1618 return readkey(n.m_tree, n.m_id, v);
1619}
bool readkey(ConstNodeRef const &n, T *v)
Definition node.hpp:1616

◆ readkey() [2/8]

template<class T>
bool c4::yml::readkey ( ConstNodeRef const & n,
T const & wrapper )
inline

Definition at line 1621 of file node.hpp.

1622{
1623 return readkey(n.m_tree, n.m_id, wrapper);
1624}

◆ readkey() [3/8]

template<class T>
bool c4::yml::readkey ( NodeRef const & n,
T * v )
inline

Definition at line 1627 of file node.hpp.

1628{
1629 return readkey(n.tree(), n.id(), v);
1630}

◆ readkey() [4/8]

template<class T>
bool c4::yml::readkey ( NodeRef const & n,
T const & wrapper )
inline

Definition at line 1632 of file node.hpp.

1633{
1634 return readkey(n.tree(), n.id(), wrapper);
1635}

◆ read() [5/8]

template<class T>
bool c4::yml::read ( Tree const * tree,
id_type id,
T const & wrapper )

Definition at line 1367 of file tree.hpp.

1368{
1369 return C4_LIKELY(!(tree->type(id) & VALNIL)) ? from_chars(tree->val(id), wrapper) : false;
1370}
bool from_chars(csubstr buf, uint8_t *v) noexcept
@ VALNIL
the val is null (eg {a : } results in a null val)
Definition node_type.hpp:50

◆ readkey() [5/8]

template<class T>
bool c4::yml::readkey ( Tree const * tree,
id_type id,
T const & wrapper )

Definition at line 1372 of file tree.hpp.

1373{
1374 return C4_LIKELY(!(tree->type(id) & KEYNIL)) ? from_chars(tree->key(id), wrapper) : false;
1375}
@ KEYNIL
the key is null (eg { : b} results in a null key)
Definition node_type.hpp:49

◆ read() [6/8]

template<class T>
auto c4::yml::read ( Tree const * tree,
id_type id,
T * v )->typenamestd::enable_if<!std::is_arithmetic< T >::value, bool >::type
inline

convert the val of a scalar node to a particular non-arithmetic non-float type, by forwarding its val to from_chars<T>().

The full string is used.

Returns
false if the conversion failed, or if the key was empty and unquoted

Definition at line 1386 of file tree.hpp.

1388{
1389 return C4_LIKELY(!(tree->type(id) & VALNIL)) ? from_chars(tree->val(id), v) : false;
1390}

◆ readkey() [6/8]

template<class T>
auto c4::yml::readkey ( Tree const * tree,
id_type id,
T * v )->typenamestd::enable_if<!std::is_arithmetic< T >::value, bool >::type
inline

convert the key of a node to a particular non-arithmetic non-float type, by forwarding its key to from_chars<T>().

The full string is used.

Returns
false if the conversion failed, or if the key was empty and unquoted

Definition at line 1397 of file tree.hpp.

1399{
1400 return C4_LIKELY(!(tree->type(id) & KEYNIL)) ? from_chars(tree->key(id), v) : false;
1401}

◆ read() [7/8]

template<class T>
auto c4::yml::read ( Tree const * tree,
id_type id,
T * v )->typenamestd::enable_if< std::is_arithmetic< T >::value &&!std::is_floating_point< T >::value, bool >::type
inline

convert the val of a scalar node to a particular arithmetic integral non-float type, by forwarding its val to from_chars<T>().

The full string is used.

Returns
false if the conversion failed

Definition at line 1412 of file tree.hpp.

1414{
1415 using U = typename std::remove_cv<T>::type;
1416 enum { ischar = std::is_same<char, U>::value || std::is_same<signed char, U>::value || std::is_same<unsigned char, U>::value }; // NOLINT
1417 csubstr val = tree->val(id);
1418 NodeType ty = tree->type(id);
1419 if(C4_UNLIKELY((ty & VALNIL) || val.empty()))
1420 return false;
1421 // quote integral numbers if they have a leading 0
1422 // https://github.com/biojppm/rapidyaml/issues/291
1423 char first = val.str[0];
1424 if(ty.is_val_quoted() && (first != '0' && !ischar))
1425 return false;
1426 else if(first == '+')
1427 val = val.sub(1);
1428 return from_chars(val, v);
1429}
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503
bool empty() const noexcept
Definition substr.hpp:356
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ readkey() [7/8]

template<class T>
auto c4::yml::readkey ( Tree const * tree,
id_type id,
T * v )->typenamestd::enable_if< std::is_arithmetic< T >::value &&!std::is_floating_point< T >::value, bool >::type
inline

convert the key of a node to a particular arithmetic integral non-float type, by forwarding its val to from_chars<T>().

The full string is used.

Returns
false if the conversion failed

Definition at line 1437 of file tree.hpp.

1439{
1440 using U = typename std::remove_cv<T>::type;
1441 enum { ischar = std::is_same<char, U>::value || std::is_same<signed char, U>::value || std::is_same<unsigned char, U>::value }; // NOLINT
1442 csubstr key = tree->key(id);
1443 NodeType ty = tree->type(id);
1444 if((ty & KEYNIL) || key.empty())
1445 return false;
1446 // quote integral numbers if they have a leading 0
1447 // https://github.com/biojppm/rapidyaml/issues/291
1448 char first = key.str[0];
1449 if(ty.is_key_quoted() && (first != '0' && !ischar))
1450 return false;
1451 else if(first == '+')
1452 key = key.sub(1);
1453 return from_chars(key, v);
1454}
Key< K > key(K &&k)
Definition node.hpp:43

◆ to_chars_float()

template<class T>
size_t c4::yml::to_chars_float ( substr buf,
T val )

encode a floating point value to a string.

Definition at line 1461 of file tree.hpp.

1462{
1463 static_assert(std::is_floating_point<T>::value, "must be floating point");
1464 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wfloat-equal")
1465 if(C4_UNLIKELY(std::isnan(val)))
1466 return to_chars(buf, csubstr(".nan"));
1467 else if(C4_UNLIKELY(val == std::numeric_limits<T>::infinity()))
1468 return to_chars(buf, csubstr(".inf"));
1469 else if(C4_UNLIKELY(val == -std::numeric_limits<T>::infinity()))
1470 return to_chars(buf, csubstr("-.inf"));
1471 return to_chars(buf, val);
1472 C4_SUPPRESS_WARNING_GCC_CLANG_POP
1473}
size_t to_chars(substr buf, escaped_scalar e)
formatting implementation to escape a scalar with escape_scalar()

◆ from_chars_float()

template<class T>
bool c4::yml::from_chars_float ( csubstr buf,
T * val )

decode a floating point from string.

Accepts special values: .nan, .inf, -.inf

Definition at line 1479 of file tree.hpp.

1480{
1481 static_assert(std::is_floating_point<T>::value, "must be floating point");
1482 if(buf.begins_with('+'))
1483 {
1484 buf = buf.sub(1);
1485 }
1486 if(C4_LIKELY(from_chars(buf, val)))
1487 {
1488 return true;
1489 }
1490 else if(C4_UNLIKELY(buf == ".nan" || buf == ".NaN" || buf == ".NAN"))
1491 {
1492 *val = std::numeric_limits<T>::quiet_NaN();
1493 return true;
1494 }
1495 else if(C4_UNLIKELY(buf == ".inf" || buf == ".Inf" || buf == ".INF"))
1496 {
1497 *val = std::numeric_limits<T>::infinity();
1498 return true;
1499 }
1500 else if(C4_UNLIKELY(buf == "-.inf" || buf == "-.Inf" || buf == "-.INF"))
1501 {
1502 *val = -std::numeric_limits<T>::infinity();
1503 return true;
1504 }
1505 else
1506 {
1507 return false;
1508 }
1509}
bool begins_with(const C c) const noexcept
true if the first character of the string is c
Definition substr.hpp:851

◆ read() [8/8]

template<class T>
std::enable_if< std::is_floating_point< T >::value, bool >::type c4::yml::read ( Tree const * tree,
id_type id,
T * v )
inline

convert the val of a scalar node to a floating point type, by forwarding its val to from_chars_float<T>().

Returns
false if the conversion failed
Warning
Unlike non-floating types, only the leading part of the string that may constitute a number is processed. This happens because the float parsing is delegated to fast_float, which is implemented that way. Consequently, for example, all of "34", "34 " "34hg" "34 gh" will be read as 34. If you are not sure about the contents of the data, you can use csubstr::first_real_span() to check before calling >>, for example like this:
csubstr val = node.val();
if(val.first_real_span() == val)
node >> v;
else
ERROR("not a real")
basic_substring first_real_span() const
get the first span which can be interpreted as a real (floating-point) number
Definition substr.hpp:1365

Definition at line 1535 of file tree.hpp.

1536{
1537 csubstr val = tree->val(id);
1538 return C4_LIKELY(!val.empty()) ? from_chars_float(val, v) : false;
1539}
bool from_chars_float(csubstr buf, T *val)
decode a floating point from string.
Definition tree.hpp:1479

◆ readkey() [8/8]

template<class T>
std::enable_if< std::is_floating_point< T >::value, bool >::type c4::yml::readkey ( Tree const * tree,
id_type id,
T * v )
inline

convert the key of a scalar node to a floating point type, by forwarding its key to from_chars_float<T>().

Returns
false if the conversion failed
Warning
Unlike non-floating types, only the leading part of the string that may constitute a number is processed. This happens because the float parsing is delegated to fast_float, which is implemented that way. Consequently, for example, all of "34", "34 " "34hg" "34 gh" will be read as 34. If you are not sure about the contents of the data, you can use csubstr::first_real_span() to check before calling >>, for example like this:
csubstr key = node.key();
if(key.first_real_span() == key)
node >> v;
else
ERROR("not a real")

Definition at line 1565 of file tree.hpp.

1566{
1567 csubstr key = tree->key(id);
1568 return C4_LIKELY(!key.empty()) ? from_chars_float(key, v) : false;
1569}

◆ serialize_to_arena()

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

Definition at line 1575 of file tree.hpp.

1576{
1577 substr rem(tree->m_arena.sub(tree->m_arena_pos));
1578 size_t num = serialize_scalar(rem, a);
1579 if(num > rem.len)
1580 {
1581 rem = tree->_grow_arena(num);
1582 num = serialize_scalar(rem, a);
1583 _RYML_ASSERT_VISIT_(tree->m_callbacks, num <= rem.len, tree, NONE);
1584 }
1585 rem = tree->_request_span(num);
1586 return rem;
1587}
size_t m_arena_pos
Definition tree.hpp:1344
substr m_arena
Definition tree.hpp:1343
Callbacks m_callbacks
Definition tree.hpp:1346
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
@ NONE
an index to none
Definition common.hpp:263
auto serialize_scalar(substr buf, T const &a) -> typename std::enable_if< std::is_floating_point< T >::value, size_t >::type
Definition tree.hpp:66