node/convert: support conversion for std::string_view (#1148)

This commit is contained in:
Kefu Chai
2023-07-25 03:51:05 +08:00
committed by GitHub
parent b8882652fc
commit 35b4498026
2 changed files with 27 additions and 0 deletions

View File

@@ -18,6 +18,10 @@
#include <valarray>
#include <vector>
#if __cplusplus >= 201703L
#include <string_view>
#endif
#include "yaml-cpp/binary.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/node/iterator.h"
@@ -89,6 +93,20 @@ struct convert<char[N]> {
static Node encode(const char* rhs) { return Node(rhs); }
};
#if __cplusplus >= 201703L
template <>
struct convert<std::string_view> {
static Node encode(std::string_view rhs) { return Node(std::string(rhs)); }
static bool decode(const Node& node, std::string_view& rhs) {
if (!node.IsScalar())
return false;
rhs = node.Scalar();
return true;
}
};
#endif
template <>
struct convert<_Null> {
static Node encode(const _Null& /* rhs */) { return Node(); }