remove Node::m_isValid member

a Node is valid when its shared_memory_holder is not null
This commit is contained in:
Hannes Janetzek 2016-11-24 11:10:58 +01:00
parent 52cc604c3f
commit 405d9784b5
2 changed files with 46 additions and 55 deletions

View File

@ -16,58 +16,48 @@
namespace YAML { namespace YAML {
inline Node::Node() inline Node::Node()
: m_isValid(true), : m_pMemory(new detail::memory_holder),
//m_pMemory(nullptr),
m_pMemory(new detail::memory_holder),
m_pNode(NULL) {} m_pNode(NULL) {}
inline Node::Node(NodeType::value type) inline Node::Node(NodeType::value type)
: m_isValid(true), : m_pMemory(new detail::memory_holder),
m_pMemory(new detail::memory_holder),
m_pNode(&(m_pMemory->create_node())) { m_pNode(&(m_pMemory->create_node())) {
m_pNode->set_type(type); m_pNode->set_type(type);
} }
template <typename T> template <typename T>
inline Node::Node(const T& rhs) inline Node::Node(const T& rhs)
: m_isValid(true), : m_pMemory(new detail::memory_holder),
m_pMemory(new detail::memory_holder),
m_pNode(&(m_pMemory->create_node())) { m_pNode(&(m_pMemory->create_node())) {
Assign(rhs); Assign(rhs);
} }
inline Node::Node(const detail::iterator_value& rhs) inline Node::Node(const detail::iterator_value& rhs)
: m_isValid(rhs.m_isValid), : m_pMemory(rhs.m_pMemory),
m_pMemory(rhs.m_pMemory),
m_pNode(rhs.m_pNode) {} m_pNode(rhs.m_pNode) {}
inline Node::Node(const Node& rhs) inline Node::Node(const Node& rhs)
: m_isValid(rhs.m_isValid), : m_pMemory(rhs.m_pMemory),
m_pMemory(rhs.m_pMemory),
m_pNode(rhs.m_pNode) {} m_pNode(rhs.m_pNode) {}
inline Node::Node(Zombie) inline Node::Node(Zombie)
: m_isValid(false), : m_pMemory(nullptr),
m_pMemory(nullptr),
m_pNode(NULL) {} m_pNode(NULL) {}
inline Node::Node(Node&& rhs) inline Node::Node(Node&& rhs)
: m_isValid(rhs.m_isValid), : m_pMemory(std::move(rhs.m_pMemory)),
m_pMemory(rhs.m_pMemory), // move ?
m_pNode(rhs.m_pNode) { m_pNode(rhs.m_pNode) {
rhs.m_pNode = nullptr; rhs.m_pNode = nullptr;
} }
inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory) inline Node::Node(detail::node& node, detail::shared_memory_holder pMemory)
: m_isValid(true), : m_pMemory(pMemory),
m_pMemory(pMemory),
m_pNode(&node) {} m_pNode(&node) {}
inline Node::~Node() {} inline Node::~Node() {}
inline void Node::EnsureNodeExists() const { inline void Node::EnsureNodeExists() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
if (!m_pNode) { if (!m_pNode) {
m_pMemory.reset(new detail::memory_holder); m_pMemory.reset(new detail::memory_holder);
@ -77,21 +67,21 @@ inline void Node::EnsureNodeExists() const {
} }
inline bool Node::IsDefined() const { inline bool Node::IsDefined() const {
if (!m_isValid) { if (!isValid()) {
return false; return false;
} }
return m_pNode ? m_pNode->is_defined() : true; return m_pNode ? m_pNode->is_defined() : true;
} }
inline Mark Node::Mark() const { inline Mark Node::Mark() const {
if (!m_isValid) { if (!isValid()) {
throw InvalidNode(); throw InvalidNode();
} }
return m_pNode ? m_pNode->mark() : Mark::null_mark(); return m_pNode ? m_pNode->mark() : Mark::null_mark();
} }
inline NodeType::value Node::Type() const { inline NodeType::value Node::Type() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
return m_pNode ? m_pNode->type() : NodeType::Null; return m_pNode ? m_pNode->type() : NodeType::Null;
} }
@ -158,45 +148,45 @@ struct as_if<std::string, void> {
// access functions // access functions
template <typename T> template <typename T>
inline T Node::as() const { inline T Node::as() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
return as_if<T, void>(*this)(); return as_if<T, void>(*this)();
} }
template <typename T, typename S> template <typename T, typename S>
inline T Node::as(const S& fallback) const { inline T Node::as(const S& fallback) const {
if (!m_isValid) if (!isValid())
return fallback; return fallback;
return as_if<T, S>(*this)(fallback); return as_if<T, S>(*this)(fallback);
} }
inline const std::string& Node::Scalar() const { inline const std::string& Node::Scalar() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar; return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar;
} }
inline const std::string& Node::Tag() const { inline const std::string& Node::Tag() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar; return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar;
} }
inline void Node::SetTag(const std::string& tag) { inline void Node::SetTag(const std::string& tag) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
m_pNode->set_tag(tag); m_pNode->set_tag(tag);
} }
inline EmitterStyle::value Node::Style() const { inline EmitterStyle::value Node::Style() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
return m_pNode ? m_pNode->style() : EmitterStyle::Default; return m_pNode ? m_pNode->style() : EmitterStyle::Default;
} }
inline void Node::SetStyle(EmitterStyle::value style) { inline void Node::SetStyle(EmitterStyle::value style) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
m_pNode->set_style(style); m_pNode->set_style(style);
@ -204,7 +194,7 @@ inline void Node::SetStyle(EmitterStyle::value style) {
// assignment // assignment
inline bool Node::is(const Node& rhs) const { inline bool Node::is(const Node& rhs) const {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
if (!m_pNode || !rhs.m_pNode) if (!m_pNode || !rhs.m_pNode)
return false; return false;
@ -213,14 +203,14 @@ inline bool Node::is(const Node& rhs) const {
template <typename T> template <typename T>
inline Node& Node::operator=(const T& rhs) { inline Node& Node::operator=(const T& rhs) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
Assign(rhs); Assign(rhs);
return *this; return *this;
} }
inline void Node::reset(const YAML::Node& rhs) { inline void Node::reset(const YAML::Node& rhs) {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
m_pMemory = rhs.m_pMemory; m_pMemory = rhs.m_pMemory;
m_pNode = rhs.m_pNode; m_pNode = rhs.m_pNode;
@ -228,35 +218,35 @@ inline void Node::reset(const YAML::Node& rhs) {
template <typename T> template <typename T>
inline void Node::Assign(const T& rhs) { inline void Node::Assign(const T& rhs) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
AssignData(convert<T>::encode(rhs)); AssignData(convert<T>::encode(rhs));
} }
template <> template <>
inline void Node::Assign(const std::string& rhs) { inline void Node::Assign(const std::string& rhs) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
m_pNode->set_scalar(rhs); m_pNode->set_scalar(rhs);
} }
inline void Node::Assign(const char* rhs) { inline void Node::Assign(const char* rhs) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
m_pNode->set_scalar(rhs); m_pNode->set_scalar(rhs);
} }
inline void Node::Assign(char* rhs) { inline void Node::Assign(char* rhs) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
m_pNode->set_scalar(rhs); m_pNode->set_scalar(rhs);
} }
inline Node& Node::operator=(const Node& rhs) { inline Node& Node::operator=(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
if (is(rhs)) if (is(rhs))
return *this; return *this;
@ -265,7 +255,7 @@ inline Node& Node::operator=(const Node& rhs) {
} }
inline Node& Node::operator=(Node&& rhs) { inline Node& Node::operator=(Node&& rhs) {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
if (is(rhs)) if (is(rhs))
return *this; return *this;
@ -276,7 +266,7 @@ inline Node& Node::operator=(Node&& rhs) {
} }
inline void Node::AssignData(Node&& rhs) { inline void Node::AssignData(Node&& rhs) {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
rhs.EnsureNodeExists(); rhs.EnsureNodeExists();
@ -286,7 +276,7 @@ inline void Node::AssignData(Node&& rhs) {
} }
inline void Node::AssignNode(const Node& rhs) { inline void Node::AssignNode(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
rhs.EnsureNodeExists(); rhs.EnsureNodeExists();
@ -303,32 +293,32 @@ inline void Node::AssignNode(const Node& rhs) {
// size/iterator // size/iterator
inline std::size_t Node::size() const { inline std::size_t Node::size() const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
return m_pNode ? m_pNode->size() : 0; return m_pNode ? m_pNode->size() : 0;
} }
inline const_iterator Node::begin() const { inline const_iterator Node::begin() const {
if (!m_isValid) if (!isValid())
return const_iterator(); return const_iterator();
return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory) return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
: const_iterator(); : const_iterator();
} }
inline iterator Node::begin() { inline iterator Node::begin() {
if (!m_isValid) if (!isValid())
return iterator(); return iterator();
return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator(); return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
} }
inline const_iterator Node::end() const { inline const_iterator Node::end() const {
if (!m_isValid) if (!isValid())
return const_iterator(); return const_iterator();
return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator(); return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
} }
inline iterator Node::end() { inline iterator Node::end() {
if (!m_isValid) if (!isValid())
return iterator(); return iterator();
return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator(); return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
} }
@ -336,13 +326,13 @@ inline iterator Node::end() {
// sequence // sequence
template <typename T> template <typename T>
inline void Node::push_back(const T& rhs) { inline void Node::push_back(const T& rhs) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
push_back(Node(rhs)); push_back(Node(rhs));
} }
inline void Node::push_back(const Node& rhs) { inline void Node::push_back(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid) if (!isValid() || !rhs.isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
rhs.EnsureNodeExists(); rhs.EnsureNodeExists();
@ -399,7 +389,7 @@ inline typename to_value_t<T>::return_type to_value(const T& t) {
// indexing // indexing
template <typename Key> template <typename Key>
inline const Node Node::operator[](const Key& key) const { inline const Node Node::operator[](const Key& key) const {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
detail::node* value = static_cast<const detail::node&>(*m_pNode) detail::node* value = static_cast<const detail::node&>(*m_pNode)
@ -412,7 +402,7 @@ inline const Node Node::operator[](const Key& key) const {
template <typename Key> template <typename Key>
inline Node Node::operator[](const Key& key) { inline Node Node::operator[](const Key& key) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory); detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory);
@ -421,14 +411,14 @@ inline Node Node::operator[](const Key& key) {
template <typename Key> template <typename Key>
inline bool Node::remove(const Key& key) { inline bool Node::remove(const Key& key) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
return m_pNode->remove(detail::to_value(key), m_pMemory); return m_pNode->remove(detail::to_value(key), m_pMemory);
} }
inline const Node Node::operator[](const Node& key) const { inline const Node Node::operator[](const Node& key) const {
if (!m_isValid || !key.m_isValid) if (!isValid() || !key.isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
key.EnsureNodeExists(); key.EnsureNodeExists();
@ -442,7 +432,7 @@ inline const Node Node::operator[](const Node& key) const {
} }
inline Node Node::operator[](const Node& key) { inline Node Node::operator[](const Node& key) {
if (!m_isValid || !key.m_isValid) if (!isValid() || !key.isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
key.EnsureNodeExists(); key.EnsureNodeExists();
@ -452,7 +442,7 @@ inline Node Node::operator[](const Node& key) {
} }
inline bool Node::remove(const Node& key) { inline bool Node::remove(const Node& key) {
if (!m_isValid || !key.m_isValid) if (!isValid() || !key.isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
key.EnsureNodeExists(); key.EnsureNodeExists();
@ -462,7 +452,7 @@ inline bool Node::remove(const Node& key) {
// map // map
template <typename Key, typename Value> template <typename Key, typename Value>
inline void Node::force_insert(const Key& key, const Value& value) { inline void Node::force_insert(const Key& key, const Value& value) {
if (!m_isValid) if (!isValid())
throw InvalidNode(); throw InvalidNode();
EnsureNodeExists(); EnsureNodeExists();
m_pNode->force_insert(detail::to_value(key), detail::to_value(value), m_pNode->force_insert(detail::to_value(key), detail::to_value(value),

View File

@ -132,9 +132,10 @@ class YAML_CPP_API Node {
void AssignNode(const Node& rhs); void AssignNode(const Node& rhs);
private: private:
bool m_isValid;
mutable detail::shared_memory_holder m_pMemory; mutable detail::shared_memory_holder m_pMemory;
mutable detail::node* m_pNode; mutable detail::node* m_pNode;
bool isValid() const { return m_pMemory; }
}; };
YAML_CPP_API bool operator==(const Node& lhs, const Node& rhs); YAML_CPP_API bool operator==(const Node& lhs, const Node& rhs);