rapidyaml  0.11.0
parse and emit YAML, and do it fast
map.hpp
Go to the documentation of this file.
1 #ifndef _C4_YML_STD_MAP_HPP_
2 #define _C4_YML_STD_MAP_HPP_
3 
4 /** @file map.hpp write/read std::map to/from a YAML tree. */
5 
6 #include "c4/yml/node.hpp"
7 #include <map>
8 
9 namespace c4 {
10 namespace yml {
11 
12 // std::map requires child nodes in the data
13 // tree hierarchy (a MAP node in ryml parlance).
14 // So it should be serialized via write()/read().
15 
16 template<class K, class V, class Less, class Alloc>
17 void write(c4::yml::NodeRef *n, std::map<K, V, Less, Alloc> const& m)
18 {
19  *n |= c4::yml::MAP;
20  for(auto const& C4_RESTRICT p : m)
21  {
22  auto ch = n->append_child();
23  ch << c4::yml::key(p.first);
24  ch << p.second;
25  }
26 }
27 
28 /** read the node members, assigning into the existing map. If a key
29  * is already present in the map, then its value will be
30  * move-assigned. */
31 template<class K, class V, class Less, class Alloc>
32 bool read(c4::yml::ConstNodeRef const& n, std::map<K, V, Less, Alloc> * m)
33 {
34  for(auto const& C4_RESTRICT ch : n)
35  {
36  K k{};
37  ch >> c4::yml::key(k);
38  ch >> (*m)[k];
39  }
40  return true;
41 }
42 
43 } // namespace yml
44 } // namespace c4
45 
46 #endif // _C4_YML_STD_MAP_HPP_
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
NodeRef append_child()
Definition: node.hpp:1407
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:38
void write(NodeRef *n, T const &v)
Definition: node.hpp:1626
Key< K > key(K &k)
Definition: node.hpp:43
bool read(ConstNodeRef const &n, T *v)
Definition: node.hpp:1632
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
Node classes.