mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Added reading/writing std::vector
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
|
||||
#include "yaml-cpp/node/node.h"
|
||||
#include "yaml-cpp/node/iterator.h"
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
@@ -19,10 +20,10 @@ namespace YAML
|
||||
return Node(rhs);
|
||||
}
|
||||
|
||||
static bool decode(const Node& value, std::string& rhs) {
|
||||
if(value.Type() != NodeType::Scalar)
|
||||
static bool decode(const Node& node, std::string& rhs) {
|
||||
if(node.Type() != NodeType::Scalar)
|
||||
return false;
|
||||
rhs = value.scalar();
|
||||
rhs = node.scalar();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@@ -36,10 +37,10 @@ namespace YAML
|
||||
return Node(stream.str());\
|
||||
}\
|
||||
\
|
||||
static bool decode(const Node& value, type& rhs) {\
|
||||
if(value.Type() != NodeType::Scalar)\
|
||||
static bool decode(const Node& node, type& rhs) {\
|
||||
if(node.Type() != NodeType::Scalar)\
|
||||
return false;\
|
||||
std::stringstream stream(value.scalar());\
|
||||
std::stringstream stream(node.scalar());\
|
||||
stream >> rhs;\
|
||||
return !!stream;\
|
||||
}\
|
||||
@@ -66,17 +67,37 @@ namespace YAML
|
||||
template<typename K, typename V>
|
||||
struct convert<std::map<K, V> > {
|
||||
static Node encode(const std::map<K, V>& rhs) {
|
||||
Node value(NodeType::Map);
|
||||
Node node(NodeType::Map);
|
||||
for(typename std::map<K, V>::const_iterator it=rhs.begin();it!=rhs.end();++it)
|
||||
value[it->first] = it->second;
|
||||
return value;
|
||||
node[it->first] = it->second;
|
||||
return node;
|
||||
}
|
||||
|
||||
static bool decode(const Node& value, std::map<K, V>& rhs) {
|
||||
static bool decode(const Node& node, std::map<K, V>& rhs) {
|
||||
rhs.clear();
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct convert<std::vector<T> > {
|
||||
static Node encode(const std::vector<T>& rhs) {
|
||||
Node node(NodeType::Sequence);
|
||||
for(std::size_t i=0;i<rhs.size();i++)
|
||||
node.append(rhs[i]);
|
||||
return node;
|
||||
}
|
||||
|
||||
static bool decode(const Node& node, std::vector<T>& rhs) {
|
||||
if(node.Type() != NodeType::Sequence)
|
||||
return false;
|
||||
|
||||
rhs.clear();
|
||||
for(const_iterator it=node.begin();it!=node.end();++it)
|
||||
rhs.push_back(it->as<T>());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
@@ -46,9 +46,9 @@ namespace YAML
|
||||
value_type dereference() const {
|
||||
const typename base_type::value_type& v = *this->base();
|
||||
if(v.pNode)
|
||||
return value_type(Value(*v, m_pMemory));
|
||||
return value_type(Node(*v, m_pMemory));
|
||||
if(v.first && v.second)
|
||||
return value_type(Value(*v.first, m_pMemory), Value(*v.second, m_pMemory));
|
||||
return value_type(Node(*v.first, m_pMemory), Node(*v.second, m_pMemory));
|
||||
return value_type();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user