rapidyaml  0.7.2
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 template<class K, class V, class Less, class Alloc>
29 bool read(c4::yml::ConstNodeRef const& n, std::map<K, V, Less, Alloc> * m)
30 {
31  K k{};
32  V v{};
33  for(auto const& C4_RESTRICT ch : n)
34  {
35  ch >> c4::yml::key(k);
36  ch >> v;
37  m->emplace(std::make_pair(std::move(k), std::move(v)));
38  }
39  return true;
40 }
41 
42 } // namespace yml
43 } // namespace c4
44 
45 #endif // _C4_YML_STD_MAP_HPP_
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:836
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:975
NodeRef append_child()
Definition: node.hpp:1385
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:35
void write(NodeRef *n, T const &v)
Definition: node.hpp:1602
Key< K > key(K &k)
Definition: node.hpp:43
std::enable_if< ! std::is_floating_point< T >::value, bool >::type read(NodeRef const &n, T *v)
convert the val of a scalar node to a floating point type, by forwarding its val to from_chars_float<...
Definition: node.hpp:1676
Definition: common.cpp:12
Node classes.