rapidyaml  0.12.1
parse and emit YAML, and do it fast
vector.hpp
Go to the documentation of this file.
1 #ifndef _C4_YML_STD_VECTOR_HPP_
2 #define _C4_YML_STD_VECTOR_HPP_
3 
4 #include "c4/yml/node.hpp"
5 #include <c4/std/vector.hpp>
6 #include <vector>
7 
8 namespace c4 {
9 namespace yml {
10 
11 // vector is a sequence-like type, and it requires child nodes
12 // in the data tree hierarchy (a SEQ node in ryml parlance).
13 // So it should be serialized via write()/read().
14 
15 
16 template<class V, class Alloc>
17 void write(c4::yml::NodeRef *n, std::vector<V, Alloc> const& vec)
18 {
19  *n |= c4::yml::SEQ;
20  for(V const& v : vec)
21  n->append_child() << v;
22 }
23 
24 /** read the node members, overwriting existing vector entries. */
25 template<class V, class Alloc>
26 bool read(c4::yml::ConstNodeRef const& n, std::vector<V, Alloc> *vec)
27 {
28  C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wuseless-cast")
29  vec->resize(static_cast<size_t>(n.num_children()));
30  C4_SUPPRESS_WARNING_GCC_POP
31  size_t pos = 0;
32  for(ConstNodeRef const child : n)
33  child >> (*vec)[pos++];
34  return true;
35 }
36 
37 /** read the node members, overwriting existing vector entries.
38  * specialization: std::vector<bool> uses std::vector<bool>::reference as
39  * the return value of its operator[]. */
40 template<class Alloc>
41 bool read(c4::yml::ConstNodeRef const& n, std::vector<bool, Alloc> *vec)
42 {
43  C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wuseless-cast")
44  vec->resize(static_cast<size_t>(n.num_children()));
45  C4_SUPPRESS_WARNING_GCC_POP
46  size_t pos = 0;
47  bool tmp = {};
48  for(ConstNodeRef const child : n)
49  {
50  child >> tmp;
51  (*vec)[pos++] = tmp;
52  }
53  return true;
54 }
55 
56 } // namespace yml
57 } // namespace c4
58 
59 #endif // _C4_YML_STD_VECTOR_HPP_
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:855
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:995
NodeRef append_child()
Definition: node.hpp:1413
provides conversion and comparison facilities from/between std::vector<char> to c4::substr and c4::cs...
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:40
void write(NodeRef *n, T const &v)
Definition: node.hpp:1632
bool read(ConstNodeRef const &n, T *v)
Definition: node.hpp:1638
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
Node classes.
id_type num_children() const RYML_NOEXCEPT
O(num_children).
Definition: node.hpp:386