rapidyaml  0.7.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 template<class V, class Alloc>
25 bool read(c4::yml::ConstNodeRef const& n, std::vector<V, Alloc> *vec)
26 {
27  C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wuseless-cast")
28  vec->resize(static_cast<size_t>(n.num_children()));
29  C4_SUPPRESS_WARNING_GCC_POP
30  size_t pos = 0;
31  for(ConstNodeRef const child : n)
32  child >> (*vec)[pos++];
33  return true;
34 }
35 
36 /** specialization: std::vector<bool> uses std::vector<bool>::reference as
37  * the return value of its operator[]. */
38 template<class Alloc>
39 bool read(c4::yml::ConstNodeRef const& n, std::vector<bool, Alloc> *vec)
40 {
41  C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wuseless-cast")
42  vec->resize(static_cast<size_t>(n.num_children()));
43  C4_SUPPRESS_WARNING_GCC_POP
44  size_t pos = 0;
45  bool tmp = {};
46  for(ConstNodeRef const child : n)
47  {
48  child >> tmp;
49  (*vec)[pos++] = tmp;
50  }
51  return true;
52 }
53 
54 } // namespace yml
55 } // namespace c4
56 
57 #endif // _C4_YML_STD_VECTOR_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
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:36
void write(NodeRef *n, T const &v)
Definition: node.hpp:1602
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.
id_type num_children() const RYML_NOEXCEPT
O(num_children).
Definition: node.hpp:378