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;
|
||||
} // namespace YAML
|
||||
|
||||
#define BAD_DECODE_EXCEPTION throw YAML::conversion::DecodeException();
|
||||
|
||||
|
||||
namespace YAML {
|
||||
namespace conversion {
|
||||
inline bool IsInfinity(const std::string& input) {
|
||||
@ -58,12 +55,17 @@ template <>
|
||||
struct convert<Node> {
|
||||
static Node encode(const Node& rhs) { return rhs; }
|
||||
|
||||
static Node decode(const Node& node) { //FIXME, this is dangerous
|
||||
throw std::runtime_error("this should not have been encountered");
|
||||
Node rhs;
|
||||
static bool decode(const Node& node, Node& rhs) {
|
||||
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
|
||||
@ -73,10 +75,9 @@ struct convert<std::string> {
|
||||
|
||||
static std::string decode(const Node& node) {
|
||||
if (!node.IsScalar())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
return node.Scalar();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// C-strings can only be encoded
|
||||
@ -101,7 +102,7 @@ struct convert<_Null> {
|
||||
|
||||
static _Null decode(const Node& node) {
|
||||
if (!node.IsNull())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
return _Null();
|
||||
}
|
||||
};
|
||||
@ -166,19 +167,19 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
|
||||
return Node(stream.str()); \
|
||||
} \
|
||||
\
|
||||
static type decode(const Node& node) { \
|
||||
static type decode(const Node& node) { \
|
||||
if (node.Type() != NodeType::Scalar) { \
|
||||
BAD_DECODE_EXCEPTION; \
|
||||
throw YAML::conversion::DecodeException();; \
|
||||
} \
|
||||
const std::string& input = node.Scalar(); \
|
||||
std::stringstream stream(input); \
|
||||
stream.unsetf(std::ios::dec); \
|
||||
if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \
|
||||
BAD_DECODE_EXCEPTION \
|
||||
throw YAML::conversion::DecodeException(); \
|
||||
} \
|
||||
type rhs; \
|
||||
if (conversion::ConvertStreamTo(stream, rhs)) { \
|
||||
return rhs; \
|
||||
return rhs; \
|
||||
} \
|
||||
if (std::numeric_limits<type>::has_infinity) { \
|
||||
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) {
|
||||
if (!node.IsMap())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::map<K, V, C, A> rhs;
|
||||
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) {
|
||||
if (!node.IsMap())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::unordered_map<K, V, H, P, A> rhs;
|
||||
for (const auto& element : node)
|
||||
@ -297,7 +298,7 @@ struct convert<std::vector<T, A>> {
|
||||
|
||||
static std::vector<T, A> decode(const Node& node) {
|
||||
if (!node.IsSequence())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::vector<T, A> rhs;
|
||||
for (const auto& element : node)
|
||||
@ -323,7 +324,7 @@ struct convert<std::list<T,A>> {
|
||||
|
||||
static std::list<T,A> decode(const Node& node) {
|
||||
if (!node.IsSequence())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::list<T,A> rhs;
|
||||
for (const auto& element : node)
|
||||
@ -350,7 +351,7 @@ struct convert<std::array<T, N>> {
|
||||
|
||||
static std::array<T, N> decode(const Node& node) {
|
||||
if (!isNodeValid(node))
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::array<T, N> rhs;
|
||||
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) {
|
||||
if (!node.IsSequence() || node.size() != 2)
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::pair<T, U> rhs;
|
||||
#if defined(__GNUC__) && __GNUC__ < 4
|
||||
@ -410,11 +411,11 @@ struct convert<Binary> {
|
||||
|
||||
static Binary decode(const Node& node) {
|
||||
if (!node.IsScalar())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
std::vector<unsigned char> data = DecodeBase64(node.Scalar());
|
||||
if (data.empty() && !node.Scalar().empty())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
Binary rhs;
|
||||
rhs.swap(data);
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
|
||||
#include "yaml-cpp/node/detail/node.h"
|
||||
#include "yaml-cpp/node/detail/node_data.h"
|
||||
#include "yaml-cpp/node/api_switch.h"
|
||||
|
||||
#include <algorithm>
|
||||
#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>
|
||||
inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
|
||||
|
||||
try {
|
||||
#ifdef PRE_CPP17_SHIM
|
||||
return static_switch<decltype(has_decode_new_api<convert<T>>(
|
||||
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
|
||||
return static_api_switch<decltype(has_decode_new_api<convert<T>>(
|
||||
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
|
||||
@ -160,20 +109,19 @@ inline bool node::equals(const T& rhs, shared_memory_holder pMemory) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
#undef PRE_CPP17_SHIM
|
||||
|
||||
|
||||
inline bool node::equals(const char* rhs, shared_memory_holder pMemory) {
|
||||
std::string lhs;
|
||||
if (convert<std::string>::decode(Node(*this, std::move(pMemory)), lhs)) {
|
||||
return lhs == rhs;
|
||||
try {
|
||||
return static_api_switch<decltype(has_decode_new_api<convert<std::string>>(
|
||||
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
|
||||
template <typename Key>
|
||||
inline node* node_data::get(const Key& key,
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
#include "yaml-cpp/node/detail/node.h"
|
||||
#include "yaml-cpp/node/iterator.h"
|
||||
#include "yaml-cpp/node/node.h"
|
||||
#include "yaml-cpp/node/api_switch.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
@ -88,44 +89,6 @@ inline NodeType::value Node::Type() const {
|
||||
|
||||
// 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 <typename T, typename S>
|
||||
struct as_if {
|
||||
@ -137,19 +100,8 @@ struct as_if {
|
||||
return fallback;
|
||||
|
||||
try {
|
||||
#ifdef PRE_CPP17_SHIM
|
||||
return static_switch<decltype(has_decode_new_api<convert<T>>(
|
||||
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
|
||||
return detail::static_api_switch<decltype(detail::has_decode_new_api<convert<T>>(
|
||||
0))::value>::template decode<T>(node);
|
||||
} catch (const conversion::DecodeException& e) {
|
||||
return fallback;
|
||||
} catch (...) {
|
||||
@ -204,19 +156,8 @@ struct as_if<T, void> {
|
||||
throw TypedBadConversion<T>(node.Mark());
|
||||
|
||||
try {
|
||||
#ifdef PRE_CPP17_SHIM
|
||||
return static_switch<decltype(has_decode_new_api<convert<T>>(
|
||||
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
|
||||
return detail::static_api_switch<decltype(detail::has_decode_new_api<convert<T>>(
|
||||
0))::value>::template decode<T>(node);
|
||||
} catch(const conversion::DecodeException& e) {
|
||||
throw TypedBadConversion<T>(node.Mark());
|
||||
} catch (...) {
|
||||
@ -224,7 +165,6 @@ struct as_if<T, void> {
|
||||
}
|
||||
};
|
||||
};
|
||||
#undef PRE_CPP17_SHIM
|
||||
|
||||
template <>
|
||||
struct as_if<std::string, void> {
|
||||
|
||||
@ -40,7 +40,7 @@ bool IsFlexibleCase(const std::string& str) {
|
||||
namespace YAML {
|
||||
bool convert<bool>::decode(const Node& node) {
|
||||
if (!node.IsScalar())
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
// we can't use iostream bool extraction operators as they don't
|
||||
// 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()))
|
||||
BAD_DECODE_EXCEPTION
|
||||
throw YAML::conversion::DecodeException();
|
||||
|
||||
for (const auto& name : names) {
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user