Implemented map access by already-existing node

This commit is contained in:
Jesse Beder
2011-09-07 02:39:59 -05:00
parent a530630f1c
commit fed95c5da4
5 changed files with 197 additions and 63 deletions

View File

@@ -18,34 +18,28 @@ namespace YAML
class node
{
public:
node();
node(): m_pData(new node_data) {}
ValueType::value type() const;
void assign_data(const node& rhs);
void set_scalar(const std::string& data);
ValueType::value type() const { return m_pData->type(); }
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));
}
}
}

View File

@@ -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;
};
}

View File

@@ -12,18 +12,27 @@
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)
{
}
@@ -33,7 +42,7 @@ namespace YAML
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);
}
@@ -87,19 +93,9 @@ namespace YAML
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

View File

@@ -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);

View File

@@ -1,11 +1,132 @@
#include "yaml-cpp/value/detail/node_data.h"
#include "yaml-cpp/value/detail/node.h"
#include <sstream>
namespace YAML
{
namespace detail
{
node_data::node_data(const std::string& scalar): m_type(ValueType::Scalar), m_scalar(scalar)
node_data::node_data(): m_isDefined(false), m_type(ValueType::Null)
{
}
void node_data::set_type(ValueType::value type)
{
if(type == ValueType::Undefined) {
m_type = type;
m_isDefined = false;
return;
}
m_isDefined = true;
if(type == m_type)
return;
m_type = type;
switch(m_type) {
case ValueType::Null:
break;
case ValueType::Scalar:
m_scalar.clear();
break;
case ValueType::Sequence:
m_sequence.clear();
break;
case ValueType::Map:
m_map.clear();
break;
case ValueType::Undefined:
assert(false);
break;
}
}
void node_data::set_null()
{
m_isDefined = true;
m_type = ValueType::Null;
}
void node_data::set_scalar(const std::string& scalar)
{
m_isDefined = true;
m_type = ValueType::Scalar;
m_scalar = scalar;
}
// indexing
shared_node node_data::operator[](shared_node pKey) const
{
if(m_type != ValueType::Map)
return shared_node(new node);
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first == pKey)
return it->second;
}
return shared_node(new node);
}
shared_node node_data::operator[](shared_node pKey)
{
switch(m_type) {
case ValueType::Undefined:
case ValueType::Null:
case ValueType::Scalar:
m_type = ValueType::Map;
m_map.clear();
break;
case ValueType::Sequence:
convert_sequence_to_map();
break;
case ValueType::Map:
break;
}
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first == pKey)
return it->second;
}
shared_node pValue(new node);
m_map.push_back(kv_pair(pKey, pValue));
return pValue;
}
bool node_data::remove(shared_node pKey)
{
if(m_type != ValueType::Map)
return false;
for(node_map::iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first == pKey) {
m_map.erase(it);
return true;
}
}
return false;
}
void node_data::convert_sequence_to_map()
{
assert(m_type == ValueType::Sequence);
m_map.clear();
for(std::size_t i=0;i<m_sequence.size();i++) {
std::stringstream stream;
stream << i;
shared_node pKey(new node);
pKey->set_scalar(stream.str());
m_map.push_back(kv_pair(pKey, m_sequence[i]));
}
m_sequence.clear();
m_type = ValueType::Map;
}
}
}