From 0526d7c29997bbb02400e4b23b23e1ce6595e040 Mon Sep 17 00:00:00 2001 From: Jonathan Hamilton Date: Thu, 28 May 2015 20:10:25 -0700 Subject: [PATCH 1/2] Return fallback if .as(fallback) called on a node that doesn't exist Since 1025f76df1b32b6ec3571ca928d7797a768a3341 it seems that missing nodes point to a 'zombie' node, where the m_isValid member is false. This caused .as(const S& fallback) throw an exception if the node was missing, instead of returning the provided fallback value (as was the behaviour previous to the above commit) --- include/yaml-cpp/node/impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 26cccbd..d711527 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -149,7 +149,7 @@ inline const T Node::as() const { template inline const T Node::as(const S& fallback) const { if (!m_isValid) - throw InvalidNode(); + return fallback; return as_if(*this)(fallback); } From 12b1e7fa673bd718c3ba65309408ded2e043bc97 Mon Sep 17 00:00:00 2001 From: Jonathan Hamilton Date: Thu, 28 May 2015 20:23:38 -0700 Subject: [PATCH 2/2] Make a missing node iterator .begin() == end() Instead of throwing an exception, just treat the iterator as if it had zero positions --- include/yaml-cpp/node/impl.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index d711527..69db1b7 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -282,26 +282,26 @@ inline std::size_t Node::size() const { inline const_iterator Node::begin() const { if (!m_isValid) - throw InvalidNode(); + return const_iterator(); return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory) : const_iterator(); } inline iterator Node::begin() { if (!m_isValid) - throw InvalidNode(); + return iterator(); return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator(); } inline const_iterator Node::end() const { if (!m_isValid) - throw InvalidNode(); + return const_iterator(); return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator(); } inline iterator Node::end() { if (!m_isValid) - throw InvalidNode(); + return iterator(); return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator(); }