Support conversion for std::valarray (#956)

This commit is contained in:
sfalmo
2022-09-20 07:31:51 +02:00
committed by GitHub
parent 97ebcf035a
commit 4ae4cb7309
2 changed files with 42 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
#include <unordered_map>
#include <sstream>
#include <type_traits>
#include <valarray>
#include <vector>
#include "yaml-cpp/binary.h"
@@ -363,6 +364,37 @@ struct convert<std::array<T, N>> {
}
};
// std::valarray
template <typename T>
struct convert<std::valarray<T>> {
static Node encode(const std::valarray<T>& rhs) {
Node node(NodeType::Sequence);
for (const auto& element : rhs) {
node.push_back(element);
}
return node;
}
static bool decode(const Node& node, std::valarray<T>& rhs) {
if (!node.IsSequence()) {
return false;
}
rhs.resize(node.size());
for (auto i = 0u; i < node.size(); ++i) {
#if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3:
rhs[i] = node[i].template as<T>();
#else
rhs[i] = node[i].as<T>();
#endif
}
return true;
}
};
// std::pair
template <typename T, typename U>
struct convert<std::pair<T, U>> {