stream << KEY_NOT_FOUND << ": " << key;
return stream.str();
}
-
-template <typename T>
-inline const std::string BAD_SUBSCRIPT_WITH_KEY(
- const T&, typename disable_if<is_numeric<T>>::type* = 0) {
- return BAD_SUBSCRIPT;
-}
-
-inline const std::string BAD_SUBSCRIPT_WITH_KEY(const std::string& key) {
- std::stringstream stream;
- stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
- return stream.str();
-}
-
-template <typename T>
-inline const std::string BAD_SUBSCRIPT_WITH_KEY(
- const T& key, typename enable_if<is_numeric<T>>::type* = 0) {
- std::stringstream stream;
- stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
- return stream.str();
-}
-
-inline const std::string INVALID_NODE_WITH_KEY(const std::string& key) {
- std::stringstream stream;
- if (key.empty()) {
- return INVALID_NODE;
- }
- stream << "invalid node; first invalid key: \"" << key << "\"";
- return stream.str();
-}
}
class YAML_CPP_API Exception : public std::runtime_error {
class YAML_CPP_API InvalidNode : public RepresentationException {
public:
- InvalidNode(std::string key)
- : RepresentationException(Mark::null_mark(),
- ErrorMsg::INVALID_NODE_WITH_KEY(key)) {}
+ InvalidNode()
+ : RepresentationException(Mark::null_mark(), ErrorMsg::INVALID_NODE) {}
InvalidNode(const InvalidNode&) = default;
virtual ~InvalidNode() YAML_CPP_NOEXCEPT;
};
class YAML_CPP_API BadSubscript : public RepresentationException {
public:
- template <typename Key>
- BadSubscript(const Key& key)
- : RepresentationException(Mark::null_mark(),
- ErrorMsg::BAD_SUBSCRIPT_WITH_KEY(key)) {}
+ BadSubscript()
+ : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_SUBSCRIPT) {}
BadSubscript(const BadSubscript&) = default;
virtual ~BadSubscript() YAML_CPP_NOEXCEPT;
};
#include "yaml-cpp/node/detail/node.h"
#include "yaml-cpp/node/iterator.h"
#include "yaml-cpp/node/node.h"
-#include <sstream>
#include <string>
namespace YAML {
-inline Node::Node()
- : m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {}
+inline Node::Node() : m_isValid(true), m_pMemory(nullptr), m_pNode(nullptr) {}
inline Node::Node(NodeType::value type)
: m_isValid(true),
- m_invalidKey{},
m_pMemory(new detail::memory_holder),
m_pNode(&m_pMemory->create_node()) {
m_pNode->set_type(type);
template <typename T>
inline Node::Node(const T& rhs)
: m_isValid(true),
- m_invalidKey{},
m_pMemory(new detail::memory_holder),
m_pNode(&m_pMemory->create_node()) {
Assign(rhs);
inline Node::Node(const detail::iterator_value& rhs)
: m_isValid(rhs.m_isValid),
- m_invalidKey(rhs.m_invalidKey),
m_pMemory(rhs.m_pMemory),
m_pNode(rhs.m_pNode) {}
inline Node::Node(const Node& rhs)
: m_isValid(rhs.m_isValid),
- m_invalidKey(rhs.m_invalidKey),
m_pMemory(rhs.m_pMemory),
m_pNode(rhs.m_pNode) {}
-inline Node::Node(Zombie)
- : m_isValid(false), m_invalidKey{}, m_pMemory{}, m_pNode(nullptr) {}
-
-inline Node::Node(Zombie, const std::string& key)
- : m_isValid(false), m_invalidKey(key), m_pMemory{}, m_pNode(NULL) {}
+inline Node::Node(Zombie) : m_isValid(false), m_pMemory{}, m_pNode(nullptr) {}
inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
- : m_isValid(true), m_invalidKey{}, m_pMemory(pMemory), m_pNode(&node) {}
+ : m_isValid(true), m_pMemory(pMemory), m_pNode(&node) {}
inline Node::~Node() {}
inline void Node::EnsureNodeExists() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
if (!m_pNode) {
m_pMemory.reset(new detail::memory_holder);
m_pNode = &m_pMemory->create_node();
inline Mark Node::Mark() const {
if (!m_isValid) {
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
}
return m_pNode ? m_pNode->mark() : Mark::null_mark();
}
inline NodeType::value Node::Type() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
return m_pNode ? m_pNode->type() : NodeType::Null;
}
template <typename T>
inline T Node::as() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
return as_if<T, void>(*this)();
}
inline const std::string& Node::Scalar() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
}
inline const std::string& Node::Tag() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
}
inline void Node::SetTag(const std::string& tag) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
m_pNode->set_tag(tag);
}
inline EmitterStyle::value Node::Style() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
return m_pNode ? m_pNode->style() : EmitterStyle::Default;
}
inline void Node::SetStyle(EmitterStyle::value style) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
m_pNode->set_style(style);
}
// assignment
inline bool Node::is(const Node& rhs) const {
if (!m_isValid || !rhs.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
if (!m_pNode || !rhs.m_pNode)
return false;
return m_pNode->is(*rhs.m_pNode);
template <typename T>
inline Node& Node::operator=(const T& rhs) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
Assign(rhs);
return *this;
}
inline Node& Node::operator=(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
if (is(rhs))
return *this;
AssignNode(rhs);
inline void Node::reset(const YAML::Node& rhs) {
if (!m_isValid || !rhs.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
m_pMemory = rhs.m_pMemory;
m_pNode = rhs.m_pNode;
}
template <typename T>
inline void Node::Assign(const T& rhs) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
AssignData(convert<T>::encode(rhs));
}
template <>
inline void Node::Assign(const std::string& rhs) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
m_pNode->set_scalar(rhs);
}
inline void Node::Assign(const char* rhs) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
m_pNode->set_scalar(rhs);
}
inline void Node::Assign(char* rhs) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
m_pNode->set_scalar(rhs);
}
inline void Node::AssignData(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
rhs.EnsureNodeExists();
inline void Node::AssignNode(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
rhs.EnsureNodeExists();
if (!m_pNode) {
// size/iterator
inline std::size_t Node::size() const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
return m_pNode ? m_pNode->size() : 0;
}
template <typename T>
inline void Node::push_back(const T& rhs) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
push_back(Node(rhs));
}
inline void Node::push_back(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
rhs.EnsureNodeExists();
}
} // namespace detail
-template<typename Key>
-std::string key_to_string(const Key& key) {
- return streamable_to_string<Key, is_streamable<std::stringstream, Key>::value>().impl(key);
-}
-
// indexing
template <typename Key>
inline const Node Node::operator[](const Key& key) const {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
detail::node* value = static_cast<const detail::node&>(*m_pNode).get(
detail::to_value(key), m_pMemory);
if (!value) {
- return Node(ZombieNode, key_to_string(key));
+ return Node(ZombieNode);
}
return Node(*value, m_pMemory);
}
template <typename Key>
inline Node Node::operator[](const Key& key) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory);
return Node(value, m_pMemory);
template <typename Key>
inline bool Node::remove(const Key& key) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
return m_pNode->remove(detail::to_value(key), m_pMemory);
}
inline const Node Node::operator[](const Node& key) const {
if (!m_isValid || !key.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
key.EnsureNodeExists();
m_pMemory->merge(*key.m_pMemory);
detail::node* value =
static_cast<const detail::node&>(*m_pNode).get(*key.m_pNode, m_pMemory);
if (!value) {
- return Node(ZombieNode, key_to_string(key));
+ return Node(ZombieNode);
}
return Node(*value, m_pMemory);
}
inline Node Node::operator[](const Node& key) {
if (!m_isValid || !key.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
key.EnsureNodeExists();
m_pMemory->merge(*key.m_pMemory);
inline bool Node::remove(const Node& key) {
if (!m_isValid || !key.m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
key.EnsureNodeExists();
return m_pNode->remove(*key.m_pNode, m_pMemory);
template <typename Key, typename Value>
inline void Node::force_insert(const Key& key, const Value& value) {
if (!m_isValid)
- throw InvalidNode(m_invalidKey);
+ throw InvalidNode();
EnsureNodeExists();
m_pNode->force_insert(detail::to_value(key), detail::to_value(value),
m_pMemory);
+++ /dev/null
-#include "yaml-cpp/yaml.h" // IWYU pragma: keep
-
-#include "gtest/gtest.h"
-
-#define EXPECT_THROW_EXCEPTION(exception_type, statement, message) \
- ASSERT_THROW(statement, exception_type); \
- try { \
- statement; \
- } catch (const exception_type& e) { \
- EXPECT_EQ(e.msg, message); \
- }
-
-namespace YAML {
-namespace {
-
-TEST(ErrorMessageTest, BadSubscriptErrorMessage) {
- const char *example_yaml = "first:\n"
- " second: 1\n"
- " third: 2\n";
-
- Node doc = Load(example_yaml);
-
- // Test that printable key is part of error message
- EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"]["fourth"],
- "operator[] call on a scalar (key: \"fourth\")");
-
- EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][37],
- "operator[] call on a scalar (key: \"37\")");
-
-
- // Non-printable key is not included in error message
- EXPECT_THROW_EXCEPTION(YAML::BadSubscript,
- doc["first"]["second"][std::vector<int>()],
- "operator[] call on a scalar");
-
- EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][Node()],
- "operator[] call on a scalar");
-}
-
-TEST(ErrorMessageTest, Ex9_1_InvalidNodeErrorMessage) {
- const char *example_yaml = "first:\n"
- " second: 1\n"
- " third: 2\n";
-
- const Node doc = Load(example_yaml);
-
- // Test that printable key is part of error message
- EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"]["fourth"].as<int>(),
- "invalid node; first invalid key: \"fourth\"");
-
- EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"][37].as<int>(),
- "invalid node; first invalid key: \"37\"");
-
- // Non-printable key is not included in error message
- EXPECT_THROW_EXCEPTION(YAML::InvalidNode,
- doc["first"][std::vector<int>()].as<int>(),
- "invalid node; this may result from using a map "
- "iterator as a sequence iterator, or vice-versa");
-}
-}
-}