Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
0bfa7a70e6
@ -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>();
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "yaml-cpp/binary.h"
|
#include "yaml-cpp/binary.h"
|
||||||
#include "yaml-cpp/node/impl.h"
|
#include "yaml-cpp/node/impl.h"
|
||||||
@ -28,10 +29,14 @@
|
|||||||
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
|
||||||
|
|
||||||
|
#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) {
|
||||||
@ -53,9 +58,11 @@ template <>
|
|||||||
struct convert<Node> {
|
struct convert<Node> {
|
||||||
static Node encode(const Node& rhs) { return rhs; }
|
static Node encode(const Node& rhs) { return rhs; }
|
||||||
|
|
||||||
static bool decode(const Node& node, Node& rhs) {
|
static Node decode(const Node& node) { //FIXME, this is dangerous
|
||||||
|
throw std::runtime_error("this should not have been encountered");
|
||||||
|
Node rhs;
|
||||||
rhs.reset(node);
|
rhs.reset(node);
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,12 +71,12 @@ 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;
|
BAD_DECODE_EXCEPTION
|
||||||
rhs = node.Scalar();
|
return node.Scalar();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// C-strings can only be encoded
|
// C-strings can only be encoded
|
||||||
@ -92,8 +99,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())
|
||||||
|
BAD_DECODE_EXCEPTION
|
||||||
|
return _Null();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -157,37 +166,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; \
|
BAD_DECODE_EXCEPTION; \
|
||||||
} \
|
} \
|
||||||
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; \
|
BAD_DECODE_EXCEPTION \
|
||||||
} \
|
} \
|
||||||
|
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; \
|
BAD_DECODE_EXCEPTION \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,7 +230,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
|
||||||
@ -236,11 +243,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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
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:
|
||||||
@ -248,7 +255,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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -288,11 +295,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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
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:
|
||||||
@ -300,7 +307,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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -314,11 +321,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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
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:
|
||||||
@ -326,7 +333,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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -341,11 +348,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;
|
BAD_DECODE_EXCEPTION
|
||||||
}
|
|
||||||
|
|
||||||
|
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:
|
||||||
@ -354,7 +361,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:
|
||||||
@ -373,12 +380,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() or node.size() != 2)
|
||||||
return false;
|
BAD_DECODE_EXCEPTION
|
||||||
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>();
|
||||||
@ -391,7 +397,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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -402,16 +408,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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
|
Binary rhs;
|
||||||
rhs.swap(data);
|
rhs.swap(data);
|
||||||
return true;
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -98,19 +98,26 @@ 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)) {
|
const auto rslt = convert<T>::decode(Node(*this, pMemory));
|
||||||
return lhs == rhs;
|
return rslt == rhs;
|
||||||
|
} catch (const conversion::DecodeException& e) {
|
||||||
|
return false;
|
||||||
|
} catch(...) {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
}
|
}
|
||||||
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)) {
|
const auto rslt =
|
||||||
return lhs == rhs;
|
convert<std::string>::decode(Node(*this, std::move(pMemory)));
|
||||||
|
return rslt == rhs;
|
||||||
|
} catch (const conversion::DecodeException& e) {
|
||||||
|
return false;
|
||||||
|
} catch(...) {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// indexing
|
// indexing
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
inline Node::Node()
|
inline Node::Node()
|
||||||
: m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {}
|
: m_isValid(true), m_invalidKey{}, m_pMemory(nullptr), m_pNode(nullptr) {}
|
||||||
@ -97,10 +98,35 @@ 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 convert<T>::decode(node);
|
||||||
return t;
|
} catch (const conversion::DecodeException& e) {
|
||||||
return fallback;
|
return fallback;
|
||||||
|
} catch (...) {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//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 (...) {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -127,11 +153,14 @@ 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 convert<T>::decode(node);
|
||||||
return t;
|
} catch(const conversion::DecodeException& e) {
|
||||||
throw TypedBadConversion<T>(node.Mark());
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
}
|
} catch (...) {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -321,6 +350,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);
|
||||||
|
|||||||
@ -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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
// 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;
|
BAD_DECODE_EXCEPTION
|
||||||
|
|
||||||
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;
|
BAD_DECODE_EXCEPTION
|
||||||
}
|
}
|
||||||
} // namespace YAML
|
} // namespace YAML
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user