slightly better structure; remove id-constexpr all together in favour of clearer code structure
This commit is contained in:
parent
cc2018e9f7
commit
6f79903b89
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
|
||||||
@ -34,9 +34,6 @@ template <typename T>
|
|||||||
struct convert;
|
struct convert;
|
||||||
} // namespace YAML
|
} // namespace YAML
|
||||||
|
|
||||||
#define BAD_DECODE_EXCEPTION throw YAML::conversion::DecodeException();
|
|
||||||
|
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
namespace conversion {
|
namespace conversion {
|
||||||
inline bool IsInfinity(const std::string& input) {
|
inline bool IsInfinity(const std::string& input) {
|
||||||
@ -58,12 +55,17 @@ template <>
|
|||||||
struct convert<Node> {
|
struct convert<Node> {
|
||||||
static Node encode(const Node& rhs) { return rhs; }
|
static Node encode(const Node& rhs) { return rhs; }
|
||||||
|
|
||||||
static Node decode(const Node& node) { //FIXME, this is dangerous
|
static bool decode(const Node& node, Node& rhs) {
|
||||||
throw std::runtime_error("this should not have been encountered");
|
|
||||||
Node rhs;
|
|
||||||
rhs.reset(node);
|
rhs.reset(node);
|
||||||
return rhs;
|
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
|
||||||
@ -73,10 +75,9 @@ struct convert<std::string> {
|
|||||||
|
|
||||||
static std::string decode(const Node& node) {
|
static std::string decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
return node.Scalar();
|
return node.Scalar();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// C-strings can only be encoded
|
// C-strings can only be encoded
|
||||||
@ -101,7 +102,7 @@ struct convert<_Null> {
|
|||||||
|
|
||||||
static _Null decode(const Node& node) {
|
static _Null decode(const Node& node) {
|
||||||
if (!node.IsNull())
|
if (!node.IsNull())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
return _Null();
|
return _Null();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -166,19 +167,19 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
|
|||||||
return Node(stream.str()); \
|
return Node(stream.str()); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static type decode(const Node& node) { \
|
static type decode(const Node& node) { \
|
||||||
if (node.Type() != NodeType::Scalar) { \
|
if (node.Type() != NodeType::Scalar) { \
|
||||||
BAD_DECODE_EXCEPTION; \
|
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) { \
|
||||||
BAD_DECODE_EXCEPTION \
|
throw YAML::conversion::DecodeException(); \
|
||||||
} \
|
} \
|
||||||
type rhs; \
|
type rhs; \
|
||||||
if (conversion::ConvertStreamTo(stream, rhs)) { \
|
if (conversion::ConvertStreamTo(stream, rhs)) { \
|
||||||
return rhs; \
|
return rhs; \
|
||||||
} \
|
} \
|
||||||
if (std::numeric_limits<type>::has_infinity) { \
|
if (std::numeric_limits<type>::has_infinity) { \
|
||||||
if (conversion::IsInfinity(input)) { \
|
if (conversion::IsInfinity(input)) { \
|
||||||
@ -194,7 +195,7 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
|
|||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
BAD_DECODE_EXCEPTION \
|
throw YAML::conversion::DecodeException(); \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +246,7 @@ struct convert<std::map<K, V, C, A>> {
|
|||||||
|
|
||||||
static std::map<K, V, C, A> decode(const Node& node) {
|
static std::map<K, V, C, A> decode(const Node& node) {
|
||||||
if (!node.IsMap())
|
if (!node.IsMap())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::map<K, V, C, A> rhs;
|
std::map<K, V, C, A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
@ -271,7 +272,7 @@ struct convert<std::unordered_map<K, V, H, P, A>> {
|
|||||||
|
|
||||||
static std::unordered_map<K, V, H, P, A> decode(const Node& node) {
|
static std::unordered_map<K, V, H, P, A> decode(const Node& node) {
|
||||||
if (!node.IsMap())
|
if (!node.IsMap())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::unordered_map<K, V, H, P, A> rhs;
|
std::unordered_map<K, V, H, P, A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
@ -297,7 +298,7 @@ struct convert<std::vector<T, A>> {
|
|||||||
|
|
||||||
static std::vector<T, A> decode(const Node& node) {
|
static std::vector<T, A> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::vector<T, A> rhs;
|
std::vector<T, A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
@ -323,7 +324,7 @@ struct convert<std::list<T,A>> {
|
|||||||
|
|
||||||
static std::list<T,A> decode(const Node& node) {
|
static std::list<T,A> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::list<T,A> rhs;
|
std::list<T,A> rhs;
|
||||||
for (const auto& element : node)
|
for (const auto& element : node)
|
||||||
@ -350,7 +351,7 @@ struct convert<std::array<T, N>> {
|
|||||||
|
|
||||||
static std::array<T, N> decode(const Node& node) {
|
static std::array<T, N> decode(const Node& node) {
|
||||||
if (!isNodeValid(node))
|
if (!isNodeValid(node))
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::array<T, N> rhs;
|
std::array<T, N> rhs;
|
||||||
for (auto i = 0u; i < node.size(); ++i) {
|
for (auto i = 0u; i < node.size(); ++i) {
|
||||||
@ -382,7 +383,7 @@ struct convert<std::pair<T, U>> {
|
|||||||
|
|
||||||
static std::pair<T, U> decode(const Node& node) {
|
static std::pair<T, U> decode(const Node& node) {
|
||||||
if (!node.IsSequence() || node.size() != 2)
|
if (!node.IsSequence() || node.size() != 2)
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
std::pair<T, U> rhs;
|
std::pair<T, U> rhs;
|
||||||
#if defined(__GNUC__) && __GNUC__ < 4
|
#if defined(__GNUC__) && __GNUC__ < 4
|
||||||
@ -410,11 +411,11 @@ struct convert<Binary> {
|
|||||||
|
|
||||||
static Binary decode(const Node& node) {
|
static Binary decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
BAD_DECODE_EXCEPTION
|
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())
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
|
|
||||||
Binary rhs;
|
Binary rhs;
|
||||||
rhs.swap(data);
|
rhs.swap(data);
|
||||||
|
|||||||
@ -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>
|
||||||
@ -96,63 +97,11 @@ struct remove_idx<Key,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//shim to emulate constexpr-if, which is a feature of c++17
|
|
||||||
#if __cplusplus < 201703L || (defined(_MSVC_LANG) && _MSVC_LANG < 201703L)
|
|
||||||
#define PRE_CPP17_SHIM
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef PRE_CPP17_SHIM
|
|
||||||
template <bool AorB>
|
|
||||||
struct static_switch;
|
|
||||||
|
|
||||||
template<> //new api
|
|
||||||
struct static_switch<true> {
|
|
||||||
template<class T>
|
|
||||||
static T call(const Node& node) {
|
|
||||||
return convert<T>::decode(node);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> //old api
|
|
||||||
struct static_switch<false> {
|
|
||||||
template<class T>
|
|
||||||
static T call(const Node& node) {
|
|
||||||
T t;
|
|
||||||
if (convert<T>::decode(node, t))
|
|
||||||
return t;
|
|
||||||
throw conversion::DecodeException();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//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 <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) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
#ifdef PRE_CPP17_SHIM
|
return static_api_switch<decltype(has_decode_new_api<convert<T>>(
|
||||||
return static_switch<decltype(has_decode_new_api<convert<T>>(
|
0))::value>::template decode<T>(Node(*this, pMemory)) == rhs;
|
||||||
0))::value>::template call<T>(Node(*this, pMemory)) == rhs;
|
|
||||||
#else
|
|
||||||
if constexpr (decltype(has_decode_new_api<convert<T>>(0))::value >)
|
|
||||||
return convert<T>::decode(Node(*this, pMemory)) == rhs;
|
|
||||||
else {
|
|
||||||
T lhs;
|
|
||||||
if (convert<T>::decode(Node(*this, pMemory), lhs))
|
|
||||||
return lhs == rhs;
|
|
||||||
throw conversion::DecodeException();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} catch(const conversion::DecodeException& e) {
|
} catch(const conversion::DecodeException& e) {
|
||||||
//throw; //prefer to throw over returning just the inability to deserialize
|
//throw; //prefer to throw over returning just the inability to deserialize
|
||||||
return false; //not doing this breaks upstream functionality
|
return false; //not doing this breaks upstream functionality
|
||||||
@ -160,20 +109,19 @@ inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#undef PRE_CPP17_SHIM
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
template <typename Key>
|
template <typename Key>
|
||||||
inline node* node_data::get(const Key& key,
|
inline node* node_data::get(const Key& key,
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
#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>
|
#include <type_traits>
|
||||||
@ -88,44 +89,6 @@ inline NodeType::value Node::Type() const {
|
|||||||
|
|
||||||
// access
|
// access
|
||||||
|
|
||||||
//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{});
|
|
||||||
|
|
||||||
//shim to emulate constexpr-if, which is a feature of c++17
|
|
||||||
#if __cplusplus < 201703L || (defined(_MSVC_LANG) && _MSVC_LANG < 201703L)
|
|
||||||
#define PRE_CPP17_SHIM
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef PRE_CPP17_SHIM
|
|
||||||
template <bool AorB>
|
|
||||||
struct static_switch;
|
|
||||||
|
|
||||||
template<> //new api
|
|
||||||
struct static_switch<true> {
|
|
||||||
template<class T>
|
|
||||||
static T call(const Node& node) {
|
|
||||||
return convert<T>::decode(node);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> //old api
|
|
||||||
struct static_switch<false> {
|
|
||||||
template<class T>
|
|
||||||
static T call(const Node& node) {
|
|
||||||
T t;
|
|
||||||
if (convert<T>::decode(node, t))
|
|
||||||
return t;
|
|
||||||
throw conversion::DecodeException();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
// template helpers
|
// template helpers
|
||||||
template <typename T, typename S>
|
template <typename T, typename S>
|
||||||
struct as_if {
|
struct as_if {
|
||||||
@ -137,19 +100,8 @@ struct as_if {
|
|||||||
return fallback;
|
return fallback;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
#ifdef PRE_CPP17_SHIM
|
return detail::static_api_switch<decltype(detail::has_decode_new_api<convert<T>>(
|
||||||
return static_switch<decltype(has_decode_new_api<convert<T>>(
|
0))::value>::template decode<T>(node);
|
||||||
0))::value>::template call<T>(node);
|
|
||||||
#else
|
|
||||||
if constexpr (decltype(has_decodex<convert<T>>(0))::value >)
|
|
||||||
return convert<T>::decodex(node);
|
|
||||||
else {
|
|
||||||
T t;
|
|
||||||
if (convert<T>::decode(node, t))
|
|
||||||
return t;
|
|
||||||
throw conversion::DecodeException();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} catch (const conversion::DecodeException& e) {
|
} catch (const conversion::DecodeException& e) {
|
||||||
return fallback;
|
return fallback;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
@ -204,19 +156,8 @@ struct as_if<T, void> {
|
|||||||
throw TypedBadConversion<T>(node.Mark());
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
#ifdef PRE_CPP17_SHIM
|
return detail::static_api_switch<decltype(detail::has_decode_new_api<convert<T>>(
|
||||||
return static_switch<decltype(has_decode_new_api<convert<T>>(
|
0))::value>::template decode<T>(node);
|
||||||
0))::value>::template call<T>(node);
|
|
||||||
#else
|
|
||||||
if constexpr (decltype(has_decodex<convert<T>>(0))::value >)
|
|
||||||
return convert<T>::decodex(node);
|
|
||||||
else {
|
|
||||||
T t;
|
|
||||||
if (convert<T>::decode(node, t))
|
|
||||||
return t;
|
|
||||||
throw conversion::DecodeException();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} catch(const conversion::DecodeException& e) {
|
} catch(const conversion::DecodeException& e) {
|
||||||
throw TypedBadConversion<T>(node.Mark());
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
@ -224,7 +165,6 @@ struct as_if<T, void> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
#undef PRE_CPP17_SHIM
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct as_if<std::string, void> {
|
struct as_if<std::string, void> {
|
||||||
|
|||||||
@ -40,7 +40,7 @@ bool IsFlexibleCase(const std::string& str) {
|
|||||||
namespace YAML {
|
namespace YAML {
|
||||||
bool convert<bool>::decode(const Node& node) {
|
bool convert<bool>::decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
BAD_DECODE_EXCEPTION
|
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,7 +55,7 @@ bool convert<bool>::decode(const Node& node) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!IsFlexibleCase(node.Scalar()))
|
if (!IsFlexibleCase(node.Scalar()))
|
||||||
BAD_DECODE_EXCEPTION
|
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())) {
|
||||||
@ -67,6 +67,6 @@ bool convert<bool>::decode(const Node& node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BAD_DECODE_EXCEPTION
|
throw YAML::conversion::DecodeException();
|
||||||
}
|
}
|
||||||
} // namespace YAML
|
} // namespace YAML
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user