rapidyaml  0.11.0
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 3691 of file quickstart.cpp.

3692 {
3693  *n |= ryml::SEQ;
3694  for(auto const& v : seq.seq_member)
3695  n->append_child() << v;
3696 }
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 3698 of file quickstart.cpp.

3699 {
3700  *n |= ryml::MAP;
3701  for(auto const& v : map.map_member)
3702  n->append_child() << ryml::key(v.first) << v.second;
3703 }
@ 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 3704 of file quickstart.cpp.

3705 {
3706  *n |= ryml::MAP;
3707  // these are leaf nodes:
3708  n->append_child() << ryml::key("v2") << val.v2;
3709  n->append_child() << ryml::key("v3") << val.v3;
3710  n->append_child() << ryml::key("v4") << val.v4;
3711  // these are container nodes:
3712  n->append_child() << ryml::key("seq") << val.seq;
3713  n->append_child() << ryml::key("map") << val.map;
3714 }

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 3717 of file quickstart.cpp.

3718 {
3719  seq->seq_member.resize(static_cast<size_t>(n.num_children())); // num_children() is O(N)
3720  size_t pos = 0;
3721  for(auto const ch : n.children())
3722  ch >> seq->seq_member[pos++];
3723  return true;
3724 }

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 3726 of file quickstart.cpp.

3727 {
3728  K k{};
3729  V v{};
3730  for(auto const ch : n)
3731  {
3732  ch >> c4::yml::key(k) >> v;
3733  map->map_member.emplace(std::make_pair(std::move(k), std::move(v)));
3734  }
3735  return true;
3736 }

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 3737 of file quickstart.cpp.

3738 {
3739  // these are leaf nodes:
3740  n["v2"] >> val->v2;
3741  n["v3"] >> val->v3;
3742  n["v4"] >> val->v4;
3743  // these are container nodes:
3744  n["seq"] >> val->seq;
3745  n["map"] >> val->map;
3746  return true;
3747 }
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.