Merge 6f79903b89 into 9eb1142900
This commit is contained in:
commit
6170121246
@ -178,11 +178,12 @@ struct convert<Vec3> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, Vec3& rhs) {
|
static Vec3 decode(const Node& node) {
|
||||||
if(!node.IsSequence() || node.size() != 3) {
|
if(!node.IsSequence() || node.size() != 3) {
|
||||||
return false;
|
throw YAML::conversion::DecodeException("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vec3 rhs;
|
||||||
rhs.x = node[0].as<double>();
|
rhs.x = node[0].as<double>();
|
||||||
rhs.y = node[1].as<double>();
|
rhs.y = node[1].as<double>();
|
||||||
rhs.z = node[2].as<double>();
|
rhs.z = node[2].as<double>();
|
||||||
@ -197,5 +198,50 @@ Then you could use `Vec3` wherever you could use any other type:
|
|||||||
```cpp
|
```cpp
|
||||||
YAML::Node node = YAML::Load("start: [1, 3, 0]");
|
YAML::Node node = YAML::Load("start: [1, 3, 0]");
|
||||||
Vec3 v = node["start"].as<Vec3>();
|
Vec3 v = node["start"].as<Vec3>();
|
||||||
node["end"] = Vec3(2, -1, 0);
|
node["end"] = Vec3{2, -1, 0};
|
||||||
|
```
|
||||||
|
|
||||||
|
Observe that in the above example the custom type is, decalred as
|
||||||
|
a struct, explicit default constructable and all its members are
|
||||||
|
exposed. For non default constructable types like
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
class NonDefCtorVec3 : public Vec3 {
|
||||||
|
using Vec3::x;
|
||||||
|
using Vec3::y;
|
||||||
|
using Vec3::z;
|
||||||
|
public:
|
||||||
|
NonDefCtorVec3(double x, double y, double z)
|
||||||
|
: Vec3() { this->x=x; this->y=y; this->z=z;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
```
|
||||||
|
a new API is available, that freshens up the signature of the 'convert<T>::decode'
|
||||||
|
method and introduces the abortion of the deserialization process by throwing
|
||||||
|
an `DecodeException`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
namespace YAML {
|
||||||
|
template <>
|
||||||
|
struct convert<NonDefCtorVec3> {
|
||||||
|
static Node encode(const NonDefCtorVec3& rhs) {
|
||||||
|
return convert<Vec3>::encode(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
static NonDefCtorVec3 decode(const Node& node) {
|
||||||
|
if (!node.IsSequence() || node.size() != 3) {
|
||||||
|
throw YAML::conversion::DecodeException();
|
||||||
|
}
|
||||||
|
return {node[0].as<double>(), node[1].as<double>(), node[2].as<double>()};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The behavior is exactly the same
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
YAML::Node node = YAML::Load("start: [1, 3, 0]");
|
||||||
|
NonDefCtorVec3 v = node["start"].as<NonDefCtorVec3>();
|
||||||
|
node["end"] = NonDefCtorVec3(2, -1, 0);
|
||||||
```
|
```
|
||||||
@ -298,6 +298,15 @@ class YAML_CPP_API BadFile : public Exception {
|
|||||||
BadFile(const BadFile&) = default;
|
BadFile(const BadFile&) = default;
|
||||||
~BadFile() YAML_CPP_NOEXCEPT override;
|
~BadFile() YAML_CPP_NOEXCEPT override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
namespace conversion{
|
||||||
|
class DecodeException : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
DecodeException(const std::string& s="") : std::runtime_error(s) {};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace YAML
|
} // namespace YAML
|
||||||
|
|
||||||
#endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
|||||||
53
include/yaml-cpp/node/api_switch.h
Normal file
53
include/yaml-cpp/node/api_switch.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
//
|
||||||
|
// Created by marcel on 3/3/22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef YAML_CPP_API_SWITCH_H
|
||||||
|
#define YAML_CPP_API_SWITCH_H
|
||||||
|
|
||||||
|
#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 "yaml-cpp/node/node.h"
|
||||||
|
#include "yaml-cpp/exceptions.h"
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace YAML{
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
//detect the method of the new api
|
||||||
|
template <typename>
|
||||||
|
std::false_type has_decode_new_api(long);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto has_decode_new_api(int)
|
||||||
|
-> decltype( T::decode(std::declval<const Node&>()), std::true_type{});
|
||||||
|
|
||||||
|
template <bool AorB>
|
||||||
|
struct static_api_switch;
|
||||||
|
|
||||||
|
template<> //new api call-path
|
||||||
|
struct static_api_switch<true> {
|
||||||
|
template<class T>
|
||||||
|
static T decode(const Node& node) {
|
||||||
|
return convert<T>::decode(node);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> //old api call-path
|
||||||
|
struct static_api_switch<false> {
|
||||||
|
template<class T>
|
||||||
|
static T decode(const Node& node) {
|
||||||
|
T t;
|
||||||
|
if (convert<T>::decode(node, t))
|
||||||
|
return t;
|
||||||
|
throw conversion::DecodeException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // YAML_CPP_API_SWITCH_H
|
||||||
@ -17,6 +17,7 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <valarray>
|
#include <valarray>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
|
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@ -33,6 +34,7 @@
|
|||||||
namespace YAML {
|
namespace YAML {
|
||||||
class Binary;
|
class Binary;
|
||||||
struct _Null;
|
struct _Null;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct convert;
|
struct convert;
|
||||||
} // namespace YAML
|
} // namespace YAML
|
||||||
@ -62,6 +64,13 @@ struct convert<Node> {
|
|||||||
rhs.reset(node);
|
rhs.reset(node);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static Node decode(const Node& node) {
|
||||||
|
// throw std::runtime_error("this should not have been encountered");
|
||||||
|
// Node rhs;
|
||||||
|
// rhs.reset(node);
|
||||||
|
// return rhs;
|
||||||
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
// std::string
|
// std::string
|
||||||
@ -69,11 +78,10 @@ template <>
|
|||||||
struct convert<std::string> {
|
struct convert<std::string> {
|
||||||
static Node encode(const std::string& rhs) { return Node(rhs); }
|
static Node encode(const std::string& rhs) { return Node(rhs); }
|
||||||
|
|
||||||
static bool decode(const Node& node, std::string& rhs) {
|
static std::string decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
rhs = node.Scalar();
|
return node.Scalar();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -111,8 +119,10 @@ template <>
|
|||||||
struct convert<_Null> {
|
struct convert<_Null> {
|
||||||
static Node encode(const _Null& /* rhs */) { return Node(); }
|
static Node encode(const _Null& /* rhs */) { return Node(); }
|
||||||
|
|
||||||
static bool decode(const Node& node, _Null& /* rhs */) {
|
static _Null decode(const Node& node) {
|
||||||
return node.IsNull();
|
if (!node.IsNull())
|
||||||
|
throw YAML::conversion::DecodeException();
|
||||||
|
return _Null();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -176,37 +186,35 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
|
|||||||
return Node(stream.str()); \
|
return Node(stream.str()); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static bool decode(const Node& node, type& rhs) { \
|
static type decode(const Node& node) { \
|
||||||
if (node.Type() != NodeType::Scalar) { \
|
if (node.Type() != NodeType::Scalar) { \
|
||||||
return false; \
|
throw YAML::conversion::DecodeException();; \
|
||||||
} \
|
} \
|
||||||
const std::string& input = node.Scalar(); \
|
const std::string& input = node.Scalar(); \
|
||||||
std::stringstream stream(input); \
|
std::stringstream stream(input); \
|
||||||
stream.unsetf(std::ios::dec); \
|
stream.unsetf(std::ios::dec); \
|
||||||
if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \
|
if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \
|
||||||
return false; \
|
throw YAML::conversion::DecodeException(); \
|
||||||
} \
|
} \
|
||||||
|
type rhs; \
|
||||||
if (conversion::ConvertStreamTo(stream, rhs)) { \
|
if (conversion::ConvertStreamTo(stream, rhs)) { \
|
||||||
return true; \
|
return rhs; \
|
||||||
} \
|
} \
|
||||||
if (std::numeric_limits<type>::has_infinity) { \
|
if (std::numeric_limits<type>::has_infinity) { \
|
||||||
if (conversion::IsInfinity(input)) { \
|
if (conversion::IsInfinity(input)) { \
|
||||||
rhs = std::numeric_limits<type>::infinity(); \
|
return std::numeric_limits<type>::infinity(); \
|
||||||
return true; \
|
|
||||||
} else if (conversion::IsNegativeInfinity(input)) { \
|
} else if (conversion::IsNegativeInfinity(input)) { \
|
||||||
rhs = negative_op std::numeric_limits<type>::infinity(); \
|
return negative_op std::numeric_limits<type>::infinity(); \
|
||||||
return true; \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
if (std::numeric_limits<type>::has_quiet_NaN) { \
|
if (std::numeric_limits<type>::has_quiet_NaN) { \
|
||||||
if (conversion::IsNaN(input)) { \
|
if (conversion::IsNaN(input)) { \
|
||||||
rhs = std::numeric_limits<type>::quiet_NaN(); \
|
return std::numeric_limits<type>::quiet_NaN(); \
|
||||||
return true; \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
return false; \
|
throw YAML::conversion::DecodeException(); \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,7 +250,7 @@ template <>
|
|||||||
struct convert<bool> {
|
struct convert<bool> {
|
||||||
static Node encode(bool rhs) { return rhs ? Node("true") : Node("false"); }
|
static Node encode(bool rhs) { return rhs ? Node("true") : Node("false"); }
|
||||||
|
|
||||||
YAML_CPP_API static bool decode(const Node& node, bool& rhs);
|
YAML_CPP_API static bool decode(const Node& node);
|
||||||
};
|
};
|
||||||
|
|
||||||
// std::map
|
// std::map
|
||||||
@ -255,11 +263,11 @@ struct convert<std::map<K, V, C, A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, std::map<K, V, C, A>& rhs) {
|
static std::map<K, V, C, A> decode(const Node& node) {
|
||||||
if (!node.IsMap())
|
if (!node.IsMap())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
rhs.clear();
|
std::map<K, V, C, A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
// workaround for GCC 3:
|
// workaround for GCC 3:
|
||||||
@ -267,7 +275,7 @@ struct convert<std::map<K, V, C, A>> {
|
|||||||
#else
|
#else
|
||||||
rhs[element.first.as<K>()] = element.second.as<V>();
|
rhs[element.first.as<K>()] = element.second.as<V>();
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -281,11 +289,11 @@ struct convert<std::unordered_map<K, V, H, P, A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, std::unordered_map<K, V, H, P, A>& rhs) {
|
static std::unordered_map<K, V, H, P, A> decode(const Node& node) {
|
||||||
if (!node.IsMap())
|
if (!node.IsMap())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
rhs.clear();
|
std::unordered_map<K, V, H, P, A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
// workaround for GCC 3:
|
// workaround for GCC 3:
|
||||||
@ -293,7 +301,7 @@ struct convert<std::unordered_map<K, V, H, P, A>> {
|
|||||||
#else
|
#else
|
||||||
rhs[element.first.as<K>()] = element.second.as<V>();
|
rhs[element.first.as<K>()] = element.second.as<V>();
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -307,11 +315,11 @@ struct convert<std::vector<T, A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, std::vector<T, A>& rhs) {
|
static std::vector<T, A> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
rhs.clear();
|
std::vector<T, A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
// workaround for GCC 3:
|
// workaround for GCC 3:
|
||||||
@ -319,7 +327,7 @@ struct convert<std::vector<T, A>> {
|
|||||||
#else
|
#else
|
||||||
rhs.push_back(element.as<T>());
|
rhs.push_back(element.as<T>());
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -333,11 +341,11 @@ struct convert<std::list<T,A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, std::list<T,A>& rhs) {
|
static std::list<T,A> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
rhs.clear();
|
std::list<T,A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
// workaround for GCC 3:
|
// workaround for GCC 3:
|
||||||
@ -345,7 +353,7 @@ struct convert<std::list<T,A>> {
|
|||||||
#else
|
#else
|
||||||
rhs.push_back(element.as<T>());
|
rhs.push_back(element.as<T>());
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -360,11 +368,11 @@ struct convert<std::array<T, N>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, std::array<T, N>& rhs) {
|
static std::array<T, N> decode(const Node& node) {
|
||||||
if (!isNodeValid(node)) {
|
if (!isNodeValid(node))
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
}
|
|
||||||
|
|
||||||
|
std::array<T, N> rhs;
|
||||||
for (auto i = 0u; i < node.size(); ++i) {
|
for (auto i = 0u; i < node.size(); ++i) {
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
// workaround for GCC 3:
|
// workaround for GCC 3:
|
||||||
@ -373,7 +381,7 @@ struct convert<std::array<T, N>> {
|
|||||||
rhs[i] = node[i].as<T>();
|
rhs[i] = node[i].as<T>();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -423,12 +431,11 @@ struct convert<std::pair<T, U>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, std::pair<T, U>& rhs) {
|
static std::pair<T, U> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence() || node.size() != 2)
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
if (node.size() != 2)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
|
std::pair<T, U> rhs;
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
// workaround for GCC 3:
|
// workaround for GCC 3:
|
||||||
rhs.first = node[0].template as<T>();
|
rhs.first = node[0].template as<T>();
|
||||||
@ -441,7 +448,7 @@ struct convert<std::pair<T, U>> {
|
|||||||
#else
|
#else
|
||||||
rhs.second = node[1].as<U>();
|
rhs.second = node[1].as<U>();
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -452,16 +459,17 @@ struct convert<Binary> {
|
|||||||
return Node(EncodeBase64(rhs.data(), rhs.size()));
|
return Node(EncodeBase64(rhs.data(), rhs.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool decode(const Node& node, Binary& rhs) {
|
static Binary decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::vector<unsigned char> data = DecodeBase64(node.Scalar());
|
std::vector<unsigned char> data = DecodeBase64(node.Scalar());
|
||||||
if (data.empty() && !node.Scalar().empty())
|
if (data.empty() && !node.Scalar().empty())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
|
Binary rhs;
|
||||||
rhs.swap(data);
|
rhs.swap(data);
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "yaml-cpp/node/detail/node.h"
|
#include "yaml-cpp/node/detail/node.h"
|
||||||
#include "yaml-cpp/node/detail/node_data.h"
|
#include "yaml-cpp/node/detail/node_data.h"
|
||||||
|
#include "yaml-cpp/node/api_switch.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@ -98,19 +99,27 @@ struct remove_idx<Key,
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
|
inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
|
||||||
T lhs;
|
try {
|
||||||
if (convert<T>::decode(Node(*this, pMemory), lhs)) {
|
return static_api_switch<decltype(has_decode_new_api<convert<T>>(
|
||||||
return lhs == rhs;
|
0))::value>::template decode<T>(Node(*this, pMemory)) == rhs;
|
||||||
|
} catch(const conversion::DecodeException& e) {
|
||||||
|
//throw; //prefer to throw over returning just the inability to deserialize
|
||||||
|
return false; //not doing this breaks upstream functionality
|
||||||
|
} catch (...) {
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool node::equals(const char* rhs, shared_memory_holder pMemory) {
|
inline bool node::equals(const char* rhs, shared_memory_holder pMemory) {
|
||||||
std::string lhs;
|
try {
|
||||||
if (convert<std::string>::decode(Node(*this, std::move(pMemory)), lhs)) {
|
return static_api_switch<decltype(has_decode_new_api<convert<std::string>>(
|
||||||
return lhs == rhs;
|
0))::value>::template decode<std::string>(Node(*this, std::move(pMemory))) == rhs;
|
||||||
|
} catch(const conversion::DecodeException& e) {
|
||||||
|
//throw; //prefer to throw over returning just the inability to deserialize
|
||||||
|
return false; //not doing this breaks upstream functionality
|
||||||
|
} catch (...) {
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// indexing
|
// indexing
|
||||||
|
|||||||
@ -12,8 +12,10 @@
|
|||||||
#include "yaml-cpp/node/detail/node.h"
|
#include "yaml-cpp/node/detail/node.h"
|
||||||
#include "yaml-cpp/node/iterator.h"
|
#include "yaml-cpp/node/iterator.h"
|
||||||
#include "yaml-cpp/node/node.h"
|
#include "yaml-cpp/node/node.h"
|
||||||
|
#include "yaml-cpp/node/api_switch.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
inline Node::Node()
|
inline Node::Node()
|
||||||
@ -97,10 +99,36 @@ struct as_if {
|
|||||||
if (!node.m_pNode)
|
if (!node.m_pNode)
|
||||||
return fallback;
|
return fallback;
|
||||||
|
|
||||||
T t;
|
try {
|
||||||
if (convert<T>::decode(node, t))
|
return detail::static_api_switch<decltype(detail::has_decode_new_api<convert<T>>(
|
||||||
return t;
|
0))::value>::template decode<T>(node);
|
||||||
return fallback;
|
} catch (const conversion::DecodeException& e) {
|
||||||
|
return fallback;
|
||||||
|
} catch (...) {
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
//specialize for Node
|
||||||
|
template <typename S>
|
||||||
|
struct as_if<Node, S> {
|
||||||
|
explicit as_if(const Node& node_) : node(node_) {}
|
||||||
|
const Node& node;
|
||||||
|
|
||||||
|
Node operator()(const S& fallback) const {
|
||||||
|
if (!node.m_pNode)
|
||||||
|
return fallback;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Node n;
|
||||||
|
n.reset(node);
|
||||||
|
return node;
|
||||||
|
} catch (const conversion::DecodeException& e) {
|
||||||
|
return fallback;
|
||||||
|
} catch (...) {
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -127,11 +155,15 @@ struct as_if<T, void> {
|
|||||||
if (!node.m_pNode)
|
if (!node.m_pNode)
|
||||||
throw TypedBadConversion<T>(node.Mark());
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
|
|
||||||
T t;
|
try {
|
||||||
if (convert<T>::decode(node, t))
|
return detail::static_api_switch<decltype(detail::has_decode_new_api<convert<T>>(
|
||||||
return t;
|
0))::value>::template decode<T>(node);
|
||||||
throw TypedBadConversion<T>(node.Mark());
|
} catch(const conversion::DecodeException& e) {
|
||||||
}
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
|
} catch (...) {
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -321,6 +353,16 @@ std::string key_to_string(const Key& key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// indexing
|
// indexing
|
||||||
|
template <typename Key>
|
||||||
|
inline bool Node::ContainsKey(const Key& key) const {
|
||||||
|
EnsureNodeExists();
|
||||||
|
if (! IsMap())
|
||||||
|
return false;
|
||||||
|
detail::node* value =
|
||||||
|
static_cast<const detail::node&>(*m_pNode).get(key, m_pMemory);
|
||||||
|
return (bool)value;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Key>
|
template <typename Key>
|
||||||
inline const Node Node::operator[](const Key& key) const {
|
inline const Node Node::operator[](const Key& key) const {
|
||||||
EnsureNodeExists();
|
EnsureNodeExists();
|
||||||
|
|||||||
@ -99,6 +99,8 @@ class YAML_CPP_API Node {
|
|||||||
|
|
||||||
// indexing
|
// indexing
|
||||||
template <typename Key>
|
template <typename Key>
|
||||||
|
bool ContainsKey(const Key& key) const;
|
||||||
|
template <typename Key>
|
||||||
const Node operator[](const Key& key) const;
|
const Node operator[](const Key& key) const;
|
||||||
template <typename Key>
|
template <typename Key>
|
||||||
Node operator[](const Key& key);
|
Node operator[](const Key& key);
|
||||||
@ -141,6 +143,7 @@ YAML_CPP_API bool operator==(const Node& lhs, const Node& rhs);
|
|||||||
|
|
||||||
YAML_CPP_API Node Clone(const Node& node);
|
YAML_CPP_API Node Clone(const Node& node);
|
||||||
|
|
||||||
|
//forward declare this customization point for all types
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct convert;
|
struct convert;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,9 +38,9 @@ bool IsFlexibleCase(const std::string& str) {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
bool convert<bool>::decode(const Node& node, bool& rhs) {
|
bool convert<bool>::decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
// we can't use iostream bool extraction operators as they don't
|
// we can't use iostream bool extraction operators as they don't
|
||||||
// recognize all possible values in the table below (taken from
|
// recognize all possible values in the table below (taken from
|
||||||
@ -55,20 +55,18 @@ bool convert<bool>::decode(const Node& node, bool& rhs) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!IsFlexibleCase(node.Scalar()))
|
if (!IsFlexibleCase(node.Scalar()))
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
for (const auto& name : names) {
|
for (const auto& name : names) {
|
||||||
if (name.truename == tolower(node.Scalar())) {
|
if (name.truename == tolower(node.Scalar())) {
|
||||||
rhs = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.falsename == tolower(node.Scalar())) {
|
if (name.falsename == tolower(node.Scalar())) {
|
||||||
rhs = false;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
throw YAML::conversion::DecodeException();
|
||||||
}
|
}
|
||||||
} // namespace YAML
|
} // namespace YAML
|
||||||
|
|||||||
@ -42,6 +42,30 @@ template <class T> using CustomList = std::list<T,CustomAllocator<T>>;
|
|||||||
template <class K, class V, class C=std::less<K>> using CustomMap = std::map<K,V,C,CustomAllocator<std::pair<const K,V>>>;
|
template <class K, class V, class C=std::less<K>> using CustomMap = std::map<K,V,C,CustomAllocator<std::pair<const K,V>>>;
|
||||||
template <class K, class V, class H=std::hash<K>, class P=std::equal_to<K>> using CustomUnorderedMap = std::unordered_map<K,V,H,P,CustomAllocator<std::pair<const K,V>>>;
|
template <class K, class V, class H=std::hash<K>, class P=std::equal_to<K>> using CustomUnorderedMap = std::unordered_map<K,V,H,P,CustomAllocator<std::pair<const K,V>>>;
|
||||||
|
|
||||||
|
struct Vec3 {
|
||||||
|
double x, y, z;
|
||||||
|
bool operator==(const Vec3& rhs) const {
|
||||||
|
return x == rhs.x && y == rhs.y && z == rhs.z;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class NonDefCtorVec3 : public Vec3 {
|
||||||
|
using Vec3::x;
|
||||||
|
using Vec3::y;
|
||||||
|
using Vec3::z;
|
||||||
|
public:
|
||||||
|
NonDefCtorVec3(double x, double y, double z)
|
||||||
|
: Vec3() {
|
||||||
|
this->x=x;
|
||||||
|
this->y=y;
|
||||||
|
this->z=z;
|
||||||
|
};
|
||||||
|
bool operator==(const NonDefCtorVec3& rhs) const {
|
||||||
|
return x == rhs.x && y == rhs.y && z == rhs.z;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
using ::testing::AnyOf;
|
using ::testing::AnyOf;
|
||||||
@ -56,6 +80,45 @@ using ::testing::Eq;
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
|
|
||||||
|
//define custom convert structs
|
||||||
|
template<>
|
||||||
|
struct convert<Vec3> {
|
||||||
|
static Node encode(const Vec3& rhs) {
|
||||||
|
Node node;
|
||||||
|
node.push_back(rhs.x);
|
||||||
|
node.push_back(rhs.y);
|
||||||
|
node.push_back(rhs.z);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool decode(const Node& node, Vec3& rhs) {
|
||||||
|
if(!node.IsSequence() || node.size() != 3) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
rhs.x = node[0].as<double>();
|
||||||
|
rhs.y = node[1].as<double>();
|
||||||
|
rhs.z = node[2].as<double>();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct convert<NonDefCtorVec3> {
|
||||||
|
static Node encode(const NonDefCtorVec3& rhs) {
|
||||||
|
return convert<Vec3>::encode(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
static NonDefCtorVec3 decode(const Node& node) {
|
||||||
|
if (!node.IsSequence() || node.size() != 3) {
|
||||||
|
throw YAML::conversion::DecodeException();
|
||||||
|
}
|
||||||
|
return {node[0].as<double>(), node[1].as<double>(), node[2].as<double>()};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
TEST(NodeTest, SimpleScalar) {
|
TEST(NodeTest, SimpleScalar) {
|
||||||
Node node = Node("Hello, World!");
|
Node node = Node("Hello, World!");
|
||||||
@ -725,6 +788,24 @@ TEST(NodeTest, AccessNonexistentKeyOnConstNode) {
|
|||||||
ASSERT_FALSE(other["5"]);
|
ASSERT_FALSE(other["5"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(NodeTest, CustomClassDecoding) {
|
||||||
|
YAML::Node node;
|
||||||
|
node.push_back(1.0);
|
||||||
|
node.push_back(2.0);
|
||||||
|
node.push_back(3.0);
|
||||||
|
ASSERT_TRUE(node.IsSequence());
|
||||||
|
EXPECT_EQ(node.as<Vec3>(), (Vec3{1.0, 2.0, 3.0}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(NodeTest, CustomNonDefaultConstructibleClassDecoding) {
|
||||||
|
YAML::Node node;
|
||||||
|
node.push_back(1.0);
|
||||||
|
node.push_back(2.0);
|
||||||
|
node.push_back(3.0);
|
||||||
|
ASSERT_TRUE(node.IsSequence());
|
||||||
|
EXPECT_EQ(node.as<NonDefCtorVec3>(), (NonDefCtorVec3{1.0, 2.0, 3.0}));
|
||||||
|
}
|
||||||
|
|
||||||
class NodeEmitterTest : public ::testing::Test {
|
class NodeEmitterTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
void ExpectOutput(const std::string& output, const Node& node) {
|
void ExpectOutput(const std::string& output, const Node& node) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user