Set the 'memory' to only store node_refs, not nodes

This commit is contained in:
Jesse Beder 2011-09-07 14:56:04 -05:00
parent 4aa61944fe
commit 1ab16bac62
8 changed files with 32 additions and 31 deletions

View File

@ -18,14 +18,14 @@ namespace YAML
inline shared_node node_data::get(const Key& key, shared_memory_holder pMemory) const
{
if(m_type != ValueType::Map)
return shared_node(new node);
return pMemory->create_node();
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
if(equals(it->first, key, pMemory))
return it->second;
}
return shared_node(new node);
return pMemory->create_node();
}
template<typename Key>
@ -41,7 +41,7 @@ namespace YAML
m_map.clear();
break;
case ValueType::Sequence:
convert_sequence_to_map();
convert_sequence_to_map(pMemory);
break;
case ValueType::Map:
break;
@ -53,7 +53,7 @@ namespace YAML
}
shared_node pKey = convert_to_node(key, pMemory);
shared_node pValue(new node);
shared_node pValue = pMemory->create_node();
m_map.push_back(kv_pair(pKey, pValue));
return pValue;
}

View File

@ -19,7 +19,7 @@ namespace YAML
void merge(const memory& rhs);
private:
typedef std::set<shared_node> Nodes;
typedef std::set<shared_node_ref> Nodes;
Nodes m_nodes;
};

View File

@ -18,7 +18,7 @@ namespace YAML
class node
{
public:
node(): m_pRef(new node_ref) {}
explicit node(shared_node_ref pRef): m_pRef(pRef) {}
ValueType::value type() const { return m_pRef->type(); }
@ -36,9 +36,9 @@ namespace YAML
template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) { return m_pRef->get(key, pMemory); }
template<typename Key> bool remove(const Key& key, shared_memory_holder pMemory) { return m_pRef->remove(key, pMemory); }
shared_node get(shared_node pKey) const { return static_cast<const node_ref&>(*m_pRef).get(pKey); }
shared_node get(shared_node pKey) { return m_pRef->get(pKey); }
bool remove(shared_node pKey) { return m_pRef->remove(pKey); }
shared_node get(shared_node pKey, shared_memory_holder pMemory) const { return static_cast<const node_ref&>(*m_pRef).get(pKey, pMemory); }
shared_node get(shared_node pKey, shared_memory_holder pMemory) { return m_pRef->get(pKey, pMemory); }
bool remove(shared_node pKey, shared_memory_holder pMemory) { return m_pRef->remove(pKey, pMemory); }
private:
shared_node_ref m_pRef;

View File

@ -34,12 +34,12 @@ namespace YAML
template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory);
template<typename Key> bool remove(const Key& key, shared_memory_holder pMemory);
shared_node get(shared_node pKey) const;
shared_node get(shared_node pKey);
bool remove(shared_node pKey);
shared_node get(shared_node pKey, shared_memory_holder pMemory) const;
shared_node get(shared_node pKey, shared_memory_holder pMemory);
bool remove(shared_node pKey, shared_memory_holder pMemory);
private:
void convert_sequence_to_map();
void convert_sequence_to_map(shared_memory_holder pMemory);
template<typename T>
static bool equals(detail::shared_node pNode, const T& rhs, detail::shared_memory_holder pMemory);

View File

@ -33,9 +33,9 @@ namespace YAML
template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) { return m_pData->get(key, pMemory); }
template<typename Key> bool remove(const Key& key, shared_memory_holder pMemory) { return m_pData->remove(key, pMemory); }
shared_node get(shared_node pKey) const { return static_cast<const node_data&>(*m_pData).get(pKey); }
shared_node get(shared_node pKey) { return m_pData->get(pKey); }
bool remove(shared_node pKey) { return m_pData->remove(pKey); }
shared_node get(shared_node pKey, shared_memory_holder pMemory) const { return static_cast<const node_data&>(*m_pData).get(pKey, pMemory); }
shared_node get(shared_node pKey, shared_memory_holder pMemory) { return m_pData->get(pKey, pMemory); }
bool remove(shared_node pKey, shared_memory_holder pMemory) { return m_pData->remove(pKey, pMemory); }
private:
shared_node_data m_pData;

View File

@ -160,19 +160,19 @@ namespace YAML
inline const Value Value::operator[](const Value& key) const
{
detail::shared_node pValue = static_cast<const detail::node&>(*m_pNode).get(key.m_pNode);
detail::shared_node pValue = static_cast<const detail::node&>(*m_pNode).get(key.m_pNode, m_pMemory);
return Value(pValue, m_pMemory);
}
inline Value Value::operator[](const Value& key)
{
detail::shared_node pValue = m_pNode->get(key.m_pNode);
detail::shared_node pValue = m_pNode->get(key.m_pNode, m_pMemory);
return Value(pValue, m_pMemory);
}
inline bool Value::remove(const Value& key)
{
return m_pNode->remove(key.m_pNode);
return m_pNode->remove(key.m_pNode, m_pMemory);
}
inline const Value Value::operator[](const char *key) const

View File

@ -16,9 +16,9 @@ namespace YAML
shared_node memory::create_node()
{
shared_node pNode(new node);
m_nodes.insert(pNode);
return pNode;
shared_node_ref pRef(new node_ref);
m_nodes.insert(pRef);
return shared_node(new node(pRef));
}
void memory::merge(const memory& rhs)

View File

@ -1,4 +1,5 @@
#include "yaml-cpp/value/detail/node_data.h"
#include "yaml-cpp/value/detail/memory.h"
#include "yaml-cpp/value/detail/node.h"
#include <sstream>
@ -57,20 +58,20 @@ namespace YAML
}
// indexing
shared_node node_data::get(shared_node pKey) const
shared_node node_data::get(shared_node pKey, shared_memory_holder pMemory) const
{
if(m_type != ValueType::Map)
return shared_node(new node);
return pMemory->create_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);
return pMemory->create_node();
}
shared_node node_data::get(shared_node pKey)
shared_node node_data::get(shared_node pKey, shared_memory_holder pMemory)
{
switch(m_type) {
case ValueType::Undefined:
@ -80,7 +81,7 @@ namespace YAML
m_map.clear();
break;
case ValueType::Sequence:
convert_sequence_to_map();
convert_sequence_to_map(pMemory);
break;
case ValueType::Map:
break;
@ -91,12 +92,12 @@ namespace YAML
return it->second;
}
shared_node pValue(new node);
shared_node pValue = pMemory->create_node();
m_map.push_back(kv_pair(pKey, pValue));
return pValue;
}
bool node_data::remove(shared_node pKey)
bool node_data::remove(shared_node pKey, shared_memory_holder /* pMemory */)
{
if(m_type != ValueType::Map)
return false;
@ -111,7 +112,7 @@ namespace YAML
return false;
}
void node_data::convert_sequence_to_map()
void node_data::convert_sequence_to_map(shared_memory_holder pMemory)
{
assert(m_type == ValueType::Sequence);
@ -120,7 +121,7 @@ namespace YAML
std::stringstream stream;
stream << i;
shared_node pKey(new node);
shared_node pKey = pMemory->create_node();
pKey->set_scalar(stream.str());
m_map.push_back(kv_pair(pKey, m_sequence[i]));
}