mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Implemented map access by already-existing node
This commit is contained in:
@@ -18,34 +18,28 @@ namespace YAML
|
||||
class node
|
||||
{
|
||||
public:
|
||||
node();
|
||||
node(): m_pData(new node_data) {}
|
||||
|
||||
ValueType::value type() const { return m_pData->type(); }
|
||||
|
||||
ValueType::value type() const;
|
||||
void assign_data(const node& rhs);
|
||||
void set_scalar(const std::string& data);
|
||||
void set_data(const node& rhs) { m_pData = rhs.m_pData; }
|
||||
|
||||
void set_type(ValueType::value type) { m_pData->set_type(type); }
|
||||
void set_null() { m_pData->set_null(); }
|
||||
void set_scalar(const std::string& scalar) { m_pData->set_scalar(scalar); }
|
||||
|
||||
// indexing
|
||||
template<typename Key> shared_node operator[](const Key& key) const { return (static_cast<const node_data&>(*m_pData))[key]; }
|
||||
template<typename Key> shared_node operator[](const Key& key) { return (*m_pData)[key]; }
|
||||
template<typename Key> bool remove(const Key& key) { return m_pData->remove(key); }
|
||||
|
||||
shared_node operator[](shared_node pKey) const { return (static_cast<const node_data&>(*m_pData))[pKey]; }
|
||||
shared_node operator[](shared_node pKey) { return (*m_pData)[pKey]; }
|
||||
bool remove(shared_node pKey) { return m_pData->remove(pKey); }
|
||||
|
||||
private:
|
||||
shared_node_data m_pData;
|
||||
};
|
||||
|
||||
inline node::node()
|
||||
{
|
||||
}
|
||||
|
||||
inline ValueType::value node::type() const
|
||||
{
|
||||
return m_pData ? m_pData->type() : ValueType::Null;
|
||||
}
|
||||
|
||||
inline void node::assign_data(const node& rhs)
|
||||
{
|
||||
m_pData = rhs.m_pData;
|
||||
}
|
||||
|
||||
inline void node::set_scalar(const std::string& data)
|
||||
{
|
||||
m_pData.reset(new node_data(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -9,7 +9,8 @@
|
||||
#include "yaml-cpp/dll.h"
|
||||
#include "yaml-cpp/value/type.h"
|
||||
#include "yaml-cpp/value/ptr.h"
|
||||
#include <pair>
|
||||
#include <list>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace YAML
|
||||
@@ -19,12 +20,29 @@ namespace YAML
|
||||
class node_data
|
||||
{
|
||||
public:
|
||||
explicit node_data(const std::string& scalar);
|
||||
node_data();
|
||||
|
||||
ValueType::value type() const { return m_type; }
|
||||
const std::string scalar() const { return m_data; }
|
||||
void set_type(ValueType::value type);
|
||||
void set_null();
|
||||
void set_scalar(const std::string& scalar);
|
||||
|
||||
ValueType::value type() const { return m_isDefined ? m_type : ValueType::Undefined; }
|
||||
const std::string scalar() const { return m_scalar; }
|
||||
|
||||
// indexing
|
||||
template<typename Key> shared_node operator[](const Key& key) const;
|
||||
template<typename Key> shared_node operator[](const Key& key);
|
||||
template<typename Key> bool remove(const Key& key);
|
||||
|
||||
shared_node operator[](shared_node pKey) const;
|
||||
shared_node operator[](shared_node pKey);
|
||||
bool remove(shared_node pKey);
|
||||
|
||||
private:
|
||||
void convert_sequence_to_map();
|
||||
|
||||
private:
|
||||
bool m_isDefined;
|
||||
ValueType::value m_type;
|
||||
|
||||
// scalar
|
||||
@@ -36,7 +54,7 @@ namespace YAML
|
||||
|
||||
// map
|
||||
typedef std::pair<shared_node, shared_node> kv_pair;
|
||||
typedef std::vector<kv_pair> node_map;
|
||||
typedef std::list<kv_pair> node_map;
|
||||
node_map m_map;
|
||||
};
|
||||
}
|
||||
|
@@ -12,28 +12,37 @@
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
inline Value::Value(): m_pMemory(new detail::memory_holder)
|
||||
inline Value::Value(): m_pMemory(new detail::memory_holder), m_pNode(m_pMemory->create_node())
|
||||
{
|
||||
EnsureNodeExists();
|
||||
m_pNode->set_null();
|
||||
}
|
||||
|
||||
inline Value::Value(ValueType::value type): m_pMemory(new detail::memory_holder), m_pNode(m_pMemory->create_node())
|
||||
{
|
||||
m_pNode->set_type(type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Value::Value(const T& rhs): m_pMemory(new detail::memory_holder)
|
||||
inline Value::Value(const T& rhs): m_pMemory(new detail::memory_holder), m_pNode(m_pMemory->create_node())
|
||||
{
|
||||
Assign(rhs);
|
||||
}
|
||||
|
||||
inline Value::Value(const Value& rhs): m_pNode(rhs.m_pNode), m_pMemory(rhs.m_pMemory)
|
||||
inline Value::Value(const Value& rhs): m_pMemory(rhs.m_pMemory), m_pNode(rhs.m_pNode)
|
||||
{
|
||||
}
|
||||
|
||||
inline Value::Value(detail::shared_node pNode, detail::shared_memory_holder pMemory): m_pMemory(pMemory), m_pNode(pNode)
|
||||
{
|
||||
}
|
||||
|
||||
inline Value::~Value()
|
||||
{
|
||||
}
|
||||
|
||||
inline ValueType::value Value::Type() const
|
||||
{
|
||||
return m_pNode ? m_pNode->type() : ValueType::Undefined;
|
||||
return m_pNode->type();
|
||||
}
|
||||
|
||||
// access
|
||||
@@ -63,19 +72,16 @@ namespace YAML
|
||||
template<>
|
||||
inline void Value::Assign(const std::string& rhs)
|
||||
{
|
||||
EnsureNodeExists();
|
||||
m_pNode->set_scalar(rhs);
|
||||
}
|
||||
|
||||
inline void Value::Assign(const char *rhs)
|
||||
{
|
||||
EnsureNodeExists();
|
||||
m_pNode->set_scalar(rhs);
|
||||
}
|
||||
|
||||
inline void Value::Assign(char *rhs)
|
||||
{
|
||||
EnsureNodeExists();
|
||||
m_pNode->set_scalar(rhs);
|
||||
}
|
||||
|
||||
@@ -86,20 +92,10 @@ namespace YAML
|
||||
AssignNode(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Value::EnsureNodeExists()
|
||||
{
|
||||
if(!m_pNode)
|
||||
m_pNode = m_pMemory->create_node();
|
||||
}
|
||||
|
||||
|
||||
void Value::AssignData(const Value& rhs)
|
||||
{
|
||||
EnsureNodeExists();
|
||||
if(!rhs.m_pNode)
|
||||
throw std::runtime_error("Tried to assign an undefined value");
|
||||
|
||||
m_pNode->assign_data(*rhs.m_pNode);
|
||||
m_pNode->set_data(*rhs.m_pNode);
|
||||
m_pMemory->merge(*rhs.m_pMemory);
|
||||
}
|
||||
|
||||
@@ -139,64 +135,68 @@ namespace YAML
|
||||
template<typename Key>
|
||||
inline const Value Value::operator[](const Key& key) const
|
||||
{
|
||||
return Value();
|
||||
detail::shared_node pValue = (static_cast<const detail::node&>(*m_pNode))[key];
|
||||
return Value(pValue, m_pMemory);
|
||||
}
|
||||
|
||||
template<typename Key>
|
||||
inline Value Value::operator[](const Key& key)
|
||||
{
|
||||
return Value();
|
||||
detail::shared_node pValue = (*m_pNode)[key];
|
||||
return Value(pValue, m_pMemory);
|
||||
}
|
||||
|
||||
template<typename Key>
|
||||
inline bool Value::remove(const Key& key)
|
||||
{
|
||||
return false;
|
||||
return m_pNode->remove(key);
|
||||
}
|
||||
|
||||
inline const Value Value::operator[](const Value& key) const
|
||||
{
|
||||
return Value();
|
||||
detail::shared_node pValue = (static_cast<const detail::node&>(*m_pNode))[*key.m_pNode];
|
||||
return Value(pValue, m_pMemory);
|
||||
}
|
||||
|
||||
inline Value Value::operator[](const Value& key)
|
||||
{
|
||||
return Value();
|
||||
detail::shared_node pValue = (*m_pNode)[*key.m_pNode];
|
||||
return Value(pValue, m_pMemory);
|
||||
}
|
||||
|
||||
inline bool Value::remove(const Value& key)
|
||||
{
|
||||
return false;
|
||||
return m_pNode->remove(*key.m_pNode);
|
||||
}
|
||||
|
||||
inline const Value Value::operator[](const char *key) const
|
||||
{
|
||||
return Value();
|
||||
return operator[](std::string(key));
|
||||
}
|
||||
|
||||
inline Value Value::operator[](const char *key)
|
||||
{
|
||||
return Value();
|
||||
return operator[](std::string(key));
|
||||
}
|
||||
|
||||
inline bool Value::remove(const char *key)
|
||||
{
|
||||
return false;
|
||||
return m_pNode->remove(std::string(key));
|
||||
}
|
||||
|
||||
inline const Value Value::operator[](char *key) const
|
||||
{
|
||||
return Value();
|
||||
return operator[](static_cast<const char *>(key));
|
||||
}
|
||||
|
||||
inline Value Value::operator[](char *key)
|
||||
{
|
||||
return Value();
|
||||
return operator[](static_cast<const char *>(key));
|
||||
}
|
||||
|
||||
inline bool Value::remove(char *key)
|
||||
{
|
||||
return false;
|
||||
return remove(static_cast<const char *>(key));
|
||||
}
|
||||
|
||||
// free functions
|
||||
|
@@ -59,17 +59,18 @@ namespace YAML
|
||||
bool remove(char *key);
|
||||
|
||||
private:
|
||||
explicit Value(detail::shared_node pNode, detail::shared_memory_holder pMemory);
|
||||
|
||||
template<typename T> void Assign(const T& rhs);
|
||||
void Assign(const char *rhs);
|
||||
void Assign(char *rhs);
|
||||
|
||||
void EnsureNodeExists();
|
||||
void AssignData(const Value& rhs);
|
||||
void AssignNode(const Value& rhs);
|
||||
|
||||
private:
|
||||
detail::shared_node m_pNode;
|
||||
detail::shared_memory_holder m_pMemory;
|
||||
detail::shared_node m_pNode;
|
||||
};
|
||||
|
||||
int compare(const Value& lhs, const Value& rhs);
|
||||
|
Reference in New Issue
Block a user