2008-09-04 02:20:39 +04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include "parserstate.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "iterator.h"
|
2009-05-24 03:51:01 +04:00
|
|
|
#include "conversion.h"
|
2008-09-04 02:20:39 +04:00
|
|
|
|
|
|
|
namespace YAML
|
|
|
|
{
|
|
|
|
class Content;
|
|
|
|
class Scanner;
|
2009-07-11 03:39:14 +04:00
|
|
|
class Emitter;
|
2008-09-04 02:20:39 +04:00
|
|
|
|
|
|
|
enum CONTENT_TYPE { CT_NONE, CT_SCALAR, CT_SEQUENCE, CT_MAP };
|
|
|
|
|
|
|
|
class Node
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Node();
|
|
|
|
~Node();
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
void Parse(Scanner *pScanner, const ParserState& state);
|
|
|
|
|
|
|
|
CONTENT_TYPE GetType() const;
|
|
|
|
|
2008-11-18 07:20:07 +03:00
|
|
|
// file location of start of this node
|
|
|
|
int GetLine() const { return m_line; }
|
|
|
|
int GetColumn() const { return m_column; }
|
|
|
|
|
2008-09-04 02:20:39 +04:00
|
|
|
// accessors
|
|
|
|
Iterator begin() const;
|
|
|
|
Iterator end() const;
|
|
|
|
unsigned size() const;
|
|
|
|
|
2008-09-25 03:29:00 +04:00
|
|
|
// extraction of scalars
|
2009-05-24 03:51:01 +04:00
|
|
|
bool GetScalar(std::string& s) const;
|
|
|
|
|
|
|
|
// we can specialize this for other values
|
2008-09-04 02:20:39 +04:00
|
|
|
template <typename T>
|
2009-05-24 03:51:01 +04:00
|
|
|
bool Read(T& value) const;
|
2008-09-04 02:20:39 +04:00
|
|
|
|
2008-09-25 03:29:00 +04:00
|
|
|
template <typename T>
|
|
|
|
friend void operator >> (const Node& node, T& value);
|
2008-09-04 02:20:39 +04:00
|
|
|
|
2008-09-25 03:29:00 +04:00
|
|
|
// just for maps
|
2008-09-04 02:20:39 +04:00
|
|
|
template <typename T>
|
2008-09-25 03:29:00 +04:00
|
|
|
const Node& GetValue(const T& key) const;
|
2008-09-04 02:20:39 +04:00
|
|
|
|
2008-09-25 03:29:00 +04:00
|
|
|
template <typename T>
|
|
|
|
const Node& operator [] (const T& key) const;
|
|
|
|
|
|
|
|
const Node& operator [] (const char *key) const;
|
2008-09-04 02:20:39 +04:00
|
|
|
|
2008-09-25 03:29:00 +04:00
|
|
|
// just for sequences
|
2008-09-04 02:20:39 +04:00
|
|
|
const Node& operator [] (unsigned u) const;
|
|
|
|
const Node& operator [] (int i) const;
|
|
|
|
|
2009-05-23 01:48:05 +04:00
|
|
|
// for anchors/aliases
|
|
|
|
const Node *Identity() const { return m_pIdentity; }
|
|
|
|
bool IsAlias() const { return m_alias; }
|
|
|
|
bool IsReferenced() const { return m_referenced; }
|
|
|
|
|
2009-07-11 03:39:14 +04:00
|
|
|
// emitting
|
|
|
|
friend Emitter& operator << (Emitter& out, const Node& node);
|
2008-09-04 02:20:39 +04:00
|
|
|
|
|
|
|
// ordering
|
|
|
|
int Compare(const Node& rhs) const;
|
|
|
|
friend bool operator < (const Node& n1, const Node& n2);
|
|
|
|
|
2008-09-19 06:44:49 +04:00
|
|
|
private:
|
|
|
|
// shouldn't be copyable! (at least for now)
|
|
|
|
Node(const Node& rhs);
|
|
|
|
Node& operator = (const Node& rhs);
|
|
|
|
|
2008-09-04 02:20:39 +04:00
|
|
|
private:
|
|
|
|
void ParseHeader(Scanner *pScanner, const ParserState& state);
|
|
|
|
void ParseTag(Scanner *pScanner, const ParserState& state);
|
|
|
|
void ParseAnchor(Scanner *pScanner, const ParserState& state);
|
|
|
|
void ParseAlias(Scanner *pScanner, const ParserState& state);
|
|
|
|
|
|
|
|
private:
|
2008-11-18 07:20:07 +03:00
|
|
|
int m_line, m_column;
|
2008-09-04 02:20:39 +04:00
|
|
|
std::string m_anchor, m_tag;
|
|
|
|
Content *m_pContent;
|
|
|
|
bool m_alias;
|
2009-05-23 01:48:05 +04:00
|
|
|
const Node *m_pIdentity;
|
|
|
|
mutable bool m_referenced;
|
2008-09-04 02:20:39 +04:00
|
|
|
};
|
2008-09-25 03:29:00 +04:00
|
|
|
|
|
|
|
// templated things we need to keep inline in the header
|
|
|
|
template <typename T>
|
2009-05-24 03:51:01 +04:00
|
|
|
inline bool Node::Read(T& value) const {
|
|
|
|
std::string scalar;
|
|
|
|
if(!GetScalar(scalar))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return Convert(scalar, value);
|
2008-09-25 03:29:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void operator >> (const Node& node, T& value)
|
|
|
|
{
|
2009-05-24 03:51:01 +04:00
|
|
|
if(!node.Read(value))
|
2008-11-18 07:20:07 +03:00
|
|
|
throw InvalidScalar(node.m_line, node.m_column);
|
2008-09-25 03:29:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline const Node& Node::GetValue(const T& key) const
|
|
|
|
{
|
|
|
|
if(!m_pContent)
|
|
|
|
throw BadDereference();
|
|
|
|
|
|
|
|
for(Iterator it=begin();it!=end();++it) {
|
|
|
|
T t;
|
2009-05-24 03:51:01 +04:00
|
|
|
if(it.first().Read(t)) {
|
2008-09-25 03:29:00 +04:00
|
|
|
if(key == t)
|
|
|
|
return it.second();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-01 05:40:18 +03:00
|
|
|
throw MakeTypedKeyNotFound(m_line, m_column, key);
|
2008-09-25 03:29:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline const Node& Node::operator [] (const T& key) const
|
|
|
|
{
|
|
|
|
return GetValue(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const Node& Node::operator [] (const char *key) const
|
|
|
|
{
|
|
|
|
return GetValue(std::string(key));
|
|
|
|
}
|
2008-09-04 02:20:39 +04:00
|
|
|
}
|