map.hpp (1046B)
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_