mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-10 13:01:18 +00:00
Start of moving Value -> Node and Node -> old API Node (with a #define toggle)
This commit is contained in:
5
src/node/convert.cpp
Normal file
5
src/node/convert.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "yaml-cpp/value/convert.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
}
|
29
src/node/detail/memory.cpp
Normal file
29
src/node/detail/memory.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "yaml-cpp/value/detail/memory.h"
|
||||
#include "yaml-cpp/value/detail/node.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
void memory_holder::merge(memory_holder& rhs)
|
||||
{
|
||||
if(m_pMemory == rhs.m_pMemory)
|
||||
return;
|
||||
|
||||
m_pMemory->merge(*rhs.m_pMemory);
|
||||
rhs.m_pMemory = m_pMemory;
|
||||
}
|
||||
|
||||
node& memory::create_node()
|
||||
{
|
||||
shared_node pNode(new node);
|
||||
m_nodes.insert(pNode);
|
||||
return *pNode;
|
||||
}
|
||||
|
||||
void memory::merge(const memory& rhs)
|
||||
{
|
||||
m_nodes.insert(rhs.m_nodes.begin(), rhs.m_nodes.end());
|
||||
}
|
||||
}
|
||||
}
|
195
src/node/detail/node_data.cpp
Normal file
195
src/node/detail/node_data.cpp
Normal file
@@ -0,0 +1,195 @@
|
||||
#include "yaml-cpp/value/detail/node_data.h"
|
||||
#include "yaml-cpp/value/detail/memory.h"
|
||||
#include "yaml-cpp/value/detail/node.h"
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
std::string node_data::empty_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;
|
||||
}
|
||||
|
||||
// size/iterator
|
||||
std::size_t node_data::size() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const_node_iterator node_data::begin() const
|
||||
{
|
||||
switch(m_type) {
|
||||
case ValueType::Sequence: return const_node_iterator(m_sequence.begin());
|
||||
case ValueType::Map: return const_node_iterator(m_map.begin());
|
||||
default: return const_node_iterator();
|
||||
}
|
||||
}
|
||||
|
||||
node_iterator node_data::begin()
|
||||
{
|
||||
switch(m_type) {
|
||||
case ValueType::Sequence: return node_iterator(m_sequence.begin());
|
||||
case ValueType::Map: return node_iterator(m_map.begin());
|
||||
default: return node_iterator();
|
||||
}
|
||||
}
|
||||
|
||||
const_node_iterator node_data::end() const
|
||||
{
|
||||
switch(m_type) {
|
||||
case ValueType::Sequence: return const_node_iterator(m_sequence.end());
|
||||
case ValueType::Map: return const_node_iterator(m_map.end());
|
||||
default: return const_node_iterator();
|
||||
}
|
||||
}
|
||||
|
||||
node_iterator node_data::end()
|
||||
{
|
||||
switch(m_type) {
|
||||
case ValueType::Sequence: return node_iterator(m_sequence.end());
|
||||
case ValueType::Map: return node_iterator(m_map.end());
|
||||
default: return node_iterator();
|
||||
}
|
||||
}
|
||||
|
||||
// sequence
|
||||
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);
|
||||
}
|
||||
|
||||
void node_data::insert(node& key, node& value, shared_memory_holder /* pMemory */)
|
||||
{
|
||||
if(m_type != ValueType::Map)
|
||||
throw std::runtime_error("Can't insert into a non-map node");
|
||||
|
||||
m_map.push_back(kv_pair(&key, &value));
|
||||
}
|
||||
|
||||
// indexing
|
||||
node& node_data::get(node& key, shared_memory_holder pMemory) const
|
||||
{
|
||||
if(m_type != ValueType::Map)
|
||||
return pMemory->create_node();
|
||||
|
||||
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
|
||||
if(it->first->is(key))
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
return pMemory->create_node();
|
||||
}
|
||||
|
||||
node& node_data::get(node& key, shared_memory_holder pMemory)
|
||||
{
|
||||
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(pMemory);
|
||||
break;
|
||||
case ValueType::Map:
|
||||
break;
|
||||
}
|
||||
|
||||
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
|
||||
if(it->first->is(key))
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
node& value = pMemory->create_node();
|
||||
m_map.push_back(kv_pair(&key, &value));
|
||||
return value;
|
||||
}
|
||||
|
||||
bool node_data::remove(node& key, shared_memory_holder /* pMemory */)
|
||||
{
|
||||
if(m_type != ValueType::Map)
|
||||
return false;
|
||||
|
||||
for(node_map::iterator it=m_map.begin();it!=m_map.end();++it) {
|
||||
if(it->first->is(key)) {
|
||||
m_map.erase(it);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void node_data::convert_sequence_to_map(shared_memory_holder pMemory)
|
||||
{
|
||||
assert(m_type == ValueType::Sequence);
|
||||
|
||||
m_map.clear();
|
||||
for(std::size_t i=0;i<m_sequence.size();i++) {
|
||||
std::stringstream stream;
|
||||
stream << i;
|
||||
|
||||
node& key = pMemory->create_node();
|
||||
key.set_scalar(stream.str());
|
||||
m_map.push_back(kv_pair(&key, m_sequence[i]));
|
||||
}
|
||||
|
||||
m_sequence.clear();
|
||||
m_type = ValueType::Map;
|
||||
}
|
||||
}
|
||||
}
|
23
src/node/emit.cpp
Normal file
23
src/node/emit.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "yaml-cpp/value/emit.h"
|
||||
#include "yaml-cpp/emitfromevents.h"
|
||||
#include "yaml-cpp/emitter.h"
|
||||
#include "valueevents.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Emitter& operator << (Emitter& out, const Value& value)
|
||||
{
|
||||
EmitFromEvents emitFromEvents(out);
|
||||
ValueEvents events(value);
|
||||
events.Emit(emitFromEvents);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::ostream& operator << (std::ostream& out, const Value& value)
|
||||
{
|
||||
Emitter emitter;
|
||||
emitter << value;
|
||||
out << emitter.c_str();
|
||||
return out;
|
||||
}
|
||||
}
|
29
src/node/parse.cpp
Normal file
29
src/node/parse.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "yaml-cpp/value/parse.h"
|
||||
#include "yaml-cpp/value/value.h"
|
||||
#include "yaml-cpp/value/impl.h"
|
||||
#include "yaml-cpp/parser.h"
|
||||
#include "valuebuilder.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Value Parse(const std::string& input) {
|
||||
std::stringstream stream(input);
|
||||
return Parse(stream);
|
||||
}
|
||||
|
||||
Value Parse(const char *input) {
|
||||
std::stringstream stream(input);
|
||||
return Parse(stream);
|
||||
}
|
||||
|
||||
Value Parse(std::istream& input) {
|
||||
Parser parser(input);
|
||||
ValueBuilder builder;
|
||||
if(!parser.HandleNextDocument(builder))
|
||||
return Value();
|
||||
|
||||
return builder.Root();
|
||||
}
|
||||
}
|
128
src/node/valuebuilder.cpp
Normal file
128
src/node/valuebuilder.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "valuebuilder.h"
|
||||
#include "yaml-cpp/mark.h"
|
||||
#include "yaml-cpp/value.h"
|
||||
#include <cassert>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
ValueBuilder::ValueBuilder(): m_pMemory(new detail::memory_holder), m_pRoot(0), m_mapDepth(0)
|
||||
{
|
||||
m_anchors.push_back(0); // since the anchors start at 1
|
||||
}
|
||||
|
||||
ValueBuilder::~ValueBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
Value ValueBuilder::Root()
|
||||
{
|
||||
if(!m_pRoot)
|
||||
return Value();
|
||||
|
||||
return Value(*m_pRoot, m_pMemory);
|
||||
}
|
||||
|
||||
void ValueBuilder::OnDocumentStart(const Mark&)
|
||||
{
|
||||
}
|
||||
|
||||
void ValueBuilder::OnDocumentEnd()
|
||||
{
|
||||
}
|
||||
|
||||
void ValueBuilder::OnNull(const Mark& mark, anchor_t anchor)
|
||||
{
|
||||
detail::node& node = Push(anchor);
|
||||
node.set_null();
|
||||
Pop();
|
||||
}
|
||||
|
||||
void ValueBuilder::OnAlias(const Mark& /*mark*/, anchor_t anchor)
|
||||
{
|
||||
detail::node& node = *m_anchors[anchor];
|
||||
m_stack.push_back(&node);
|
||||
Pop();
|
||||
}
|
||||
|
||||
void ValueBuilder::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value)
|
||||
{
|
||||
detail::node& node = Push(anchor);
|
||||
node.set_scalar(value);
|
||||
Pop();
|
||||
}
|
||||
|
||||
void ValueBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor)
|
||||
{
|
||||
detail::node& node = Push(anchor);
|
||||
node.set_type(ValueType::Sequence);
|
||||
}
|
||||
|
||||
void ValueBuilder::OnSequenceEnd()
|
||||
{
|
||||
Pop();
|
||||
}
|
||||
|
||||
void ValueBuilder::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor)
|
||||
{
|
||||
detail::node& node = Push(anchor);
|
||||
node.set_type(ValueType::Map);
|
||||
m_mapDepth++;
|
||||
}
|
||||
|
||||
void ValueBuilder::OnMapEnd()
|
||||
{
|
||||
assert(m_mapDepth > 0);
|
||||
m_mapDepth--;
|
||||
Pop();
|
||||
}
|
||||
|
||||
detail::node& ValueBuilder::Push(anchor_t anchor)
|
||||
{
|
||||
const bool needsKey = (!m_stack.empty() && m_stack.back()->type() == ValueType::Map && m_keys.size() < m_mapDepth);
|
||||
|
||||
detail::node& node = m_pMemory->create_node();
|
||||
m_stack.push_back(&node);
|
||||
RegisterAnchor(anchor, node);
|
||||
|
||||
if(needsKey)
|
||||
m_keys.push_back(&node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void ValueBuilder::Pop()
|
||||
{
|
||||
assert(!m_stack.empty());
|
||||
if(m_stack.size() == 1) {
|
||||
m_pRoot = m_stack[0];
|
||||
m_stack.pop_back();
|
||||
return;
|
||||
}
|
||||
|
||||
detail::node& node = *m_stack.back();
|
||||
m_stack.pop_back();
|
||||
|
||||
detail::node& collection = *m_stack.back();
|
||||
|
||||
if(collection.type() == ValueType::Sequence) {
|
||||
collection.append(node, m_pMemory);
|
||||
} else if(collection.type() == ValueType::Map) {
|
||||
detail::node& key = *m_keys.back();
|
||||
if(&key != &node) {
|
||||
m_keys.pop_back();
|
||||
collection.insert(key, node, m_pMemory);
|
||||
}
|
||||
} else {
|
||||
assert(false);
|
||||
m_stack.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void ValueBuilder::RegisterAnchor(anchor_t anchor, detail::node& node)
|
||||
{
|
||||
if(anchor) {
|
||||
assert(anchor == m_anchors.size());
|
||||
m_anchors.push_back(&node);
|
||||
}
|
||||
}
|
||||
}
|
56
src/node/valuebuilder.h
Normal file
56
src/node/valuebuilder.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef VALUE_VALUEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define VALUE_VALUEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "yaml-cpp/eventhandler.h"
|
||||
#include "yaml-cpp/value/ptr.h"
|
||||
#include <vector>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Value;
|
||||
|
||||
class ValueBuilder: public EventHandler
|
||||
{
|
||||
public:
|
||||
ValueBuilder();
|
||||
virtual ~ValueBuilder();
|
||||
|
||||
Value Root();
|
||||
|
||||
virtual void OnDocumentStart(const Mark& mark);
|
||||
virtual void OnDocumentEnd();
|
||||
|
||||
virtual void OnNull(const Mark& mark, anchor_t anchor);
|
||||
virtual void OnAlias(const Mark& mark, anchor_t anchor);
|
||||
virtual void OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value);
|
||||
|
||||
virtual void OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor);
|
||||
virtual void OnSequenceEnd();
|
||||
|
||||
virtual void OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor);
|
||||
virtual void OnMapEnd();
|
||||
|
||||
private:
|
||||
detail::node& Push(anchor_t anchor);
|
||||
void Pop();
|
||||
void RegisterAnchor(anchor_t anchor, detail::node& node);
|
||||
|
||||
private:
|
||||
detail::shared_memory_holder m_pMemory;
|
||||
detail::node *m_pRoot;
|
||||
|
||||
typedef std::vector<detail::node *> Nodes;
|
||||
Nodes m_stack;
|
||||
Nodes m_anchors;
|
||||
|
||||
Nodes m_keys;
|
||||
std::size_t m_mapDepth;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // VALUE_VALUEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
98
src/node/valueevents.cpp
Normal file
98
src/node/valueevents.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "valueevents.h"
|
||||
#include "yaml-cpp/value.h"
|
||||
#include "yaml-cpp/eventhandler.h"
|
||||
#include "yaml-cpp/mark.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
void ValueEvents::AliasManager::RegisterReference(const detail::node& node)
|
||||
{
|
||||
m_anchorByIdentity.insert(std::make_pair(node.ref(), _CreateNewAnchor()));
|
||||
}
|
||||
|
||||
anchor_t ValueEvents::AliasManager::LookupAnchor(const detail::node& node) const
|
||||
{
|
||||
AnchorByIdentity::const_iterator it = m_anchorByIdentity.find(node.ref());
|
||||
if(it == m_anchorByIdentity.end())
|
||||
return 0;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
ValueEvents::ValueEvents(const Value& value): m_pMemory(value.m_pMemory), m_root(*value.m_pNode)
|
||||
{
|
||||
Setup(m_root);
|
||||
}
|
||||
|
||||
void ValueEvents::Setup(const detail::node& node)
|
||||
{
|
||||
int& refCount = m_refCount[node.ref()];
|
||||
refCount++;
|
||||
if(refCount > 1)
|
||||
return;
|
||||
|
||||
if(node.type() == ValueType::Sequence) {
|
||||
for(detail::const_node_iterator it=node.begin();it!=node.end();++it)
|
||||
Setup(**it);
|
||||
} else if(node.type() == ValueType::Map) {
|
||||
for(detail::const_node_iterator it=node.begin();it!=node.end();++it) {
|
||||
Setup(*it->first);
|
||||
Setup(*it->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ValueEvents::Emit(EventHandler& handler)
|
||||
{
|
||||
AliasManager am;
|
||||
|
||||
handler.OnDocumentStart(Mark());
|
||||
Emit(m_root, handler, am);
|
||||
handler.OnDocumentEnd();
|
||||
}
|
||||
|
||||
void ValueEvents::Emit(const detail::node& node, EventHandler& handler, AliasManager& am) const
|
||||
{
|
||||
anchor_t anchor = NullAnchor;
|
||||
if(IsAliased(node)) {
|
||||
anchor = am.LookupAnchor(node);
|
||||
if(anchor) {
|
||||
handler.OnAlias(Mark(), anchor);
|
||||
return;
|
||||
}
|
||||
|
||||
am.RegisterReference(node);
|
||||
anchor = am.LookupAnchor(node);
|
||||
}
|
||||
|
||||
switch(node.type()) {
|
||||
case ValueType::Undefined:
|
||||
break;
|
||||
case ValueType::Null:
|
||||
handler.OnNull(Mark(), anchor);
|
||||
break;
|
||||
case ValueType::Scalar:
|
||||
handler.OnScalar(Mark(), "", anchor, node.scalar());
|
||||
break;
|
||||
case ValueType::Sequence:
|
||||
handler.OnSequenceStart(Mark(), "", anchor);
|
||||
for(detail::const_node_iterator it=node.begin();it!=node.end();++it)
|
||||
Emit(**it, handler, am);
|
||||
handler.OnSequenceEnd();
|
||||
break;
|
||||
case ValueType::Map:
|
||||
handler.OnMapStart(Mark(), "", anchor);
|
||||
for(detail::const_node_iterator it=node.begin();it!=node.end();++it) {
|
||||
Emit(*it->first, handler, am);
|
||||
Emit(*it->second, handler, am);
|
||||
}
|
||||
handler.OnMapEnd();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool ValueEvents::IsAliased(const detail::node& node) const
|
||||
{
|
||||
RefCount::const_iterator it = m_refCount.find(node.ref());
|
||||
return it != m_refCount.end() && it->second > 1;
|
||||
}
|
||||
}
|
57
src/node/valueevents.h
Normal file
57
src/node/valueevents.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef VALUE_VALUEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define VALUE_VALUEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "yaml-cpp/anchor.h"
|
||||
#include "yaml-cpp/value/ptr.h"
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Value;
|
||||
class EventHandler;
|
||||
|
||||
class ValueEvents
|
||||
{
|
||||
public:
|
||||
explicit ValueEvents(const Value& value);
|
||||
|
||||
void Emit(EventHandler& handler);
|
||||
|
||||
private:
|
||||
class AliasManager {
|
||||
public:
|
||||
AliasManager(): m_curAnchor(0) {}
|
||||
|
||||
void RegisterReference(const detail::node& node);
|
||||
anchor_t LookupAnchor(const detail::node& node) const;
|
||||
|
||||
private:
|
||||
anchor_t _CreateNewAnchor() { return ++m_curAnchor; }
|
||||
|
||||
private:
|
||||
typedef std::map<const detail::node_ref*, anchor_t> AnchorByIdentity;
|
||||
AnchorByIdentity m_anchorByIdentity;
|
||||
|
||||
anchor_t m_curAnchor;
|
||||
};
|
||||
|
||||
void Setup(const detail::node& node);
|
||||
void Emit(const detail::node& node, EventHandler& handler, AliasManager& am) const;
|
||||
bool IsAliased(const detail::node& node) const;
|
||||
|
||||
private:
|
||||
detail::shared_memory_holder m_pMemory;
|
||||
detail::node& m_root;
|
||||
|
||||
typedef std::map<const detail::node_ref *, int> RefCount;
|
||||
RefCount m_refCount;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // VALUE_VALUEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
Reference in New Issue
Block a user