return to single arg returns
This commit is contained in:
parent
abc841d184
commit
7a984e275a
@ -66,7 +66,8 @@ 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 bool decode(const Node& node, Node& rhs) { //FIXME, this is dangerous
|
||||||
|
throw std::runtime_error("this should not have been encountered");
|
||||||
rhs.reset(node);
|
rhs.reset(node);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -77,10 +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 std::pair<bool, std::string> decode(const Node& node) {
|
static std::string decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
return std::make_pair(true, node.Scalar());
|
return node.Scalar();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -105,10 +106,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 std::pair<bool, _Null> decode(const Node& node) {
|
static _Null decode(const Node& node) {
|
||||||
if (!node.IsNull())
|
if (!node.IsNull())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
return std::make_pair(node.IsNull(), _Null());
|
return _Null();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -172,7 +173,7 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
|
|||||||
return Node(stream.str()); \
|
return Node(stream.str()); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static std::pair<bool, type> decode(const Node& node) { \
|
static type decode(const Node& node) { \
|
||||||
if (node.Type() != NodeType::Scalar) { \
|
if (node.Type() != NodeType::Scalar) { \
|
||||||
throw conversion::DecodeException(""); \
|
throw conversion::DecodeException(""); \
|
||||||
} \
|
} \
|
||||||
@ -188,19 +189,15 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
|
|||||||
} \
|
} \
|
||||||
if (std::numeric_limits<type>::has_infinity) { \
|
if (std::numeric_limits<type>::has_infinity) { \
|
||||||
if (conversion::IsInfinity(input)) { \
|
if (conversion::IsInfinity(input)) { \
|
||||||
return std::make_pair(true, \
|
return std::numeric_limits<type>::infinity(); \
|
||||||
std::numeric_limits<type>::infinity()); \
|
|
||||||
} else if (conversion::IsNegativeInfinity(input)) { \
|
} else if (conversion::IsNegativeInfinity(input)) { \
|
||||||
return std::make_pair(true, \
|
return negative_op std::numeric_limits<type>::infinity(); \
|
||||||
negative_op std::numeric_limits<type>::infinity()); \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
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 std::make_pair(true, \
|
|
||||||
std::numeric_limits<type>::quiet_NaN()); \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
@ -240,7 +237,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 std::pair<bool, bool> decode(const Node& node);
|
YAML_CPP_API static bool decode(const Node& node);
|
||||||
};
|
};
|
||||||
|
|
||||||
// std::map
|
// std::map
|
||||||
@ -253,7 +250,7 @@ struct convert<std::map<K, V, C, A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<bool, 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())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -265,7 +262,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 std::make_pair(true, rhs);
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -279,7 +276,7 @@ struct convert<std::vector<T, A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<bool, std::vector<T, A> > decode(const Node& node) {
|
static std::vector<T, A> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -291,7 +288,7 @@ struct convert<std::vector<T, A>> {
|
|||||||
#else
|
#else
|
||||||
rhs.push_back(element.as<T>());
|
rhs.push_back(element.as<T>());
|
||||||
#endif
|
#endif
|
||||||
return std::make_pair(true, rhs);
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -305,7 +302,7 @@ struct convert<std::list<T,A>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<bool, std::list<T,A> > decode(const Node& node) {
|
static std::list<T,A> decode(const Node& node) {
|
||||||
if (!node.IsSequence())
|
if (!node.IsSequence())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -317,7 +314,7 @@ struct convert<std::list<T,A>> {
|
|||||||
#else
|
#else
|
||||||
rhs.push_back(element.as<T>());
|
rhs.push_back(element.as<T>());
|
||||||
#endif
|
#endif
|
||||||
return std::make_pair(true, rhs);
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -332,7 +329,7 @@ struct convert<std::array<T, N>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<bool, std::array<T, N> > decode(const Node& node) {
|
static std::array<T, N> decode(const Node& node) {
|
||||||
if (!isNodeValid(node))
|
if (!isNodeValid(node))
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -345,7 +342,7 @@ struct convert<std::array<T, N>> {
|
|||||||
rhs[i] = node[i].as<T>();
|
rhs[i] = node[i].as<T>();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return std::make_pair(true, rhs);
|
return rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -364,7 +361,7 @@ struct convert<std::pair<T, U>> {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<bool, std::pair<T, U> > decode(const Node& node) {
|
static std::pair<T, U> decode(const Node& node) {
|
||||||
if (!node.IsSequence() or node.size() != 2)
|
if (!node.IsSequence() or node.size() != 2)
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -381,7 +378,7 @@ struct convert<std::pair<T, U>> {
|
|||||||
#else
|
#else
|
||||||
rhs.second = node[1].as<U>();
|
rhs.second = node[1].as<U>();
|
||||||
#endif
|
#endif
|
||||||
return std::make_pair(true, rhs);
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -392,7 +389,7 @@ struct convert<Binary> {
|
|||||||
return Node(EncodeBase64(rhs.data(), rhs.size()));
|
return Node(EncodeBase64(rhs.data(), rhs.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<bool, Binary> decode(const Node& node) {
|
static Binary decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -402,7 +399,7 @@ struct convert<Binary> {
|
|||||||
|
|
||||||
Binary rhs;
|
Binary rhs;
|
||||||
rhs.swap(data);
|
rhs.swap(data);
|
||||||
return std::make_pair(true, rhs);
|
return rhs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -100,10 +100,7 @@ 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 {
|
||||||
const auto rslt = convert<T>::decode(Node(*this, pMemory));
|
const auto rslt = convert<T>::decode(Node(*this, pMemory));
|
||||||
if (rslt.first) {
|
return rslt == rhs;
|
||||||
return rslt.second == rhs;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (const conversion::DecodeException& e) {
|
} catch (const conversion::DecodeException& e) {
|
||||||
return false;
|
return false;
|
||||||
} catch(...) {
|
} catch(...) {
|
||||||
@ -115,9 +112,7 @@ inline bool node::equals(const char* rhs, shared_memory_holder pMemory) {
|
|||||||
try {
|
try {
|
||||||
const auto rslt =
|
const auto rslt =
|
||||||
convert<std::string>::decode(Node(*this, std::move(pMemory)));
|
convert<std::string>::decode(Node(*this, std::move(pMemory)));
|
||||||
if (rslt.first)
|
return rslt == rhs;
|
||||||
return rslt.second == rhs;
|
|
||||||
return false;
|
|
||||||
} catch (const conversion::DecodeException& e) {
|
} catch (const conversion::DecodeException& e) {
|
||||||
return false;
|
return false;
|
||||||
} catch(...) {
|
} catch(...) {
|
||||||
|
|||||||
@ -99,9 +99,7 @@ struct as_if {
|
|||||||
return fallback;
|
return fallback;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const auto rslt = convert<T>::decode(node);
|
return convert<T>::decode(node);
|
||||||
if (rslt.first)
|
|
||||||
return rslt.second;
|
|
||||||
} catch (conversion::DecodeException& e) {
|
} catch (conversion::DecodeException& e) {
|
||||||
return fallback;
|
return fallback;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
@ -110,6 +108,35 @@ struct as_if {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//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.reset(node);
|
||||||
|
return node;
|
||||||
|
} catch (conversion::DecodeException& e) {
|
||||||
|
return fallback;
|
||||||
|
} catch (...) {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename S>
|
template <typename S>
|
||||||
struct as_if<std::string, S> {
|
struct as_if<std::string, S> {
|
||||||
explicit as_if(const Node& node_) : node(node_) {}
|
explicit as_if(const Node& node_) : node(node_) {}
|
||||||
@ -134,11 +161,7 @@ struct as_if<T, void> {
|
|||||||
throw TypedBadConversion<T>(node.Mark());
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto result = convert<T>::decode(node);
|
return convert<T>::decode(node);
|
||||||
if (result.first)
|
|
||||||
return result.second;
|
|
||||||
else
|
|
||||||
throw TypedBadConversion<T>(node.Mark());
|
|
||||||
} catch(const conversion::DecodeException& e) {
|
} catch(const conversion::DecodeException& e) {
|
||||||
throw TypedBadConversion<T>(node.Mark());
|
throw TypedBadConversion<T>(node.Mark());
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
|
|||||||
@ -38,7 +38,7 @@ bool IsFlexibleCase(const std::string& str) {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
std::pair<bool, bool> convert<bool>::decode(const Node& node) {
|
bool convert<bool>::decode(const Node& node) {
|
||||||
if (!node.IsScalar())
|
if (!node.IsScalar())
|
||||||
throw conversion::DecodeException("");
|
throw conversion::DecodeException("");
|
||||||
|
|
||||||
@ -59,11 +59,11 @@ std::pair<bool, bool> convert<bool>::decode(const Node& node) {
|
|||||||
|
|
||||||
for (const auto& name : names) {
|
for (const auto& name : names) {
|
||||||
if (name.truename == tolower(node.Scalar())) {
|
if (name.truename == tolower(node.Scalar())) {
|
||||||
return std::make_pair(true, true);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.falsename == tolower(node.Scalar())) {
|
if (name.falsename == tolower(node.Scalar())) {
|
||||||
return std::make_pair(true, false);
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user