vector.hpp (1231B)
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(auto 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 vec->resize(n.num_children()); 28 size_t pos = 0; 29 for(auto const ch : n) 30 ch >> (*vec)[pos++]; 31 return true; 32 } 33 34 /** specialization: std::vector<bool> uses std::vector<bool>::reference as 35 * the return value of its operator[]. */ 36 template<class Alloc> 37 bool read(c4::yml::ConstNodeRef const& n, std::vector<bool, Alloc> *vec) 38 { 39 vec->resize(n.num_children()); 40 size_t pos = 0; 41 bool tmp; 42 for(auto const ch : n) 43 { 44 ch >> tmp; 45 (*vec)[pos++] = tmp; 46 } 47 return true; 48 } 49 50 } // namespace yml 51 } // namespace c4 52 53 #endif // _C4_YML_STD_VECTOR_HPP_