Compare commits

...
Author SHA1 Message Date
Oliver Hamlet 5d2dfe8011 Add support for merge keys
Merge keys are specified here[1] for YAML 1.1. While not part of
the YAML 1.2 specification, they're very useful and are supported
in other implementations[2][3][4] that target 1.2.

Support for merge keys is optional and disabled by default. It can
be enabled by defining YAML_CPP_SUPPORT_MERGE_KEYS, either directly
or by setting the CMake option YAML_CPP_SUPPORT_MERGE_KEYS=ON.

[1]: http://yaml.org/type/merge.html
[2]: https://github.com/go-yaml/yaml
[3]: https://github.com/ruby/psych
[4]: https://bitbucket.org/ruamel/yaml
2018-08-23 19:53:38 +01:00
Alexander 1698b47b65 Use nullptr instead of 0 or NULL (clang-tidy warns) (#603) 2018-07-02 23:59:04 -05:00
Alexander Karatarakis 0f9a586ca1 Fix _NOEXCEPT not available in VS2017 15.8. Use noexcept for VS2015+ (#597) 2018-06-30 16:23:28 -05:00
Jiao 4fb1c4b92b Enable items to be removed from a sequence (#582) 2018-05-14 08:50:28 -05:00
Patrick Stotko ab5f9259a4 Use target-based include_directories to enable modern CMake usage (#583) 2018-05-05 11:03:13 -05:00
Tanki Zhang 124ae47600 Update the iterator implementation for C++17 (#575)
Fix the compiler error which prevents deriving from std::iterator in C++17
2018-04-10 23:47:23 -05:00
maek f996468a6a Fix UTF-8 code points emitting (#571) 2018-03-24 10:48:39 -05:00
Jesse Beder 562aefc114 Bump CMake version to 0.6.2.
It was incorrectly left at 0.6.0 when 0.6.1 was released, so this needs to be done to make it consistent between the tagged version and the CMake version.
2018-03-05 22:56:23 -06:00
22 changed files with 230 additions and 51 deletions
+1
View File
@@ -1 +1,2 @@
build/
/tags
+4 -1
View File
@@ -5,6 +5,9 @@ os:
compiler:
- clang
- gcc
env:
- YAML_CPP_SUPPORT_MERGE_KEYS=ON
- YAML_CPP_SUPPORT_MERGE_KEYS=OFF
before_install:
- |
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
@@ -17,7 +20,7 @@ before_install:
before_script:
- mkdir build
- cd build
- cmake ..
- cmake .. -DYAML_CPP_SUPPORT_MERGE_KEYS=$YAML_CPP_SUPPORT_MERGE_KEYS
script:
- make
- test/run-tests
+18 -4
View File
@@ -28,7 +28,7 @@ project(YAML_CPP)
set(YAML_CPP_VERSION_MAJOR "0")
set(YAML_CPP_VERSION_MINOR "6")
set(YAML_CPP_VERSION_PATCH "0")
set(YAML_CPP_VERSION_PATCH "2")
set(YAML_CPP_VERSION "${YAML_CPP_VERSION_MAJOR}.${YAML_CPP_VERSION_MINOR}.${YAML_CPP_VERSION_PATCH}")
enable_testing()
@@ -41,6 +41,7 @@ enable_testing()
option(YAML_CPP_BUILD_TESTS "Enable testing" ON)
option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
option(YAML_CPP_BUILD_CONTRIB "Enable contrib stuff in library" ON)
option(YAML_CPP_SUPPORT_MERGE_KEYS "Support YAML merge keys ('<<') in yaml-cpp's executable targets. Use '#define YAML_CPP_SUPPORT_MERGE_KEYS' instead when linking from another project." OFF)
## Build options
# --> General
@@ -97,6 +98,10 @@ else()
add_definitions(-DYAML_CPP_NO_CONTRIB)
endif()
if (YAML_CPP_SUPPORT_MERGE_KEYS)
add_definitions(-DYAML_CPP_SUPPORT_MERGE_KEYS)
endif()
set(library_sources
${sources}
${public_headers}
@@ -116,9 +121,10 @@ if(VERBOSE)
message(STATUS "contrib_private_headers: ${contrib_private_headers}")
endif()
include_directories(${YAML_CPP_SOURCE_DIR}/src)
include_directories(${YAML_CPP_SOURCE_DIR}/include)
if (CMAKE_VERSION VERSION_LESS 2.8.12)
include_directories(${YAML_CPP_SOURCE_DIR}/src)
include_directories(${YAML_CPP_SOURCE_DIR}/include)
endif()
###
@@ -275,6 +281,14 @@ set(_INSTALL_DESTINATIONS
### Library
###
add_library(yaml-cpp ${library_sources})
if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
target_include_directories(yaml-cpp
PUBLIC $<BUILD_INTERFACE:${YAML_CPP_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_ROOT_DIR}>
PRIVATE $<BUILD_INTERFACE:${YAML_CPP_SOURCE_DIR}/src>)
endif()
set_target_properties(yaml-cpp PROPERTIES
COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags}"
)
+1 -1
View File
@@ -15,7 +15,7 @@
// This is here for compatibility with older versions of Visual Studio
// which don't support noexcept
#ifdef _MSC_VER
#if defined(_MSC_VER) && _MSC_VER < 1900
#define YAML_CPP_NOEXCEPT _NOEXCEPT
#else
#define YAML_CPP_NOEXCEPT noexcept
+48 -15
View File
@@ -32,7 +32,7 @@ struct get_idx<Key,
static node* get(std::vector<node*>& sequence, const Key& key,
shared_memory_holder pMemory) {
if (key > sequence.size() || (key > 0 && !sequence[key-1]->is_defined()))
if (key > sequence.size() || (key > 0 && !sequence[key - 1]->is_defined()))
return 0;
if (key == sequence.size())
sequence.push_back(&pMemory->create_node());
@@ -56,6 +56,37 @@ struct get_idx<Key, typename std::enable_if<std::is_signed<Key>::value>::type> {
}
};
template <typename Key, typename Enable = void>
struct remove_idx {
static bool remove(std::vector<node*>&, const Key&) { return false; }
};
template <typename Key>
struct remove_idx<
Key, typename std::enable_if<std::is_unsigned<Key>::value &&
!std::is_same<Key, bool>::value>::type> {
static bool remove(std::vector<node*>& sequence, const Key& key) {
if (key >= sequence.size()) {
return false;
} else {
sequence.erase(sequence.begin() + key);
return true;
}
}
};
template <typename Key>
struct remove_idx<Key,
typename std::enable_if<std::is_signed<Key>::value>::type> {
static bool remove(std::vector<node*>& sequence, const Key& key) {
return key >= 0 ? remove_idx<std::size_t>::remove(
sequence, static_cast<std::size_t>(key))
: false;
}
};
template <typename T>
inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
T lhs;
@@ -129,21 +160,23 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) {
template <typename Key>
inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) {
if (m_type != NodeType::Map)
return false;
if (m_type == NodeType::Sequence) {
return remove_idx<Key>::remove(m_sequence, key);
} else if (m_type == NodeType::Map) {
kv_pairs::iterator it = m_undefinedPairs.begin();
while (it != m_undefinedPairs.end()) {
kv_pairs::iterator jt = std::next(it);
if (it->first->equals(key, pMemory)) {
m_undefinedPairs.erase(it);
}
it = jt;
}
for (kv_pairs::iterator it = m_undefinedPairs.begin();
it != m_undefinedPairs.end();) {
kv_pairs::iterator jt = std::next(it);
if (it->first->equals(key, pMemory))
m_undefinedPairs.erase(it);
it = jt;
}
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->equals(key, pMemory)) {
m_map.erase(it);
return true;
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->equals(key, pMemory)) {
m_map.erase(it);
return true;
}
}
}
+10 -6
View File
@@ -8,19 +8,19 @@
#endif
#include "yaml-cpp/dll.h"
#include "yaml-cpp/node/detail/node_iterator.h"
#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/ptr.h"
#include "yaml-cpp/node/detail/node_iterator.h"
#include <cstddef>
#include <iterator>
namespace YAML {
namespace detail {
struct iterator_value;
template <typename V>
class iterator_base : public std::iterator<std::forward_iterator_tag, V,
std::ptrdiff_t, V*, V> {
class iterator_base {
private:
template <typename>
@@ -37,7 +37,11 @@ class iterator_base : public std::iterator<std::forward_iterator_tag, V,
};
public:
typedef typename iterator_base::value_type value_type;
using iterator_category = std::forward_iterator_tag;
using value_type = V;
using difference_type = std::ptrdiff_t;
using pointer = V*;
using reference = V;
public:
iterator_base() : m_iterator(), m_pMemory() {}
@@ -86,7 +90,7 @@ class iterator_base : public std::iterator<std::forward_iterator_tag, V,
base_type m_iterator;
shared_memory_holder m_pMemory;
};
}
}
} // namespace detail
} // namespace YAML
#endif // VALUE_DETAIL_ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
+39 -1
View File
@@ -122,12 +122,23 @@ class node {
// NOTE: this returns a non-const node so that the top-level Node can wrap
// it, and returns a pointer so that it can be NULL (if there is no such
// key).
return static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
node* value = static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
if (!value || value->type() == NodeType::Undefined) {
return get_value_from_merge_key(key, value, pMemory);
}
#endif
return value;
}
template <typename Key>
node& get(const Key& key, shared_memory_holder pMemory) {
node& value = m_pRef->get(key, pMemory);
value.add_dependency(*this);
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
if (value.type() == NodeType::Undefined) {
return *get_value_from_merge_key(key, &value, pMemory);
}
#endif
return value;
}
template <typename Key>
@@ -159,6 +170,33 @@ class node {
}
private:
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
template <typename Key>
inline node* get_value_from_merge_key(const Key& key, node* currentValue,
shared_memory_holder pMemory) const {
node* mergeValue =
static_cast<const node_ref&>(*m_pRef).get(std::string("<<"), pMemory);
if (!mergeValue) {
return currentValue;
}
if (mergeValue->type() == NodeType::Map) {
return &mergeValue->get(key, pMemory);
}
if (mergeValue->type() == NodeType::Sequence) {
for (const_node_iterator it = mergeValue->begin();
it != mergeValue->end(); ++it) {
if (it->pNode && it->pNode->type() == NodeType::Map) {
node* value = it->pNode->get(key, pMemory);
if (value && value->type() != NodeType::Undefined) {
return value;
}
}
}
}
return currentValue;
}
#endif
shared_node_ref m_pRef;
typedef std::set<node*> nodes;
nodes m_dependencies;
+1 -1
View File
@@ -11,7 +11,7 @@ void* BuildGraphOfNextDocument(Parser& parser,
if (parser.HandleNextDocument(eventHandler)) {
return eventHandler.RootNode();
} else {
return NULL;
return nullptr;
}
}
}
+3 -3
View File
@@ -49,7 +49,7 @@ void GraphBuilderAdapter::OnMapStart(const Mark &mark, const std::string &tag,
EmitterStyle::value /* style */) {
void *pNode = m_builder.NewMap(mark, tag, GetCurrentParent());
m_containers.push(ContainerFrame(pNode, m_pKeyNode));
m_pKeyNode = NULL;
m_pKeyNode = nullptr;
RegisterAnchor(anchor, pNode);
}
@@ -62,7 +62,7 @@ void GraphBuilderAdapter::OnMapEnd() {
void *GraphBuilderAdapter::GetCurrentParent() const {
if (m_containers.empty()) {
return NULL;
return nullptr;
}
return m_containers.top().pContainer;
}
@@ -83,7 +83,7 @@ void GraphBuilderAdapter::DispositionNode(void *pNode) {
if (m_containers.top().isMap()) {
if (m_pKeyNode) {
m_builder.AssignInMap(pContainer, m_pKeyNode, pNode);
m_pKeyNode = NULL;
m_pKeyNode = nullptr;
} else {
m_pKeyNode = pNode;
}
+1 -1
View File
@@ -26,7 +26,7 @@ namespace YAML {
class GraphBuilderAdapter : public EventHandler {
public:
GraphBuilderAdapter(GraphBuilderInterface& builder)
: m_builder(builder), m_pRootNode(NULL), m_pKeyNode(NULL) {}
: m_builder(builder), m_pRootNode(nullptr), m_pKeyNode(nullptr) {}
virtual void OnDocumentStart(const Mark& mark) { (void)mark; }
virtual void OnDocumentEnd() {}
+3 -3
View File
@@ -134,12 +134,12 @@ void WriteCodePoint(ostream_wrapper& out, int codePoint) {
if (codePoint < 0 || codePoint > 0x10FFFF) {
codePoint = REPLACEMENT_CHARACTER;
}
if (codePoint < 0x7F) {
if (codePoint <= 0x7F) {
out << static_cast<char>(codePoint);
} else if (codePoint < 0x7FF) {
} else if (codePoint <= 0x7FF) {
out << static_cast<char>(0xC0 | (codePoint >> 6))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
} else if (codePoint < 0xFFFF) {
} else if (codePoint <= 0xFFFF) {
out << static_cast<char>(0xE0 | (codePoint >> 12))
<< static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
+1 -1
View File
@@ -2,7 +2,7 @@
// This is here for compatibility with older versions of Visual Studio
// which don't support noexcept
#ifdef _MSC_VER
#if defined(_MSC_VER) && _MSC_VER < 1900
#define YAML_CPP_NOEXCEPT _NOEXCEPT
#else
#define YAML_CPP_NOEXCEPT noexcept
+10 -2
View File
@@ -197,7 +197,7 @@ void node_data::insert(node& key, node& value, shared_memory_holder pMemory) {
// indexing
node* node_data::get(node& key, shared_memory_holder /* pMemory */) const {
if (m_type != NodeType::Map) {
return NULL;
return nullptr;
}
for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
@@ -205,7 +205,7 @@ node* node_data::get(node& key, shared_memory_holder /* pMemory */) const {
return it->second;
}
return NULL;
return nullptr;
}
node& node_data::get(node& key, shared_memory_holder pMemory) {
@@ -235,6 +235,14 @@ bool node_data::remove(node& key, shared_memory_holder /* pMemory */) {
if (m_type != NodeType::Map)
return false;
kv_pairs::iterator it = m_undefinedPairs.begin();
while (it != m_undefinedPairs.end()) {
kv_pairs::iterator jt = std::next(it);
if (it->first->is(key))
m_undefinedPairs.erase(it);
it = jt;
}
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->is(key)) {
m_map.erase(it);
+2 -2
View File
@@ -11,8 +11,8 @@ namespace YAML {
struct Mark;
NodeBuilder::NodeBuilder()
: m_pMemory(new detail::memory_holder), m_pRoot(0), m_mapDepth(0) {
m_anchors.push_back(0); // since the anchors start at 1
: m_pMemory(new detail::memory_holder), m_pRoot(nullptr), m_mapDepth(0) {
m_anchors.push_back(nullptr); // since the anchors start at 1
}
NodeBuilder::~NodeBuilder() {}
+1 -1
View File
@@ -7,7 +7,7 @@
namespace YAML {
ostream_wrapper::ostream_wrapper()
: m_buffer(1, '\0'),
m_pStream(0),
m_pStream(nullptr),
m_pos(0),
m_row(0),
m_col(0),
+3 -3
View File
@@ -282,7 +282,7 @@ Scanner::IndentMarker* Scanner::PushIndentTo(int column,
IndentMarker::INDENT_TYPE type) {
// are we in flow?
if (InFlowContext()) {
return 0;
return nullptr;
}
std::unique_ptr<IndentMarker> pIndent(new IndentMarker(column, type));
@@ -291,12 +291,12 @@ Scanner::IndentMarker* Scanner::PushIndentTo(int column,
// is this actually an indentation?
if (indent.column < lastIndent.column) {
return 0;
return nullptr;
}
if (indent.column == lastIndent.column &&
!(indent.type == IndentMarker::SEQ &&
lastIndent.type == IndentMarker::MAP)) {
return 0;
return nullptr;
}
// push a start token
+1 -1
View File
@@ -49,7 +49,7 @@ class Scanner {
enum INDENT_TYPE { MAP, SEQ, NONE };
enum STATUS { VALID, INVALID, UNKNOWN };
IndentMarker(int column_, INDENT_TYPE type_)
: column(column_), type(type_), status(VALID), pStartToken(0) {}
: column(column_), type(type_), status(VALID), pStartToken(nullptr) {}
int column;
INDENT_TYPE type;
+1 -1
View File
@@ -5,7 +5,7 @@ namespace YAML {
struct Mark;
Scanner::SimpleKey::SimpleKey(const Mark& mark_, std::size_t flowLevel_)
: mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0) {}
: mark(mark_), flowLevel(flowLevel_), pIndent(nullptr), pMapStart(nullptr), pKey(nullptr) {}
void Scanner::SimpleKey::Validate() {
// Note: pIndent will *not* be garbage here;
+1
View File
@@ -30,6 +30,7 @@ file(GLOB test_new_api_sources new-api/[a-z]*.cpp)
list(APPEND test_sources ${test_new_api_sources})
add_sources(${test_sources} ${test_headers})
include_directories(${YAML_CPP_SOURCE_DIR}/src)
include_directories(${YAML_CPP_SOURCE_DIR}/test)
add_executable(run-tests
+33
View File
@@ -185,6 +185,39 @@ TEST(LoadNodeTest, DereferenceIteratorError) {
EXPECT_THROW(node.begin()->begin()->Type(), InvalidNode);
}
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
TEST(NodeTest, MergeKeyScalarSupport) {
Node node = Load("{<<: {a: 1}}");
ASSERT_FALSE(!node["a"]);
EXPECT_EQ(1, node["a"].as<int>());
}
TEST(NodeTest, MergeKeyExistingKey) {
Node node = Load("{a: 1, <<: {a: 2}}");
ASSERT_FALSE(!node["a"]);
EXPECT_EQ(1, node["a"].as<int>());
}
TEST(NodeTest, MergeKeySequenceSupport) {
Node node = Load("<<: [{a: 1}, {a: 2, b: 3}]");
ASSERT_FALSE(!node["a"]);
ASSERT_FALSE(!node["b"]);
EXPECT_EQ(1, node["a"].as<int>());
EXPECT_EQ(3, node["b"].as<int>());
}
TEST(NodeTest, NestedMergeKeys) {
Node node = Load("{<<: {<<: {a: 1}}}");
ASSERT_FALSE(!node["a"]);
EXPECT_EQ(1, node["a"].as<int>());
}
#else
TEST(NodeTest, MergeKeySupport) {
Node node = Load("{<<: {a: 1}}");
ASSERT_FALSE(node["a"]);
}
#endif
TEST(NodeTest, EmitEmptyNode) {
Node node;
Emitter emitter;

Some files were not shown because too many files have changed in this diff Show More