Added append()

This commit is contained in:
Jesse Beder 2011-09-09 02:51:35 -05:00
parent 4f8680b540
commit d3bbd08273
7 changed files with 37 additions and 1 deletions

View File

@ -33,6 +33,8 @@ namespace YAML
void set_null() { m_pRef->set_null(); }
void set_scalar(const std::string& scalar) { m_pRef->set_scalar(scalar); }
void append(node& node, shared_memory_holder pMemory) { m_pRef->append(node, pMemory); }
// indexing
template<typename Key> node& get(const Key& key, shared_memory_holder pMemory) const { return static_cast<const node_ref&>(*m_pRef).get(key, pMemory); }
template<typename Key> node& get(const Key& key, shared_memory_holder pMemory) { return m_pRef->get(key, pMemory); }

View File

@ -30,6 +30,8 @@ namespace YAML
ValueType::value type() const { return m_isDefined ? m_type : ValueType::Undefined; }
const std::string& scalar() const { return m_scalar; }
void append(node& node, shared_memory_holder pMemory);
// indexing
template<typename Key> node& get(const Key& key, shared_memory_holder pMemory) const;
template<typename Key> node& get(const Key& key, shared_memory_holder pMemory);

View File

@ -30,6 +30,8 @@ namespace YAML
void set_null() { m_pData->set_null(); }
void set_scalar(const std::string& scalar) { m_pData->set_scalar(scalar); }
void append(node& node, shared_memory_holder pMemory) { m_pData->append(node, pMemory); }
// indexing
template<typename Key> node& get(const Key& key, shared_memory_holder pMemory) const { return static_cast<const node_data&>(*m_pData).get(key, pMemory); }
template<typename Key> node& get(const Key& key, shared_memory_holder pMemory) { return m_pData->get(key, pMemory); }

View File

@ -150,6 +150,19 @@ namespace YAML
return iterator();
}
// sequence
template<typename T>
inline void Value::append(const T& rhs)
{
append(Value(rhs));
}
inline void Value::append(const Value& rhs)
{
m_pNode->append(*rhs.m_pNode, m_pMemory);
m_pMemory->merge(*rhs.m_pMemory);
}
// indexing
template<typename Key>
inline const Value Value::operator[](const Key& key) const

View File

@ -45,6 +45,10 @@ namespace YAML
const_iterator end() const;
iterator end();
// sequence
template<typename T> void append(const T& rhs);
void append(const Value& rhs);
// indexing
template<typename Key> const Value operator[](const Key& key) const;
template<typename Key> Value operator[](const Key& key);

View File

@ -2,6 +2,7 @@
#include "yaml-cpp/value/detail/memory.h"
#include "yaml-cpp/value/detail/node.h"
#include <sstream>
#include <stdexcept>
namespace YAML
{
@ -57,6 +58,14 @@ namespace YAML
m_scalar = scalar;
}
void node_data::append(node& node, shared_memory_holder /* pMemory */)
{
if(m_type != ValueType::Sequence)
throw std::runtime_error("Can't append to a non-sequence node");
m_sequence.push_back(&node);
}
// indexing
node& node_data::get(node& key, shared_memory_holder pMemory) const
{

View File

@ -22,7 +22,11 @@ int main()
value["this"] = value;
value["this"]["change"] = value;
value["this"]["change"] = 5;
value["seq"] = YAML::Value(YAML::ValueType::Sequence);
value["seq"].append(2);
value["seq"].append(3);
value["seq"].append("five");
return 0;
}