This commit is contained in:
Noah Harvey 2015-11-25 09:59:40 -05:00
commit 8a09266baf
4 changed files with 35 additions and 5 deletions

View File

@ -43,6 +43,17 @@ inline bool IsNaN(const std::string& input) {
}
}
// Node
template <>
struct convert<Node> {
static Node encode(const Node& rhs) { return rhs; }
static bool decode(const Node& node, Node& rhs) {
rhs.reset(node);
return true;
}
};
// std::string
template <>
struct convert<std::string> {

View File

@ -87,7 +87,7 @@ struct as_if {
explicit as_if(const Node& node_) : node(node_) {}
const Node& node;
const T operator()(const S& fallback) const {
T operator()(const S& fallback) const {
if (!node.m_pNode)
return fallback;
@ -140,14 +140,14 @@ struct as_if<std::string, void> {
// access functions
template <typename T>
inline const T Node::as() const {
inline T Node::as() const {
if (!m_isValid)
throw InvalidNode();
return as_if<T, void>(*this)();
}
template <typename T, typename S>
inline const T Node::as(const S& fallback) const {
inline T Node::as(const S& fallback) const {
if (!m_isValid)
return fallback;
return as_if<T, S>(*this)(fallback);

View File

@ -63,9 +63,9 @@ class YAML_CPP_API Node {
// access
template <typename T>
const T as() const;
T as() const;
template <typename T, typename S>
const T as(const S& fallback) const;
T as(const S& fallback) const;
const std::string& Scalar() const;
const std::string& Tag() const;

View File

@ -80,6 +80,25 @@ TEST(NodeTest, MapWithUndefinedValues) {
EXPECT_EQ(2, node.size());
}
TEST(NodeTest, MapForceInsert) {
Node node;
Node k1("k1");
Node k2("k2");
Node v1("v1");
Node v2("v2");
node[k1] = v1;
node[k2] = v1;
EXPECT_TRUE(node.IsMap());
EXPECT_EQ("v1", node["k1"].as<std::string>());
EXPECT_EQ("v1", node["k2"].as<std::string>());
EXPECT_EQ(2, node.size());
node.force_insert(k2, v2);
EXPECT_EQ("v1", node["k1"].as<std::string>());
EXPECT_EQ("v2", node["k2"].as<std::string>());
EXPECT_EQ(2, node.size());
}
TEST(NodeTest, UndefinedConstNodeWithFallback) {
Node node;
const Node& cn = node;