From 6f7ead5171aaa576b1dedbcf79edff6952542278 Mon Sep 17 00:00:00 2001 From: Maxim Okhotskiy Date: Tue, 7 Apr 2020 19:46:34 +0300 Subject: [PATCH] Maintain order of nodes in sequences (#668) --- include/yaml-cpp/node/detail/node.h | 11 ++++++++++- src/node_data.cpp | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/yaml-cpp/node/detail/node.h b/include/yaml-cpp/node/detail/node.h index 0be7ebb..8b41466 100644 --- a/include/yaml-cpp/node/detail/node.h +++ b/include/yaml-cpp/node/detail/node.h @@ -13,10 +13,16 @@ #include "yaml-cpp/node/ptr.h" #include "yaml-cpp/node/type.h" #include +#include namespace YAML { namespace detail { class node { + private: + struct less { + bool operator ()(const node* l, const node* r) {return l->m_index < r->m_index;} + }; + public: node() : m_pRef(new node_ref), m_dependencies{} {} node(const node&) = delete; @@ -108,6 +114,7 @@ class node { void push_back(node& input, shared_memory_holder pMemory) { m_pRef->push_back(input, pMemory); input.add_dependency(*this); + m_index = m_amount.fetch_add(1); } void insert(node& key, node& value, shared_memory_holder pMemory) { m_pRef->insert(key, value, pMemory); @@ -159,8 +166,10 @@ class node { private: shared_node_ref m_pRef; - using nodes = std::set; + using nodes = std::set; nodes m_dependencies; + size_t m_index; + static std::atomic m_amount; }; } // namespace detail } // namespace YAML diff --git a/src/node_data.cpp b/src/node_data.cpp index e91d12a..cc45f21 100644 --- a/src/node_data.cpp +++ b/src/node_data.cpp @@ -12,6 +12,7 @@ namespace YAML { namespace detail { +std::atomic node::m_amount{0}; const std::string& node_data::empty_scalar() { static const std::string svalue;