rapidyaml  0.11.1
parse and emit YAML, and do it fast
Serialize/deserialize container types

To serialize/deserialize container types to a tree, implement the appropriate functions: More...

Classes

struct  my_seq_type< T >
 example user container type: seq-like More...
 
struct  my_map_type< K, V >
 example user container type: map-like More...
 
struct  my_type
 example user container type with nested container members. More...
 

Functions

template<class T >
void write (ryml::NodeRef *n, my_seq_type< T > const &seq)
 
template<class K , class V >
void write (ryml::NodeRef *n, my_map_type< K, V > const &map)
 
void write (ryml::NodeRef *n, my_type const &val)
 
template<class T >
bool read (ryml::ConstNodeRef const &n, my_seq_type< T > *seq)
 
template<class K , class V >
bool read (ryml::ConstNodeRef const &n, my_map_type< K, V > *map)
 
bool read (ryml::ConstNodeRef const &n, my_type *val)
 

Detailed Description

To serialize/deserialize container types to a tree, implement the appropriate functions:

void write(ryml::NodeRef *n, T const& seq);
bool read(ryml::ConstNodeRef const& n, T *seq);
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:849
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:989
void write(NodeRef *n, T const &v)
Definition: node.hpp:1626
bool read(ConstNodeRef const &n, T *v)
Definition: node.hpp:1632
Warning
Because of C++'s ADL rules, it is required to overload these functions in the namespace of the type you're serializing (or in the c4 namespace, or in the c4::yml namespace). [Here's an example of an issue where failing to do this was causing problems in some platforms](https://github.com/biojppm/rapidyaml/issues/424)
Note
Please take note of the following pitfall when using serialization functions: you may have to include the header with your write() or read() implementation before any other headers that use functions from it. See the include order at the top of this source file. This constraint also applies to the conversion functions for your types; just like with the STL's headers, they should be included prior to ryml's headers. Lately, some effort was directed to provide forward declarations to alleviate this problem, but it may still occur.
See also
sample_container_types
sample_std_types

Function Documentation

◆ write() [1/3]

template<class T >
void write ( ryml::NodeRef n,
my_seq_type< T > const &  seq 
)

Definition at line 3707 of file quickstart.cpp.

3708 {
3709  *n |= ryml::SEQ;
3710  for(auto const& v : seq.seq_member)
3711  n->append_child() << v;
3712 }
NodeRef append_child()
Definition: node.hpp:1407
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:39
std::vector< T > seq_member

References c4::yml::NodeRef::append_child(), c4::yml::SEQ, and my_seq_type< T >::seq_member.

◆ write() [2/3]

template<class K , class V >
void write ( ryml::NodeRef n,
my_map_type< K, V > const &  map 
)

Definition at line 3714 of file quickstart.cpp.

3715 {
3716  *n |= ryml::MAP;
3717  for(auto const& v : map.map_member)
3718  n->append_child() << ryml::key(v.first) << v.second;
3719 }
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:38
Key< K > key(K &k)
Definition: node.hpp:43
std::map< K, V > map_member

References c4::yml::NodeRef::append_child(), c4::yml::key(), c4::yml::MAP, and my_map_type< K, V >::map_member.

◆ write() [3/3]

void write ( ryml::NodeRef n,
my_type const &  val 
)

Definition at line 3720 of file quickstart.cpp.

3721 {
3722  *n |= ryml::MAP;
3723  // these are leaf nodes:
3724  n->append_child() << ryml::key("v2") << val.v2;
3725  n->append_child() << ryml::key("v3") << val.v3;
3726  n->append_child() << ryml::key("v4") << val.v4;
3727  // these are container nodes:
3728  n->append_child() << ryml::key("seq") << val.seq;
3729  n->append_child() << ryml::key("map") << val.map;
3730 }

References c4::yml::NodeRef::append_child(), c4::yml::key(), my_type::map, c4::yml::MAP, my_type::seq, my_type::v2, my_type::v3, and my_type::v4.

◆ read() [1/3]

template<class T >
bool read ( ryml::ConstNodeRef const &  n,
my_seq_type< T > *  seq 
)

Definition at line 3733 of file quickstart.cpp.

3734 {
3735  seq->seq_member.resize(static_cast<size_t>(n.num_children())); // num_children() is O(N)
3736  size_t pos = 0;
3737  for(auto const ch : n.children())
3738  ch >> seq->seq_member[pos++];
3739  return true;
3740 }

References c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::children(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::num_children(), and my_seq_type< T >::seq_member.

◆ read() [2/3]

template<class K , class V >
bool read ( ryml::ConstNodeRef const &  n,
my_map_type< K, V > *  map 
)

Definition at line 3742 of file quickstart.cpp.

3743 {
3744  K k{};
3745  V v{};
3746  for(auto const ch : n)
3747  {
3748  ch >> c4::yml::key(k) >> v;
3749  map->map_member.emplace(std::make_pair(std::move(k), std::move(v)));
3750  }
3751  return true;
3752 }

References c4::yml::key(), and my_map_type< K, V >::map_member.

◆ read() [3/3]

bool read ( ryml::ConstNodeRef const &  n,
my_type val 
)

Definition at line 3753 of file quickstart.cpp.

3754 {
3755  // these are leaf nodes:
3756  n["v2"] >> val->v2;
3757  n["v3"] >> val->v3;
3758  n["v4"] >> val->v4;
3759  // these are container nodes:
3760  n["seq"] >> val->seq;
3761  n["map"] >> val->map;
3762  return true;
3763 }
vec2< int > v2
my_map_type< int, int > map
vec3< int > v3
vec4< int > v4
my_seq_type< int > seq

References my_type::map, my_type::seq, my_type::v2, my_type::v3, and my_type::v4.