Implemented sugar Parse() functions

This commit is contained in:
Jesse Beder
2011-09-09 23:40:19 -05:00
parent 6e03bebeb0
commit 09beb5c47a
8 changed files with 67 additions and 20 deletions

View File

@@ -11,5 +11,6 @@
#include "yaml-cpp/value/convert.h"
#include "yaml-cpp/value/iterator.h"
#include "yaml-cpp/value/detail/impl.h"
#include "yaml-cpp/value/parse.h"
#endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -112,13 +112,13 @@ namespace YAML
return *this;
}
void Value::AssignData(const Value& rhs)
inline void Value::AssignData(const Value& rhs)
{
m_pNode->set_data(*rhs.m_pNode);
m_pMemory->merge(*rhs.m_pMemory);
}
void Value::AssignNode(const Value& rhs)
inline void Value::AssignNode(const Value& rhs)
{
m_pNode->set_ref(*rhs.m_pNode);
m_pMemory->merge(*rhs.m_pMemory);

View File

@@ -0,0 +1,21 @@
#ifndef VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define VALUE_PARSE_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 <string>
#include <iosfwd>
namespace YAML
{
class Value;
Value Parse(const std::string& input);
Value Parse(const char *input);
Value Parse(std::istream& input);
}
#endif // VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -17,6 +17,7 @@ namespace YAML
class Value
{
public:
friend class ValueBuilder;
friend class detail::node_data;
template<typename, typename, typename> friend class detail::iterator_base;

29
src/value/parse.cpp Normal file
View 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();
}
}

View File

@@ -14,6 +14,14 @@ namespace YAML
{
}
Value ValueBuilder::Root()
{
if(!m_pRoot)
return Value();
return Value(*m_pRoot, m_pMemory);
}
void ValueBuilder::OnDocumentStart(const Mark&)
{
}
@@ -70,13 +78,13 @@ namespace YAML
detail::node& ValueBuilder::Push(anchor_t anchor)
{
detail::node& top = *m_stack.back();
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(top.type() == ValueType::Map && m_keys.size() < m_mapDepth)
if(needsKey)
m_keys.push_back(&node);
return node;

View File

@@ -19,6 +19,8 @@ namespace YAML
ValueBuilder();
virtual ~ValueBuilder();
Value Root();
virtual void OnDocumentStart(const Mark& mark);
virtual void OnDocumentEnd();

View File

@@ -3,22 +3,7 @@
int main()
{
YAML::Value value;
value["seq"] = YAML::Value(YAML::ValueType::Sequence);
for(int i=0;i<5;i++)
value["seq"].append(i);
value["map"]["one"] = "I";
value["map"]["two"] = "II";
value["map"]["three"] = "III";
value["map"]["four"] = "IV";
for(YAML::const_iterator it=value["seq"].begin();it!=value["seq"].end();++it) {
std::cout << it->as<int>() << "\n";
}
for(YAML::const_iterator it=value["map"].begin();it!=value["map"].end();++it) {
std::cout << it->first.as<std::string>() << " -> " << it->second.as<std::string>() << "\n";
}
YAML::Value value = YAML::Parse("foo: bar");
return 0;
}