rapidyaml 0.14.0
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 1583 of file node.hpp.

1584{
1585 n->set_val_serialized(v);
1586}
size_t set_val_serialized(T const &v)
Definition node.hpp:1234

◆ read() [1/8]

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

Definition at line 1589 of file node.hpp.

1590{
1591 return read(n.m_tree, n.m_id, v);
1592}
bool read(ConstNodeRef const &n, T *v)
Definition node.hpp:1589

◆ read() [2/8]

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

Definition at line 1594 of file node.hpp.

1595{
1596 return read(n.m_tree, n.m_id, wrapper);
1597}

◆ read() [3/8]

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

Definition at line 1600 of file node.hpp.

1601{
1602 return read(n.tree(), n.id(), v);
1603}

◆ read() [4/8]

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

Definition at line 1605 of file node.hpp.

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

◆ readkey() [1/8]

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

Definition at line 1611 of file node.hpp.

1612{
1613 return readkey(n.m_tree, n.m_id, v);
1614}
bool readkey(ConstNodeRef const &n, T *v)
Definition node.hpp:1611

◆ readkey() [2/8]

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

Definition at line 1616 of file node.hpp.

1617{
1618 return readkey(n.m_tree, n.m_id, wrapper);
1619}

◆ readkey() [3/8]

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

Definition at line 1622 of file node.hpp.

1623{
1624 return readkey(n.tree(), n.id(), v);
1625}

◆ readkey() [4/8]

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

Definition at line 1627 of file node.hpp.

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

◆ read() [5/8]

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

Definition at line 1362 of file tree.hpp.

1363{
1364 return C4_LIKELY(!(tree->type(id) & VALNIL)) ? from_chars(tree->val(id), wrapper) : false;
1365}
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 1367 of file tree.hpp.

1368{
1369 return C4_LIKELY(!(tree->type(id) & KEYNIL)) ? from_chars(tree->key(id), wrapper) : false;
1370}
@ 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 1381 of file tree.hpp.

1383{
1384 return C4_LIKELY(!(tree->type(id) & VALNIL)) ? from_chars(tree->val(id), v) : false;
1385}

◆ 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 1392 of file tree.hpp.

1394{
1395 return C4_LIKELY(!(tree->type(id) & KEYNIL)) ? from_chars(tree->key(id), v) : false;
1396}

◆ 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 1407 of file tree.hpp.

1409{
1410 using U = typename std::remove_cv<T>::type;
1411 enum { ischar = std::is_same<char, U>::value || std::is_same<signed char, U>::value || std::is_same<unsigned char, U>::value }; // NOLINT
1412 csubstr val = tree->val(id);
1413 NodeType ty = tree->type(id);
1414 if(C4_UNLIKELY((ty & VALNIL) || val.empty()))
1415 return false;
1416 // quote integral numbers if they have a leading 0
1417 // https://github.com/biojppm/rapidyaml/issues/291
1418 char first = val.str[0];
1419 if(ty.is_val_quoted() && (first != '0' && !ischar))
1420 return false;
1421 else if(first == '+')
1422 val = val.sub(1);
1423 return from_chars(val, v);
1424}
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 1432 of file tree.hpp.

1434{
1435 using U = typename std::remove_cv<T>::type;
1436 enum { ischar = std::is_same<char, U>::value || std::is_same<signed char, U>::value || std::is_same<unsigned char, U>::value }; // NOLINT
1437 csubstr key = tree->key(id);
1438 NodeType ty = tree->type(id);
1439 if((ty & KEYNIL) || key.empty())
1440 return false;
1441 // quote integral numbers if they have a leading 0
1442 // https://github.com/biojppm/rapidyaml/issues/291
1443 char first = key.str[0];
1444 if(ty.is_key_quoted() && (first != '0' && !ischar))
1445 return false;
1446 else if(first == '+')
1447 key = key.sub(1);
1448 return from_chars(key, v);
1449}
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 1456 of file tree.hpp.

1457{
1458 static_assert(std::is_floating_point<T>::value, "must be floating point");
1459 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wfloat-equal")
1460 if(C4_UNLIKELY(std::isnan(val)))
1461 return to_chars(buf, csubstr(".nan"));
1462 else if(C4_UNLIKELY(val == std::numeric_limits<T>::infinity()))
1463 return to_chars(buf, csubstr(".inf"));
1464 else if(C4_UNLIKELY(val == -std::numeric_limits<T>::infinity()))
1465 return to_chars(buf, csubstr("-.inf"));
1466 return to_chars(buf, val);
1467 C4_SUPPRESS_WARNING_GCC_CLANG_POP
1468}
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 1474 of file tree.hpp.

1475{
1476 static_assert(std::is_floating_point<T>::value, "must be floating point");
1477 if(buf.begins_with('+'))
1478 {
1479 buf = buf.sub(1);
1480 }
1481 if(C4_LIKELY(from_chars(buf, val)))
1482 {
1483 return true;
1484 }
1485 else if(C4_UNLIKELY(buf == ".nan" || buf == ".NaN" || buf == ".NAN"))
1486 {
1487 *val = std::numeric_limits<T>::quiet_NaN();
1488 return true;
1489 }
1490 else if(C4_UNLIKELY(buf == ".inf" || buf == ".Inf" || buf == ".INF"))
1491 {
1492 *val = std::numeric_limits<T>::infinity();
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
1501 {
1502 return false;
1503 }
1504}
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 1530 of file tree.hpp.

1531{
1532 csubstr val = tree->val(id);
1533 return C4_LIKELY(!val.empty()) ? from_chars_float(val, v) : false;
1534}
bool from_chars_float(csubstr buf, T *val)
decode a floating point from string.
Definition tree.hpp:1474

◆ 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 1560 of file tree.hpp.

1561{
1562 csubstr key = tree->key(id);
1563 return C4_LIKELY(!key.empty()) ? from_chars_float(key, v) : false;
1564}

◆ serialize_to_arena()

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

Definition at line 1570 of file tree.hpp.

1571{
1572 substr rem(tree->m_arena.sub(tree->m_arena_pos));
1573 size_t num = serialize_scalar(rem, a);
1574 if(num > rem.len)
1575 {
1576 rem = tree->_grow_arena(num);
1577 num = serialize_scalar(rem, a);
1578 _RYML_ASSERT_VISIT_(tree->m_callbacks, num <= rem.len, tree, NONE);
1579 }
1580 rem = tree->_request_span(num);
1581 return rem;
1582}
size_t m_arena_pos
Definition tree.hpp:1339
substr m_arena
Definition tree.hpp:1338
Callbacks m_callbacks
Definition tree.hpp:1341
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
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
@ NONE
an index to none
Definition common.hpp:256