diff --git a/include/yaml-cpp/aliasmanager.h b/include/yaml-cpp/aliasmanager.h index 8b01a69..d650269 100644 --- a/include/yaml-cpp/aliasmanager.h +++ b/include/yaml-cpp/aliasmanager.h @@ -18,18 +18,12 @@ namespace YAML AliasManager(); void RegisterReference(const Node& node); - - const Node *LookupReference(const Node& node) const; anchor_t LookupAnchor(const Node& node) const; private: - const Node *_LookupReference(const Node& oldIdentity) const; anchor_t _CreateNewAnchor(); private: - typedef std::map NodeByNode; - NodeByNode m_newIdentityByOldIdentity; - typedef std::map AnchorByIdentity; AnchorByIdentity m_anchorByIdentity; diff --git a/include/yaml-cpp/contrib/graphbuilder.h b/include/yaml-cpp/contrib/graphbuilder.h index f2237e3..7c09fb2 100644 --- a/include/yaml-cpp/contrib/graphbuilder.h +++ b/include/yaml-cpp/contrib/graphbuilder.h @@ -20,7 +20,7 @@ namespace YAML { public: // Create and return a new node with a null value. - virtual void *NewNull(const std::string& tag, void *pParentNode) = 0; + virtual void *NewNull(const Mark& mark, void *pParentNode) = 0; // Create and return a new node with the given tag and value. virtual void *NewScalar(const Mark& mark, const std::string& tag, void *pParentNode, const std::string& value) = 0; @@ -74,8 +74,8 @@ namespace YAML GraphBuilderInterface& AsBuilderInterface() {return *this;} - virtual void *NewNull(const std::string& tag, void* pParentNode) { - return CheckType(m_impl.NewNull(tag, AsNode(pParentNode))); + virtual void *NewNull(const Mark& mark, void* pParentNode) { + return CheckType(m_impl.NewNull(mark, AsNode(pParentNode))); } virtual void *NewScalar(const Mark& mark, const std::string& tag, void *pParentNode, const std::string& value) { diff --git a/include/yaml-cpp/emitfromevents.h b/include/yaml-cpp/emitfromevents.h index 6b4f8ff..4f9badc 100644 --- a/include/yaml-cpp/emitfromevents.h +++ b/include/yaml-cpp/emitfromevents.h @@ -20,7 +20,7 @@ namespace YAML virtual void OnDocumentStart(const Mark& mark); virtual void OnDocumentEnd(); - virtual void OnNull(const std::string& tag, anchor_t anchor); + 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); diff --git a/include/yaml-cpp/eventhandler.h b/include/yaml-cpp/eventhandler.h index bba874f..80afdb4 100644 --- a/include/yaml-cpp/eventhandler.h +++ b/include/yaml-cpp/eventhandler.h @@ -20,7 +20,7 @@ namespace YAML virtual void OnDocumentStart(const Mark& mark) = 0; virtual void OnDocumentEnd() = 0; - virtual void OnNull(const std::string& tag, anchor_t anchor) = 0; + virtual void OnNull(const Mark& mark, anchor_t anchor) = 0; virtual void OnAlias(const Mark& mark, anchor_t anchor) = 0; virtual void OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value) = 0; diff --git a/include/yaml-cpp/iterator.h b/include/yaml-cpp/iterator.h index 069e91d..e1d95e4 100644 --- a/include/yaml-cpp/iterator.h +++ b/include/yaml-cpp/iterator.h @@ -5,6 +5,7 @@ #pragma once #endif +#include namespace YAML { @@ -15,7 +16,7 @@ namespace YAML { public: Iterator(); - Iterator(IterPriv *pData); + Iterator(std::auto_ptr pData); Iterator(const Iterator& rhs); ~Iterator(); @@ -31,7 +32,7 @@ namespace YAML friend bool operator != (const Iterator& it, const Iterator& jt); private: - IterPriv *m_pData; + std::auto_ptr m_pData; }; } diff --git a/src/ltnode.h b/include/yaml-cpp/ltnode.h similarity index 100% rename from src/ltnode.h rename to include/yaml-cpp/ltnode.h diff --git a/include/yaml-cpp/node.h b/include/yaml-cpp/node.h index 993434f..c0fa379 100644 --- a/include/yaml-cpp/node.h +++ b/include/yaml-cpp/node.h @@ -9,28 +9,32 @@ #include "yaml-cpp/conversion.h" #include "yaml-cpp/exceptions.h" #include "yaml-cpp/iterator.h" +#include "yaml-cpp/ltnode.h" #include "yaml-cpp/mark.h" #include "yaml-cpp/noncopyable.h" #include -#include -#include #include #include +#include +#include namespace YAML { class AliasManager; class Content; + class NodeOwnership; class Scanner; class Emitter; class EventHandler; - struct NodeProperties; - enum CONTENT_TYPE { CT_NONE, CT_SCALAR, CT_SEQUENCE, CT_MAP }; + struct NodeType { enum value { Null, Scalar, Sequence, Map }; }; class Node: private noncopyable { public: + friend class NodeOwnership; + friend class NodeBuilder; + Node(); ~Node(); @@ -38,16 +42,9 @@ namespace YAML std::auto_ptr Clone() const; void EmitEvents(EventHandler& eventHandler) const; void EmitEvents(AliasManager& am, EventHandler& eventHandler) const; - - void Init(CONTENT_TYPE type, const Mark& mark, const std::string& tag); - void InitNull(const std::string& tag); - void InitAlias(const Mark& mark, const Node& identity); - void SetData(const std::string& data); - void Append(std::auto_ptr pNode); - void Insert(std::auto_ptr pKey, std::auto_ptr pValue); - - CONTENT_TYPE GetType() const { return m_type; } + NodeType::value Type() const { return m_type; } + bool IsAliased() const; // file location of start of this node const Mark GetMark() const { return m_mark; } @@ -84,13 +81,8 @@ namespace YAML const Node *FindValue(const char *key) const; const Node& operator [] (const char *key) const; - // for anchors/aliases - const Node *Identity() const { return m_pIdentity; } - bool IsAlias() const { return m_alias; } - bool IsReferenced() const { return m_referenced; } - // for tags - const std::string GetTag() const { return IsAlias() ? m_pIdentity->GetTag() : m_tag; } + const std::string& Tag() const { return m_tag; } // emitting friend Emitter& operator << (Emitter& out, const Node& node); @@ -100,6 +92,16 @@ namespace YAML friend bool operator < (const Node& n1, const Node& n2); private: + explicit Node(NodeOwnership& owner); + Node& CreateNode(); + + void Init(NodeType::value type, const Mark& mark, const std::string& tag); + + void MarkAsAliased(); + void SetScalarData(const std::string& data); + void Append(Node& node); + void Insert(Node& key, Node& value); + // helper for sequences template friend struct _FindFromNodeAtIndex; const Node *FindAtIndex(std::size_t i) const; @@ -112,13 +114,18 @@ namespace YAML const Node *FindValueForKey(const T& key) const; private: + std::auto_ptr m_pOwnership; + Mark m_mark; std::string m_tag; - CONTENT_TYPE m_type; - Content *m_pContent; - bool m_alias; - const Node *m_pIdentity; - mutable bool m_referenced; + + typedef std::vector node_seq; + typedef std::map node_map; + + NodeType::value m_type; + std::string m_scalarData; + node_seq m_seqData; + node_map m_mapData; }; // comparisons with auto-conversion diff --git a/include/yaml-cpp/nodeimpl.h b/include/yaml-cpp/nodeimpl.h index 12b25b9..21dc276 100644 --- a/include/yaml-cpp/nodeimpl.h +++ b/include/yaml-cpp/nodeimpl.h @@ -7,6 +7,7 @@ #include "yaml-cpp/nodeutil.h" +#include namespace YAML { @@ -31,14 +32,17 @@ namespace YAML template inline const Node *Node::FindValue(const T& key) const { - switch(GetType()) { - case CT_MAP: - return FindValueForKey(key); - case CT_SEQUENCE: + switch(m_type) { + case NodeType::Null: + case NodeType::Scalar: + throw BadDereference(); + case NodeType::Sequence: return FindFromNodeAtIndex(*this, key); - default: - return 0; + case NodeType::Map: + return FindValueForKey(key); } + assert(false); + throw BadDereference(); } template @@ -56,14 +60,9 @@ namespace YAML template inline const Node& Node::GetValue(const T& key) const { - if(!m_pContent) - throw BadDereference(); - - const Node *pValue = FindValue(key); - if(!pValue) - throw MakeTypedKeyNotFound(m_mark, key); - - return *pValue; + if(const Node *pValue = FindValue(key)) + return *pValue; + throw MakeTypedKeyNotFound(m_mark, key); } template diff --git a/include/yaml-cpp/nodeproperties.h b/include/yaml-cpp/nodeproperties.h deleted file mode 100644 index 63be8b0..0000000 --- a/include/yaml-cpp/nodeproperties.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef NODEPROPERTIES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define NODEPROPERTIES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - -namespace YAML -{ - struct NodeProperties { - std::string tag; - std::string anchor; - }; -} - -#endif // NODEPROPERTIES_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/aliascontent.cpp b/src/aliascontent.cpp deleted file mode 100644 index e59d5ad..0000000 --- a/src/aliascontent.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "aliascontent.h" - -namespace YAML -{ - AliasContent::AliasContent(Content* pNodeContent): m_pRef(pNodeContent) - { - } - - bool AliasContent::GetBegin(std::vector ::const_iterator& i) const - { - return m_pRef->GetBegin(i); - } - - bool AliasContent::GetBegin(std::map ::const_iterator& i) const - { - return m_pRef->GetBegin(i); - } - - bool AliasContent::GetEnd(std::vector ::const_iterator& i) const - { - return m_pRef->GetEnd(i); - } - - bool AliasContent::GetEnd(std::map ::const_iterator& i) const - { - return m_pRef->GetEnd(i); - } - - Node* AliasContent::GetNode(std::size_t n) const - { - return m_pRef->GetNode(n); - } - - std::size_t AliasContent::GetSize() const - { - return m_pRef->GetSize(); - } - - bool AliasContent::IsScalar() const - { - return m_pRef->IsScalar(); - } - - bool AliasContent::IsMap() const - { - return m_pRef->IsMap(); - } - - bool AliasContent::IsSequence() const - { - return m_pRef->IsSequence(); - } - - bool AliasContent::GetScalar(std::string& scalar) const - { - return m_pRef->GetScalar(scalar); - } - - void AliasContent::EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const - { - m_pRef->EmitEvents(am, eventHandler, mark, tag, anchor); - } - - int AliasContent::Compare(Content *pContent) - { - return m_pRef->Compare(pContent); - } - - int AliasContent::Compare(Scalar *pScalar) - { - return m_pRef->Compare(pScalar); - } - - int AliasContent::Compare(Sequence *pSequence) - { - return m_pRef->Compare(pSequence); - } - - int AliasContent::Compare(Map *pMap) - { - return m_pRef->Compare(pMap); - } -} diff --git a/src/aliascontent.h b/src/aliascontent.h deleted file mode 100644 index 235e88c..0000000 --- a/src/aliascontent.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef ALIASCONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define ALIASCONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - - -#include "content.h" - -namespace YAML -{ - class AliasContent : public Content - { - public: - AliasContent(Content *pNodeContent); - - virtual bool GetBegin(std::vector ::const_iterator&) const; - virtual bool GetBegin(std::map ::const_iterator&) const; - virtual bool GetEnd(std::vector ::const_iterator&) const; - virtual bool GetEnd(std::map ::const_iterator&) const; - virtual Node* GetNode(std::size_t) const; - virtual std::size_t GetSize() const; - virtual bool IsScalar() const; - virtual bool IsMap() const; - virtual bool IsSequence() const; - - virtual bool GetScalar(std::string& s) const; - virtual void EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const; - - virtual int Compare(Content *); - virtual int Compare(Scalar *); - virtual int Compare(Sequence *); - virtual int Compare(Map *); - - private: - Content* m_pRef; - }; -} - -#endif // ALIASCONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/aliasmanager.cpp b/src/aliasmanager.cpp index 89dfd03..ed4d3b5 100644 --- a/src/aliasmanager.cpp +++ b/src/aliasmanager.cpp @@ -11,33 +11,17 @@ namespace YAML void AliasManager::RegisterReference(const Node& node) { - const Node *pIdentity = node.Identity(); - m_newIdentityByOldIdentity.insert(std::make_pair(pIdentity, &node)); m_anchorByIdentity.insert(std::make_pair(&node, _CreateNewAnchor())); } - - const Node *AliasManager::LookupReference(const Node& node) const - { - const Node *pIdentity = node.Identity(); - return _LookupReference(*pIdentity); - } - + anchor_t AliasManager::LookupAnchor(const Node& node) const { AnchorByIdentity::const_iterator it = m_anchorByIdentity.find(&node); if(it == m_anchorByIdentity.end()) - assert(false); // TODO: throw - return it->second; - } - - const Node *AliasManager::_LookupReference(const Node& oldIdentity) const - { - NodeByNode::const_iterator it = m_newIdentityByOldIdentity.find(&oldIdentity); - if(it == m_newIdentityByOldIdentity.end()) return 0; return it->second; } - + anchor_t AliasManager::_CreateNewAnchor() { return ++m_curAnchor; diff --git a/src/content.cpp b/src/content.cpp deleted file mode 100644 index a1540d1..0000000 --- a/src/content.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "content.h" -#include "yaml-cpp/node.h" -#include - -namespace YAML -{ - void Content::SetData(const std::string&) - { - assert(false); // TODO: throw - } - - void Content::Append(std::auto_ptr) - { - assert(false); // TODO: throw - } - - void Content::Insert(std::auto_ptr, std::auto_ptr) - { - assert(false); // TODO: throw - } -} diff --git a/src/content.h b/src/content.h deleted file mode 100644 index f850247..0000000 --- a/src/content.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef CONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define CONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if !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/exceptions.h" -#include "ltnode.h" -#include -#include -#include - -namespace YAML -{ - struct Mark; - struct NodeProperties; - class AliasManager; - class EventHandler; - class Map; - class Node; - class Scalar; - class Scanner; - class Sequence; - - class Content - { - public: - Content() {} - virtual ~Content() {} - - virtual bool GetBegin(std::vector ::const_iterator&) const { return false; } - virtual bool GetBegin(std::map ::const_iterator&) const { return false; } - virtual bool GetEnd(std::vector ::const_iterator&) const { return false; } - virtual bool GetEnd(std::map ::const_iterator&) const { return false; } - virtual Node *GetNode(std::size_t) const { return 0; } - virtual std::size_t GetSize() const { return 0; } - virtual bool IsScalar() const { return false; } - virtual bool IsMap() const { return false; } - virtual bool IsSequence() const { return false; } - - virtual void SetData(const std::string& data); - virtual void Append(std::auto_ptr pNode); - virtual void Insert(std::auto_ptr pKey, std::auto_ptr pValue); - virtual void EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const = 0; - - // extraction - virtual bool GetScalar(std::string&) const { return false; } - - // ordering - virtual int Compare(Content *) { return 0; } - virtual int Compare(Scalar *) { return 0; } - virtual int Compare(Sequence *) { return 0; } - virtual int Compare(Map *) { return 0; } - - protected: - }; -} - -#endif // CONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/contrib/graphbuilderadapter.cpp b/src/contrib/graphbuilderadapter.cpp index 6431a45..557e97c 100644 --- a/src/contrib/graphbuilderadapter.cpp +++ b/src/contrib/graphbuilderadapter.cpp @@ -4,10 +4,10 @@ namespace YAML { int GraphBuilderAdapter::ContainerFrame::sequenceMarker; - void GraphBuilderAdapter::OnNull(const std::string& tag, anchor_t anchor) + void GraphBuilderAdapter::OnNull(const Mark& mark, anchor_t anchor) { void *pParent = GetCurrentParent(); - void *pNode = m_builder.NewNull(tag, pParent); + void *pNode = m_builder.NewNull(mark, pParent); RegisterAnchor(anchor, pNode); DispositionNode(pNode); diff --git a/src/contrib/graphbuilderadapter.h b/src/contrib/graphbuilderadapter.h index b8c1f6a..8e5cc07 100644 --- a/src/contrib/graphbuilderadapter.h +++ b/src/contrib/graphbuilderadapter.h @@ -24,7 +24,7 @@ namespace YAML virtual void OnDocumentStart(const Mark& mark) {(void)mark;} virtual void OnDocumentEnd() {} - virtual void OnNull(const std::string& tag, anchor_t anchor); + 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); diff --git a/src/emitfromevents.cpp b/src/emitfromevents.cpp index 4105a18..49fc10b 100644 --- a/src/emitfromevents.cpp +++ b/src/emitfromevents.cpp @@ -26,12 +26,11 @@ namespace YAML { } - void EmitFromEvents::OnNull(const std::string& tag, anchor_t anchor) + void EmitFromEvents::OnNull(const Mark&, anchor_t anchor) { BeginNode(); - EmitProps(tag, anchor); - if(tag.empty()) - m_emitter << Null; + EmitProps("", anchor); + m_emitter << Null; } void EmitFromEvents::OnAlias(const Mark&, anchor_t anchor) diff --git a/src/iterator.cpp b/src/iterator.cpp index 2b7742b..f4159e3 100644 --- a/src/iterator.cpp +++ b/src/iterator.cpp @@ -4,18 +4,16 @@ namespace YAML { - Iterator::Iterator(): m_pData(0) - { - m_pData = new IterPriv; - } - - Iterator::Iterator(IterPriv *pData): m_pData(pData) + Iterator::Iterator(): m_pData(new IterPriv) { } - Iterator::Iterator(const Iterator& rhs): m_pData(0) + Iterator::Iterator(std::auto_ptr pData): m_pData(pData) + { + } + + Iterator::Iterator(const Iterator& rhs): m_pData(new IterPriv(*rhs.m_pData)) { - m_pData = new IterPriv(*rhs.m_pData); } Iterator& Iterator::operator = (const Iterator& rhs) @@ -23,14 +21,12 @@ namespace YAML if(this == &rhs) return *this; - delete m_pData; - m_pData = new IterPriv(*rhs.m_pData); + m_pData.reset(new IterPriv(*rhs.m_pData)); return *this; } Iterator::~Iterator() { - delete m_pData; } Iterator& Iterator::operator ++ () diff --git a/src/iterpriv.h b/src/iterpriv.h index cade13f..8e2ab22 100644 --- a/src/iterpriv.h +++ b/src/iterpriv.h @@ -6,7 +6,7 @@ #endif -#include "ltnode.h" +#include "yaml-cpp/ltnode.h" #include #include diff --git a/src/map.cpp b/src/map.cpp deleted file mode 100644 index 902b256..0000000 --- a/src/map.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "map.h" -#include "yaml-cpp/node.h" -#include "yaml-cpp/eventhandler.h" -#include "yaml-cpp/exceptions.h" - -namespace YAML -{ - Map::Map() - { - } - - Map::~Map() - { - Clear(); - } - - void Map::Clear() - { - for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) { - delete it->first; - delete it->second; - } - m_data.clear(); - } - - bool Map::GetBegin(std::map ::const_iterator& it) const - { - it = m_data.begin(); - return true; - } - - bool Map::GetEnd(std::map ::const_iterator& it) const - { - it = m_data.end(); - return true; - } - - std::size_t Map::GetSize() const - { - return m_data.size(); - } - - void Map::Insert(std::auto_ptr pKey, std::auto_ptr pValue) - { - node_map::const_iterator it = m_data.find(pKey.get()); - if(it != m_data.end()) - return; - - m_data[pKey.release()] = pValue.release(); - } - - void Map::EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const - { - eventHandler.OnMapStart(mark, tag, anchor); - for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) { - it->first->EmitEvents(am, eventHandler); - it->second->EmitEvents(am, eventHandler); - } - eventHandler.OnMapEnd(); - } - - int Map::Compare(Content *pContent) - { - return -pContent->Compare(this); - } - - int Map::Compare(Map *pMap) - { - node_map::const_iterator it = m_data.begin(), jt = pMap->m_data.begin(); - while(1) { - if(it == m_data.end()) { - if(jt == pMap->m_data.end()) - return 0; - else - return -1; - } - if(jt == pMap->m_data.end()) - return 1; - - int cmp = it->first->Compare(*jt->first); - if(cmp != 0) - return cmp; - - cmp = it->second->Compare(*jt->second); - if(cmp != 0) - return cmp; - } - - return 0; - } -} diff --git a/src/map.h b/src/map.h deleted file mode 100644 index ac2531b..0000000 --- a/src/map.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef MAP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define MAP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - - -#include "content.h" -#include -#include - -namespace YAML -{ - class Node; - - class Map: public Content - { - private: - typedef std::map node_map; - - public: - Map(); - virtual ~Map(); - - void Clear(); - - virtual bool GetBegin(std::map ::const_iterator& it) const; - virtual bool GetEnd(std::map ::const_iterator& it) const; - virtual std::size_t GetSize() const; - - virtual void Insert(std::auto_ptr pKey, std::auto_ptr pValue); - virtual void EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const; - - virtual bool IsMap() const { return true; } - - // ordering - virtual int Compare(Content *pContent); - virtual int Compare(Scalar *) { return 1; } - virtual int Compare(Sequence *) { return 1; } - virtual int Compare(Map *pMap); - - private: - node_map m_data; - }; -} - -#endif // MAP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/node.cpp b/src/node.cpp index a17eefa..c9a8000 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -1,33 +1,29 @@ #include "yaml-cpp/node.h" -#include "aliascontent.h" +#include "iterpriv.h" +#include "nodebuilder.h" +#include "nodeownership.h" +#include "scanner.h" +#include "tag.h" +#include "token.h" #include "yaml-cpp/aliasmanager.h" -#include "content.h" #include "yaml-cpp/emitfromevents.h" #include "yaml-cpp/emitter.h" #include "yaml-cpp/eventhandler.h" -#include "iterpriv.h" -#include "map.h" -#include "nodebuilder.h" -#include "yaml-cpp/nodeproperties.h" -#include "scalar.h" -#include "scanner.h" -#include "sequence.h" -#include "tag.h" -#include "token.h" #include #include namespace YAML { - // the ordering! - bool ltnode::operator ()(const Node *pNode1, const Node *pNode2) const - { + bool ltnode::operator()(const Node *pNode1, const Node *pNode2) const { return *pNode1 < *pNode2; } - Node::Node(): m_type(CT_NONE), m_pContent(0), m_alias(false), m_referenced(false) + Node::Node(): m_pOwnership(new NodeOwnership), m_type(NodeType::Null) + { + } + + Node::Node(NodeOwnership& owner): m_pOwnership(new NodeOwnership(&owner)), m_type(NodeType::Null) { - m_pIdentity = this; } Node::~Node() @@ -37,14 +33,24 @@ namespace YAML void Node::Clear() { - delete m_pContent; - m_type = CT_NONE; - m_pContent = 0; - m_alias = false; - m_referenced = false; + m_pOwnership.reset(new NodeOwnership); + m_type = NodeType::Null; m_tag.clear(); + m_scalarData.clear(); + m_seqData.clear(); + m_mapData.clear(); } + bool Node::IsAliased() const + { + return m_pOwnership->IsAliased(*this); + } + + Node& Node::CreateNode() + { + return m_pOwnership->Create(); + } + std::auto_ptr Node::Clone() const { std::auto_ptr pNode(new Node); @@ -64,9 +70,10 @@ namespace YAML void Node::EmitEvents(AliasManager& am, EventHandler& eventHandler) const { anchor_t anchor = NullAnchor; - if(m_referenced || m_alias) { - if(const Node *pOther = am.LookupReference(*this)) { - eventHandler.OnAlias(m_mark, am.LookupAnchor(*pOther)); + if(IsAliased()) { + anchor = am.LookupAnchor(*this); + if(anchor) { + eventHandler.OnAlias(m_mark, anchor); return; } @@ -74,93 +81,76 @@ namespace YAML anchor = am.LookupAnchor(*this); } - if(m_pContent) - m_pContent->EmitEvents(am, eventHandler, m_mark, GetTag(), anchor); - else - eventHandler.OnNull(GetTag(), anchor); + switch(m_type) { + case NodeType::Null: + eventHandler.OnNull(m_mark, anchor); + break; + case NodeType::Scalar: + eventHandler.OnScalar(m_mark, m_tag, anchor, m_scalarData); + break; + case NodeType::Sequence: + eventHandler.OnSequenceStart(m_mark, m_tag, anchor); + for(std::size_t i=0;iEmitEvents(am, eventHandler); + eventHandler.OnSequenceEnd(); + break; + case NodeType::Map: + eventHandler.OnMapStart(m_mark, m_tag, anchor); + for(node_map::const_iterator it=m_mapData.begin();it!=m_mapData.end();++it) { + it->first->EmitEvents(am, eventHandler); + it->second->EmitEvents(am, eventHandler); + } + eventHandler.OnMapEnd(); + break; + } } - void Node::Init(CONTENT_TYPE type, const Mark& mark, const std::string& tag) + void Node::Init(NodeType::value type, const Mark& mark, const std::string& tag) { Clear(); m_mark = mark; m_type = type; m_tag = tag; - m_alias = false; - m_pIdentity = this; - m_referenced = false; - - switch(type) { - case CT_SCALAR: - m_pContent = new Scalar; - break; - case CT_SEQUENCE: - m_pContent = new Sequence; - break; - case CT_MAP: - m_pContent = new Map; - break; - default: - m_pContent = 0; - break; - } } - void Node::InitNull(const std::string& tag) + void Node::MarkAsAliased() { - Clear(); - m_tag = tag; - m_alias = false; - m_pIdentity = this; - m_referenced = false; - } - - void Node::InitAlias(const Mark& mark, const Node& identity) - { - Clear(); - m_mark = mark; - m_alias = true; - m_pIdentity = &identity; - if(identity.m_pContent) { - m_pContent = new AliasContent(identity.m_pContent); - m_type = identity.GetType(); - } - identity.m_referenced = true; - } - - void Node::SetData(const std::string& data) - { - assert(m_pContent); // TODO: throw - m_pContent->SetData(data); - } - - void Node::Append(std::auto_ptr pNode) - { - assert(m_pContent); // TODO: throw - m_pContent->Append(pNode); + m_pOwnership->MarkAsAliased(*this); } - void Node::Insert(std::auto_ptr pKey, std::auto_ptr pValue) + void Node::SetScalarData(const std::string& data) { - assert(m_pContent); // TODO: throw - m_pContent->Insert(pKey, pValue); + assert(m_type == NodeType::Scalar); // TODO: throw? + m_scalarData = data; + } + + void Node::Append(Node& node) + { + assert(m_type == NodeType::Sequence); // TODO: throw? + m_seqData.push_back(&node); + } + + void Node::Insert(Node& key, Node& value) + { + assert(m_type == NodeType::Map); // TODO: throw? + m_mapData[&key] = &value; } // begin // Returns an iterator to the beginning of this (sequence or map). Iterator Node::begin() const { - if(!m_pContent) - return Iterator(); - - std::vector ::const_iterator seqIter; - if(m_pContent->GetBegin(seqIter)) - return Iterator(new IterPriv(seqIter)); - - std::map ::const_iterator mapIter; - if(m_pContent->GetBegin(mapIter)) - return Iterator(new IterPriv(mapIter)); - + switch(m_type) { + case NodeType::Null: + case NodeType::Scalar: + return Iterator(); + case NodeType::Sequence: + return Iterator(std::auto_ptr(new IterPriv(m_seqData.begin()))); + case NodeType::Map: + return Iterator(std::auto_ptr(new IterPriv(m_mapData.begin()))); + } + + assert(false); return Iterator(); } @@ -168,50 +158,62 @@ namespace YAML // . Returns an iterator to the end of this (sequence or map). Iterator Node::end() const { - if(!m_pContent) - return Iterator(); - - std::vector ::const_iterator seqIter; - if(m_pContent->GetEnd(seqIter)) - return Iterator(new IterPriv(seqIter)); - - std::map ::const_iterator mapIter; - if(m_pContent->GetEnd(mapIter)) - return Iterator(new IterPriv(mapIter)); - + switch(m_type) { + case NodeType::Null: + case NodeType::Scalar: + return Iterator(); + case NodeType::Sequence: + return Iterator(std::auto_ptr(new IterPriv(m_seqData.end()))); + case NodeType::Map: + return Iterator(std::auto_ptr(new IterPriv(m_mapData.end()))); + } + + assert(false); return Iterator(); } // size - // . Returns the size of this node, if it's a sequence node. + // . Returns the size of a sequence or map node // . Otherwise, returns zero. std::size_t Node::size() const { - if(!m_pContent) - return 0; - - return m_pContent->GetSize(); + switch(m_type) { + case NodeType::Null: + case NodeType::Scalar: + return 0; + case NodeType::Sequence: + return m_seqData.size(); + case NodeType::Map: + return m_mapData.size(); + } + + assert(false); + return 0; } const Node *Node::FindAtIndex(std::size_t i) const { - if(!m_pContent) - return 0; - - return m_pContent->GetNode(i); + if(m_type == NodeType::Sequence) + return m_seqData[i]; + return 0; } bool Node::GetScalar(std::string& s) const { - if(!m_pContent) { - if(m_tag.empty()) + switch(m_type) { + case NodeType::Null: s = "~"; - else - s = ""; - return true; + return true; + case NodeType::Scalar: + s = m_scalarData; + return true; + case NodeType::Sequence: + case NodeType::Map: + return false; } - return m_pContent->GetScalar(s); + assert(false); + return false; } Emitter& operator << (Emitter& out, const Node& node) @@ -223,17 +225,41 @@ namespace YAML int Node::Compare(const Node& rhs) const { - // Step 1: no content is the smallest - if(!m_pContent) { - if(rhs.m_pContent) - return -1; - else + if(m_type != rhs.m_type) + return rhs.m_type - m_type; + + switch(m_type) { + case NodeType::Null: + return 0; + case NodeType::Scalar: + return m_scalarData.compare(rhs.m_scalarData); + case NodeType::Sequence: + if(m_seqData.size() < rhs.m_seqData.size()) + return 1; + else if(m_seqData.size() > rhs.m_seqData.size()) + return -1; + for(std::size_t i=0;iCompare(*rhs.m_seqData[i])) + return cmp; + return 0; + case NodeType::Map: + if(m_mapData.size() < rhs.m_mapData.size()) + return 1; + else if(m_mapData.size() > rhs.m_mapData.size()) + return -1; + node_map::const_iterator it = m_mapData.begin(); + node_map::const_iterator jt = rhs.m_mapData.begin(); + for(;it!=m_mapData.end() && jt!=rhs.m_mapData.end();it++, jt++) { + if(int cmp = it->first->Compare(*jt->first)) + return cmp; + if(int cmp = it->second->Compare(*jt->second)) + return cmp; + } return 0; } - if(!rhs.m_pContent) - return 1; - - return m_pContent->Compare(rhs.m_pContent); + + assert(false); + return 0; } bool operator < (const Node& n1, const Node& n2) diff --git a/src/nodebuilder.cpp b/src/nodebuilder.cpp index 447ff87..13a7032 100644 --- a/src/nodebuilder.cpp +++ b/src/nodebuilder.cpp @@ -1,7 +1,6 @@ #include "nodebuilder.h" #include "yaml-cpp/mark.h" #include "yaml-cpp/node.h" -#include "yaml-cpp/nodeproperties.h" #include namespace YAML @@ -25,32 +24,32 @@ namespace YAML assert(m_finished); } - void NodeBuilder::OnNull(const std::string& tag, anchor_t anchor) + void NodeBuilder::OnNull(const Mark& mark, anchor_t anchor) { Node& node = Push(anchor); - node.InitNull(tag); + node.Init(NodeType::Null, mark, ""); Pop(); } - void NodeBuilder::OnAlias(const Mark& mark, anchor_t anchor) + void NodeBuilder::OnAlias(const Mark& /*mark*/, anchor_t anchor) { - Node& node = Push(); - node.InitAlias(mark, *m_anchors[anchor]); - Pop(); + Node& node = *m_anchors[anchor]; + Insert(node); + node.MarkAsAliased(); } void NodeBuilder::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value) { Node& node = Push(anchor); - node.Init(CT_SCALAR, mark, tag); - node.SetData(value); + node.Init(NodeType::Scalar, mark, tag); + node.SetScalarData(value); Pop(); } void NodeBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor) { Node& node = Push(anchor); - node.Init(CT_SEQUENCE, mark, tag); + node.Init(NodeType::Sequence, mark, tag); } void NodeBuilder::OnSequenceEnd() @@ -61,7 +60,7 @@ namespace YAML void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor) { Node& node = Push(anchor); - node.Init(CT_MAP, mark, tag); + node.Init(NodeType::Map, mark, tag); m_didPushKey.push(false); } @@ -85,14 +84,14 @@ namespace YAML return m_root; } - std::auto_ptr pNode(new Node); - m_stack.push(pNode); - return m_stack.top(); + Node& node = m_root.CreateNode(); + m_stack.push(&node); + return node; } Node& NodeBuilder::Top() { - return m_stack.empty() ? m_root : m_stack.top(); + return m_stack.empty() ? m_root : *m_stack.top(); } void NodeBuilder::Pop() @@ -103,36 +102,40 @@ namespace YAML return; } - std::auto_ptr pNode = m_stack.pop(); - Insert(pNode); + Node& node = *m_stack.top(); + m_stack.pop(); + Insert(node); } - void NodeBuilder::Insert(std::auto_ptr pNode) + void NodeBuilder::Insert(Node& node) { - Node& node = Top(); - switch(node.GetType()) { - case CT_SEQUENCE: - node.Append(pNode); + Node& curTop = Top(); + switch(curTop.Type()) { + case NodeType::Null: + case NodeType::Scalar: + assert(false); break; - case CT_MAP: + case NodeType::Sequence: + curTop.Append(node); + break; + case NodeType::Map: assert(!m_didPushKey.empty()); if(m_didPushKey.top()) { assert(!m_pendingKeys.empty()); - std::auto_ptr pKey = m_pendingKeys.pop(); - node.Insert(pKey, pNode); + Node& key = *m_pendingKeys.top(); + m_pendingKeys.pop(); + curTop.Insert(key, node); m_didPushKey.top() = false; } else { - m_pendingKeys.push(pNode); + m_pendingKeys.push(&node); m_didPushKey.top() = true; } break; - default: - assert(false); } } - void NodeBuilder::RegisterAnchor(anchor_t anchor, const Node& node) + void NodeBuilder::RegisterAnchor(anchor_t anchor, Node& node) { if(anchor) { assert(anchor == m_anchors.size()); diff --git a/src/nodebuilder.h b/src/nodebuilder.h index 1ecf2e3..eba403a 100644 --- a/src/nodebuilder.h +++ b/src/nodebuilder.h @@ -6,10 +6,10 @@ #endif #include "yaml-cpp/eventhandler.h" -#include "ptr_stack.h" #include #include #include +#include namespace YAML { @@ -24,7 +24,7 @@ namespace YAML virtual void OnDocumentStart(const Mark& mark); virtual void OnDocumentEnd(); - virtual void OnNull(const std::string& tag, anchor_t anchor); + 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); @@ -40,19 +40,19 @@ namespace YAML Node& Top(); void Pop(); - void Insert(std::auto_ptr pNode); - void RegisterAnchor(anchor_t anchor, const Node& node); + void Insert(Node& node); + void RegisterAnchor(anchor_t anchor, Node& node); private: Node& m_root; bool m_initializedRoot; bool m_finished; - ptr_stack m_stack; - ptr_stack m_pendingKeys; + std::stack m_stack; + std::stack m_pendingKeys; std::stack m_didPushKey; - typedef std::vector Anchors; + typedef std::vector Anchors; Anchors m_anchors; }; } diff --git a/src/nodeownership.cpp b/src/nodeownership.cpp new file mode 100644 index 0000000..118edbc --- /dev/null +++ b/src/nodeownership.cpp @@ -0,0 +1,31 @@ +#include "nodeownership.h" +#include "yaml-cpp/node.h" + +namespace YAML +{ + NodeOwnership::NodeOwnership(NodeOwnership *pOwner): m_pOwner(pOwner) + { + if(!m_pOwner) + m_pOwner = this; + } + + NodeOwnership::~NodeOwnership() + { + } + + Node& NodeOwnership::_Create() + { + m_nodes.push_back(std::auto_ptr(new Node)); + return m_nodes.back(); + } + + void NodeOwnership::_MarkAsAliased(const Node& node) + { + m_aliasedNodes.insert(&node); + } + + bool NodeOwnership::_IsAliased(const Node& node) const + { + return m_aliasedNodes.count(&node) > 0; + } +} diff --git a/src/nodeownership.h b/src/nodeownership.h new file mode 100644 index 0000000..aae9b99 --- /dev/null +++ b/src/nodeownership.h @@ -0,0 +1,39 @@ +#ifndef NODE_OWNERSHIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define NODE_OWNERSHIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + + +#include "yaml-cpp/noncopyable.h" +#include "ptr_vector.h" +#include + +namespace YAML +{ + class Node; + + class NodeOwnership: private noncopyable + { + public: + explicit NodeOwnership(NodeOwnership *pOwner = 0); + ~NodeOwnership(); + + Node& Create() { return m_pOwner->_Create(); } + void MarkAsAliased(const Node& node) { m_pOwner->_MarkAsAliased(node); } + bool IsAliased(const Node& node) const { return m_pOwner->_IsAliased(node); } + + private: + Node& _Create(); + void _MarkAsAliased(const Node& node); + bool _IsAliased(const Node& node) const; + + private: + ptr_vector m_nodes; + std::set m_aliasedNodes; + NodeOwnership *m_pOwner; + }; +} + +#endif // NODE_OWNERSHIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/ptr_vector.h b/src/ptr_vector.h new file mode 100644 index 0000000..e798208 --- /dev/null +++ b/src/ptr_vector.h @@ -0,0 +1,45 @@ +#ifndef PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include "yaml-cpp/noncopyable.h" +#include +#include + +namespace YAML { + + template + class ptr_vector: private YAML::noncopyable + { + public: + ptr_vector() {} + ~ptr_vector() { clear(); } + + void clear() { + for(unsigned i=0;i t) { + m_data.push_back(NULL); + m_data.back() = t.release(); + } + T& operator[](std::size_t i) { return *m_data[i]; } + const T& operator[](std::size_t i) const { return *m_data[i]; } + + T& back() { return *m_data.back(); } + const T& back() const { return *m_data.back(); } + + private: + std::vector m_data; + }; +} + +#endif // PTR_VECTOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/scalar.cpp b/src/scalar.cpp deleted file mode 100644 index 8bba6f7..0000000 --- a/src/scalar.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "scalar.h" -#include "yaml-cpp/eventhandler.h" - -namespace YAML -{ - Scalar::Scalar() - { - } - - Scalar::~Scalar() - { - } - - void Scalar::EmitEvents(AliasManager&, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const - { - eventHandler.OnScalar(mark, tag, anchor, m_data); - } - - int Scalar::Compare(Content *pContent) - { - return -pContent->Compare(this); - } - - int Scalar::Compare(Scalar *pScalar) - { - if(m_data < pScalar->m_data) - return -1; - else if(m_data > pScalar->m_data) - return 1; - else - return 0; - } -} - diff --git a/src/scalar.h b/src/scalar.h deleted file mode 100644 index 0cca420..0000000 --- a/src/scalar.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef SCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define SCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - - -#include "content.h" -#include - -namespace YAML -{ - class Scalar: public Content - { - public: - Scalar(); - virtual ~Scalar(); - - virtual void SetData(const std::string& data) { m_data = data; } - - virtual bool IsScalar() const { return true; } - virtual void EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const; - - // extraction - virtual bool GetScalar(std::string& scalar) const { - scalar = m_data; - return true; - } - - // ordering - virtual int Compare(Content *pContent); - virtual int Compare(Scalar *pScalar); - virtual int Compare(Sequence *) { return -1; } - virtual int Compare(Map *) { return -1; } - - protected: - std::string m_data; - }; -} - -#endif // SCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - diff --git a/src/sequence.cpp b/src/sequence.cpp deleted file mode 100644 index 41e3710..0000000 --- a/src/sequence.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "sequence.h" -#include "yaml-cpp/eventhandler.h" -#include "yaml-cpp/node.h" -#include - -namespace YAML -{ - Sequence::Sequence() - { - - } - - Sequence::~Sequence() - { - Clear(); - } - - void Sequence::Clear() - { - for(std::size_t i=0;i::const_iterator& it) const - { - it = m_data.begin(); - return true; - } - - bool Sequence::GetEnd(std::vector ::const_iterator& it) const - { - it = m_data.end(); - return true; - } - - Node *Sequence::GetNode(std::size_t i) const - { - if(i < m_data.size()) - return m_data[i]; - return 0; - } - - std::size_t Sequence::GetSize() const - { - return m_data.size(); - } - - void Sequence::Append(std::auto_ptr pNode) - { - m_data.push_back(pNode.release()); - } - - void Sequence::EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const - { - eventHandler.OnSequenceStart(mark, tag, anchor); - for(std::size_t i=0;iEmitEvents(am, eventHandler); - eventHandler.OnSequenceEnd(); - } - - int Sequence::Compare(Content *pContent) - { - return -pContent->Compare(this); - } - - int Sequence::Compare(Sequence *pSeq) - { - std::size_t n = m_data.size(), m = pSeq->m_data.size(); - if(n < m) - return -1; - else if(n > m) - return 1; - - for(std::size_t i=0;iCompare(*pSeq->m_data[i]); - if(cmp != 0) - return cmp; - } - - return 0; - } -} diff --git a/src/sequence.h b/src/sequence.h deleted file mode 100644 index 2078d19..0000000 --- a/src/sequence.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef SEQUENCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 -#define SEQUENCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 - -#if !defined(__GNUC__) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4) // GCC supports "pragma once" correctly since 3.4 -#pragma once -#endif - - -#include "content.h" -#include - -namespace YAML -{ - class Node; - - class Sequence: public Content - { - public: - Sequence(); - virtual ~Sequence(); - - void Clear(); - - virtual bool GetBegin(std::vector ::const_iterator& it) const; - virtual bool GetEnd(std::vector ::const_iterator& it) const; - virtual Node *GetNode(std::size_t i) const; - virtual std::size_t GetSize() const; - - virtual void Append(std::auto_ptr pNode); - virtual void EmitEvents(AliasManager& am, EventHandler& eventHandler, const Mark& mark, const std::string& tag, anchor_t anchor) const; - - virtual bool IsSequence() const { return true; } - - // ordering - virtual int Compare(Content *pContent); - virtual int Compare(Scalar *) { return 1; } - virtual int Compare(Sequence *pSeq); - virtual int Compare(Map *) { return -1; } - - protected: - std::vector m_data; - }; -} - -#endif // SEQUENCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/singledocparser.cpp b/src/singledocparser.cpp index c67a0ff..47759c3 100644 --- a/src/singledocparser.cpp +++ b/src/singledocparser.cpp @@ -48,7 +48,7 @@ namespace YAML { // an empty node *is* a possibility if(m_scanner.empty()) { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(Mark::null(), NullAnchor); return; } @@ -112,7 +112,10 @@ namespace YAML break; } - eventHandler.OnNull(tag, anchor); + if(tag == "?") + eventHandler.OnNull(mark, anchor); + else + eventHandler.OnScalar(mark, tag, anchor, ""); } void SingleDocParser::HandleSequence(EventHandler& eventHandler) @@ -147,7 +150,7 @@ namespace YAML if(!m_scanner.empty()) { const Token& token = m_scanner.peek(); if(token.type == Token::BLOCK_ENTRY || token.type == Token::BLOCK_SEQ_END) { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(token.mark, NullAnchor); continue; } } @@ -224,7 +227,7 @@ namespace YAML m_scanner.pop(); HandleNode(eventHandler); } else { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(token.mark, NullAnchor); } // now grab value (optional) @@ -232,7 +235,7 @@ namespace YAML m_scanner.pop(); HandleNode(eventHandler); } else { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(token.mark, NullAnchor); } } @@ -261,7 +264,7 @@ namespace YAML m_scanner.pop(); HandleNode(eventHandler); } else { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(token.mark, NullAnchor); } // now grab value (optional) @@ -269,7 +272,7 @@ namespace YAML m_scanner.pop(); HandleNode(eventHandler); } else { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(token.mark, NullAnchor); } // now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node) @@ -289,6 +292,7 @@ namespace YAML m_pCollectionStack->PushCollectionType(CollectionType::CompactMap); // grab key + Mark mark = m_scanner.peek().mark; m_scanner.pop(); HandleNode(eventHandler); @@ -297,7 +301,7 @@ namespace YAML m_scanner.pop(); HandleNode(eventHandler); } else { - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(mark, NullAnchor); } m_pCollectionStack->PopCollectionType(CollectionType::CompactMap); @@ -309,7 +313,7 @@ namespace YAML m_pCollectionStack->PushCollectionType(CollectionType::CompactMap); // null key - eventHandler.OnNull("", NullAnchor); + eventHandler.OnNull(m_scanner.peek().mark, NullAnchor); // grab value m_scanner.pop(); diff --git a/test/parsertests.cpp b/test/parsertests.cpp index 9393208..74e0801 100644 --- a/test/parsertests.cpp +++ b/test/parsertests.cpp @@ -698,7 +698,7 @@ namespace Test YAML::Node doc; parser.GetNextDocument(doc); - if(doc["a"] != 1) + if(doc["a"] != 4) return false; if(doc["b"] != 2) return false; @@ -729,10 +729,10 @@ namespace Test bool ExpectedTagValue(YAML::Node& node, const char* tag) { - if(node.GetTag() == tag) + if(node.Tag() == tag) return true; - throw TagMismatch(node.GetTag(), tag); + throw TagMismatch(node.Tag(), tag); } bool DefaultPlainScalarTag() diff --git a/test/spectests.cpp b/test/spectests.cpp index 106de80..719dea2 100644 --- a/test/spectests.cpp +++ b/test/spectests.cpp @@ -481,16 +481,16 @@ namespace Test { PARSE(doc, input); YAML_ASSERT(doc.size() == 3); - YAML_ASSERT(doc["not-date"].GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(doc["not-date"].Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(doc["not-date"] == "2002-04-28"); - YAML_ASSERT(doc["picture"].GetTag() == "tag:yaml.org,2002:binary"); + YAML_ASSERT(doc["picture"].Tag() == "tag:yaml.org,2002:binary"); YAML_ASSERT(doc["picture"] == "R0lGODlhDAAMAIQAAP//9/X\n" "17unp5WZmZgAAAOfn515eXv\n" "Pz7Y6OjuDg4J+fn5OTk6enp\n" "56enmleECcgggoBADs=\n" ); - YAML_ASSERT(doc["application specific tag"].GetTag() == "!something"); + YAML_ASSERT(doc["application specific tag"].Tag() == "!something"); YAML_ASSERT(doc["application specific tag"] == "The semantics of the tag\n" "above may be different for\n" @@ -519,15 +519,15 @@ namespace Test { " text: Pretty vector drawing."; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:clarkevans.com,2002:shape"); + YAML_ASSERT(doc.Tag() == "tag:clarkevans.com,2002:shape"); YAML_ASSERT(doc.size() == 3); - YAML_ASSERT(doc[0].GetTag() == "tag:clarkevans.com,2002:circle"); + YAML_ASSERT(doc[0].Tag() == "tag:clarkevans.com,2002:circle"); YAML_ASSERT(doc[0].size() == 2); YAML_ASSERT(doc[0]["center"].size() == 2); YAML_ASSERT(doc[0]["center"]["x"] == 73); YAML_ASSERT(doc[0]["center"]["y"] == 129); YAML_ASSERT(doc[0]["radius"] == 7); - YAML_ASSERT(doc[1].GetTag() == "tag:clarkevans.com,2002:line"); + YAML_ASSERT(doc[1].Tag() == "tag:clarkevans.com,2002:line"); YAML_ASSERT(doc[1].size() == 2); YAML_ASSERT(doc[1]["start"].size() == 2); YAML_ASSERT(doc[1]["start"]["x"] == 73); @@ -535,7 +535,7 @@ namespace Test { YAML_ASSERT(doc[1]["finish"].size() == 2); YAML_ASSERT(doc[1]["finish"]["x"] == 89); YAML_ASSERT(doc[1]["finish"]["y"] == 102); - YAML_ASSERT(doc[2].GetTag() == "tag:clarkevans.com,2002:label"); + YAML_ASSERT(doc[2].Tag() == "tag:clarkevans.com,2002:label"); YAML_ASSERT(doc[2].size() == 3); YAML_ASSERT(doc[2]["start"].size() == 2); YAML_ASSERT(doc[2]["start"]["x"] == 73); @@ -558,7 +558,7 @@ namespace Test { "? Ken Griffey"; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:yaml.org,2002:set"); + YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:set"); YAML_ASSERT(doc.size() == 3); YAML_ASSERT(IsNull(doc["Mark McGwire"])); YAML_ASSERT(IsNull(doc["Sammy Sosa"])); @@ -579,7 +579,7 @@ namespace Test { "- Ken Griffey: 58"; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:yaml.org,2002:omap"); + YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:omap"); YAML_ASSERT(doc.size() == 3); YAML_ASSERT(doc[0].size() == 1); YAML_ASSERT(doc[0]["Mark McGwire"] == 65); @@ -625,7 +625,7 @@ namespace Test { " Billsmer @ 338-4338."; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:clarkevans.com,2002:invoice"); + YAML_ASSERT(doc.Tag() == "tag:clarkevans.com,2002:invoice"); YAML_ASSERT(doc.size() == 8); YAML_ASSERT(doc["invoice"] == 34843); YAML_ASSERT(doc["date"] == "2001-01-23"); @@ -1185,7 +1185,7 @@ namespace Test { "!yaml!str \"foo\""; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(doc == "foo"); return true; } @@ -1223,11 +1223,11 @@ namespace Test { "!foo \"bar\""; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "!foo"); + YAML_ASSERT(doc.Tag() == "!foo"); YAML_ASSERT(doc == "bar"); PARSE_NEXT(doc); - YAML_ASSERT(doc.GetTag() == "tag:example.com,2000:app/foo"); + YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/foo"); YAML_ASSERT(doc == "bar"); return true; } @@ -1241,7 +1241,7 @@ namespace Test { "!!int 1 - 3 # Interval, not integer"; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:example.com,2000:app/int"); + YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/int"); YAML_ASSERT(doc == "1 - 3"); return true; } @@ -1255,7 +1255,7 @@ namespace Test { "!e!foo \"bar\""; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "tag:example.com,2000:app/foo"); + YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/foo"); YAML_ASSERT(doc == "bar"); return true; } @@ -1273,11 +1273,11 @@ namespace Test { "!m!light green"; PARSE(doc, input); - YAML_ASSERT(doc.GetTag() == "!my-light"); + YAML_ASSERT(doc.Tag() == "!my-light"); YAML_ASSERT(doc == "fluorescent"); PARSE_NEXT(doc); - YAML_ASSERT(doc.GetTag() == "!my-light"); + YAML_ASSERT(doc.Tag() == "!my-light"); YAML_ASSERT(doc == "green"); return true; } @@ -1292,7 +1292,7 @@ namespace Test { PARSE(doc, input); YAML_ASSERT(doc.size() == 1); - YAML_ASSERT(doc[0].GetTag() == "tag:example.com,2000:app/foo"); + YAML_ASSERT(doc[0].Tag() == "tag:example.com,2000:app/foo"); YAML_ASSERT(doc[0] == "bar"); return true; } @@ -1309,8 +1309,8 @@ namespace Test { YAML_ASSERT(doc.size() == 2); for(YAML::Iterator it=doc.begin();it!=doc.end();++it) { if(it.first() == "foo") { - YAML_ASSERT(it.first().GetTag() == "tag:yaml.org,2002:str"); - YAML_ASSERT(it.second().GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(it.second().Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(it.second() == "bar"); } else if(it.first() == "baz") { YAML_ASSERT(it.second() == "foo"); @@ -1331,9 +1331,9 @@ namespace Test { PARSE(doc, input); YAML_ASSERT(doc.size() == 1); for(YAML::Iterator it=doc.begin();it!=doc.end();++it) { - YAML_ASSERT(it.first().GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(it.first() == "foo"); - YAML_ASSERT(it.second().GetTag() == "!bar"); + YAML_ASSERT(it.second().Tag() == "!bar"); YAML_ASSERT(it.second() == "baz"); } return true; @@ -1362,11 +1362,11 @@ namespace Test { PARSE(doc, input); YAML_ASSERT(doc.size() == 3); - YAML_ASSERT(doc[0].GetTag() == "!local"); + YAML_ASSERT(doc[0].Tag() == "!local"); YAML_ASSERT(doc[0] == "foo"); - YAML_ASSERT(doc[1].GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(doc[1].Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(doc[1] == "bar"); - YAML_ASSERT(doc[2].GetTag() == "tag:example.com,2000:app/tag%21"); + YAML_ASSERT(doc[2].Tag() == "tag:example.com,2000:app/tag%21"); YAML_ASSERT(doc[2] == "baz"); return true; } @@ -1462,10 +1462,10 @@ namespace Test { YAML_ASSERT(doc.size() == 2); for(YAML::Iterator it=doc.begin();it!=doc.end();++it) { if(it.first() == "foo") { - YAML_ASSERT(it.second().GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(it.second().Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(it.second() == ""); } else if(it.first() == "") { - YAML_ASSERT(it.first().GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(it.second() == "bar"); } else return " unexpected key"; @@ -1862,12 +1862,12 @@ namespace Test { PARSE(doc, input); YAML_ASSERT(doc.size() == 5); - YAML_ASSERT(doc[0].GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(doc[0].Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(doc[0] == "a"); YAML_ASSERT(doc[1] == 'b'); YAML_ASSERT(doc[2] == "c"); YAML_ASSERT(doc[3] == "c"); - YAML_ASSERT(doc[4].GetTag() == "tag:yaml.org,2002:str"); + YAML_ASSERT(doc[4].Tag() == "tag:yaml.org,2002:str"); YAML_ASSERT(doc[4] == ""); return true; } diff --git a/util/parse.cpp b/util/parse.cpp index f8cdb52..8f194f0 100644 --- a/util/parse.cpp +++ b/util/parse.cpp @@ -23,7 +23,7 @@ public: virtual void OnDocumentStart(const YAML::Mark&) {} virtual void OnDocumentEnd() {} - virtual void OnNull(const std::string&, YAML::anchor_t) {} + virtual void OnNull(const YAML::Mark&, YAML::anchor_t) {} virtual void OnAlias(const YAML::Mark&, YAML::anchor_t) {} virtual void OnScalar(const YAML::Mark&, const std::string&, YAML::anchor_t, const std::string&) {}