rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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 /** serialize a map to a node: implementation for @ref Tree */
17 template<class K, class V, class Less, class Alloc>
18 void write(c4::yml::Tree *tree, c4::yml::id_type id, std::map<K, V, Less, Alloc> const& m)
19 {
20  tree->set_map(id);
21  for(auto const& C4_RESTRICT p : m)
22  {
23  id_type child = tree->append_child(id);
24  tree->set_key_serialized(child, p.first);
25  tree->set_serialized(child, p.second);
26  }
27 }
28 
29 /** serialize a map to a node: implementation for @ref NodeRef */
30 template<class K, class V, class Less, class Alloc>
31 C4_ALWAYS_INLINE void write(c4::yml::NodeRef *n, std::map<K, V, Less, Alloc> const& m)
32 {
33  n->set_map();
34  for(auto const& C4_RESTRICT p : m)
35  {
36  NodeRef ch = n->append_child();
37  ch.set_key_serialized(p.first);
38  ch.set_serialized(p.second);
39  }
40 }
41 
42 
43 /** deserialize a map from a node: implementation for @ref Tree .
44  * Read the node members, assigning into the existing map. If a key is
45  * already present in the map, then its value will be
46  * move-assigned. The map */
47 template<class K, class V, class Less, class Alloc>
48 ReadResult read(c4::yml::Tree const* tree, c4::yml::id_type id, std::map<K, V, Less, Alloc> * m)
49 {
50  if C4_UNLIKELY(!tree->is_map(id))
51  return ReadResult(id);
52  for(id_type child = tree->first_child(id); child != NONE; child = tree->next_sibling(child))
53  {
54  K k{};
55  ReadResult result = tree->deserialize_key(child, &k);
56  if C4_UNLIKELY(!result)
57  return result;
58  result = tree->deserialize(child, &(*m)[std::move(k)]);
59  if C4_UNLIKELY(!result)
60  return result;
61  }
62  return ReadResult();
63 }
64 
65 /** deserialize a map from a node: implementation for @ref ConstNodeRef . read
66  * the node members, assigning into the existing map. If a key is
67  * already present in the map, then its value will be
68  * move-assigned. */
69 template<class K, class V, class Less, class Alloc>
70 ReadResult read(c4::yml::ConstNodeRef const& n, std::map<K, V, Less, Alloc> * m)
71 {
72  if C4_UNLIKELY(!n.is_map())
73  return ReadResult(n.id());
74  for(ConstNodeRef const& C4_RESTRICT ch : n)
75  {
76  K k{};
77  ReadResult result = ch.deserialize_key(&k);
78  if C4_UNLIKELY(!result)
79  return result;
80  result = ch.deserialize(&(*m)[std::move(k)]);
81  if C4_UNLIKELY(!result)
82  return result;
83  }
84  return ReadResult();
85 }
86 
87 } // namespace yml
88 } // namespace c4
89 
90 #endif // C4_YML_STD_MAP_HPP_
an index to none
Definition: common.hpp:131
id_type first_child(id_type node) const
Definition: tree.hpp:577
bool is_map() const RYML_NOEXCEPT
Forward to Tree::is_map().
Definition: node.hpp:207
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition: common.hpp:161
void set_map(id_type node) RYML_NOEXCEPT
Definition: tree.hpp:731
id_type id() const noexcept
Definition: node.hpp:822
bool is_map(id_type node) const
Definition: tree.hpp:480
void set_key_serialized(id_type node, T const &key) RYML_NOEXCEPT
Definition: tree.hpp:837
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:1062
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:1180
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:124
void write(NodeRef *n, T const &v)
Definition: node.hpp:2114
ReadResult deserialize(id_type node, T *v) const
(1) deserialize a node's contents to a variable
Definition: tree.hpp:954
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:736
id_type next_sibling(id_type node) const
Definition: tree.hpp:572
void set_serialized(id_type node, T const &val) RYML_NOEXCEPT
Definition: tree.hpp:823
Node classes.
NodeRef append_child()
Definition: node.hpp:1715
ReadResult deserialize_key(id_type node, T *v) const
(1) deserialize a node's key to a variable
Definition: tree.hpp:975
void set_serialized(T const &v)
serialize a variable to this node.
Definition: node.hpp:1375
void set_key_serialized(T const &k)
serialize a variable, then assign the result to the node's key
Definition: node.hpp:1396
ReadResult read(ConstNodeRef const &n, T *v)
Definition: node.hpp:2074
void set_map()
Definition: node.hpp:1257