add Value::Vector
This commit is contained in:
parent
35c88900ac
commit
162514dc1d
@ -5,6 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <yas/serialize.hpp>
|
#include <yas/serialize.hpp>
|
||||||
#include <yas/types/std/string.hpp>
|
#include <yas/types/std/string.hpp>
|
||||||
@ -33,7 +34,8 @@ class Value {
|
|||||||
using Integer = int64_t;
|
using Integer = int64_t;
|
||||||
using Scalar = double;
|
using Scalar = double;
|
||||||
using String = std::string;
|
using String = std::string;
|
||||||
using DataPtr = std::shared_ptr<Data>;
|
using Vector = std::shared_ptr<const std::vector<uint8_t>>;
|
||||||
|
using DataPtr = std::shared_ptr<const Data>;
|
||||||
|
|
||||||
Value() noexcept {
|
Value() noexcept {
|
||||||
}
|
}
|
||||||
@ -54,6 +56,10 @@ class Value {
|
|||||||
Value& operator=(std::string_view v) noexcept { value_ = std::string(v); return *this; }
|
Value& operator=(std::string_view v) noexcept { value_ = std::string(v); return *this; }
|
||||||
Value(String&& v) noexcept : value_(std::move(v)) { }
|
Value(String&& v) noexcept : value_(std::move(v)) { }
|
||||||
Value& operator=(String&& v) noexcept { value_ = std::move(v); return *this; }
|
Value& operator=(String&& v) noexcept { value_ = std::move(v); return *this; }
|
||||||
|
Value(const Vector& v) noexcept { value_ = v; }
|
||||||
|
Value& operator=(const Vector& v) noexcept { value_ = v; return *this; }
|
||||||
|
Value(Vector&& v) noexcept { value_ = std::move(v); }
|
||||||
|
Value& operator=(Vector&& v) noexcept { value_ = std::move(v); return *this; }
|
||||||
Value(const DataPtr& v) noexcept : value_(v) { }
|
Value(const DataPtr& v) noexcept : value_(v) { }
|
||||||
Value& operator=(const DataPtr& v) noexcept { value_ = v; return *this; }
|
Value& operator=(const DataPtr& v) noexcept { value_ = v; return *this; }
|
||||||
Value(DataPtr&& v) noexcept : value_(std::move(v)) { }
|
Value(DataPtr&& v) noexcept : value_(std::move(v)) { }
|
||||||
@ -64,19 +70,26 @@ class Value {
|
|||||||
bool isInteger() const noexcept { return std::holds_alternative<Integer>(value_); }
|
bool isInteger() const noexcept { return std::holds_alternative<Integer>(value_); }
|
||||||
bool isScalar() const noexcept { return std::holds_alternative<Scalar>(value_); }
|
bool isScalar() const noexcept { return std::holds_alternative<Scalar>(value_); }
|
||||||
bool isString() const noexcept { return std::holds_alternative<String>(value_); }
|
bool isString() const noexcept { return std::holds_alternative<String>(value_); }
|
||||||
|
bool isVector() const noexcept { return std::holds_alternative<Vector>(value_); }
|
||||||
bool isData() const noexcept { return std::holds_alternative<DataPtr>(value_); }
|
bool isData() const noexcept { return std::holds_alternative<DataPtr>(value_); }
|
||||||
|
|
||||||
Integer integer() const { return get<Integer>(); }
|
Integer integer() const { return get<Integer>(); }
|
||||||
Boolean boolean() const { return get<Boolean>(); }
|
Boolean boolean() const { return get<Boolean>(); }
|
||||||
Scalar scalar() const { return get<Scalar>(); }
|
Scalar scalar() const { return get<Scalar>(); }
|
||||||
const String& string() const { return get<String>(); }
|
const String& string() const { return get<String>(); }
|
||||||
|
const Vector& vector() const { return get<Vector>(); }
|
||||||
const DataPtr& data() const { return get<DataPtr>(); }
|
const DataPtr& data() const { return get<DataPtr>(); }
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::shared_ptr<T> data() const {
|
||||||
|
if (auto ptr = std::dynamic_pointer_cast<T>(data())) return ptr;
|
||||||
|
throw IncompatibleException("data pointer downcast failure");
|
||||||
|
}
|
||||||
|
|
||||||
Integer& integer() { return get<Integer>(); }
|
Integer& integer() { return get<Integer>(); }
|
||||||
Boolean& boolean() { return get<Boolean>(); }
|
Boolean& boolean() { return get<Boolean>(); }
|
||||||
Scalar& scalar() { return get<Scalar>(); }
|
Scalar& scalar() { return get<Scalar>(); }
|
||||||
String& string() { return get<String>(); }
|
String& string() { return get<String>(); }
|
||||||
DataPtr& data() { return get<DataPtr>(); }
|
|
||||||
|
|
||||||
const char* typeName() const noexcept {
|
const char* typeName() const noexcept {
|
||||||
struct Visitor final {
|
struct Visitor final {
|
||||||
@ -86,6 +99,7 @@ class Value {
|
|||||||
auto operator()(Integer) noexcept { return "integer"; }
|
auto operator()(Integer) noexcept { return "integer"; }
|
||||||
auto operator()(Scalar) noexcept { return "scalar"; }
|
auto operator()(Scalar) noexcept { return "scalar"; }
|
||||||
auto operator()(String) noexcept { return "string"; }
|
auto operator()(String) noexcept { return "string"; }
|
||||||
|
auto operator()(Vector) noexcept { return "vector"; }
|
||||||
auto operator()(DataPtr) noexcept { return "data"; }
|
auto operator()(DataPtr) noexcept { return "data"; }
|
||||||
};
|
};
|
||||||
return std::visit(Visitor{}, value_);
|
return std::visit(Visitor{}, value_);
|
||||||
@ -98,7 +112,7 @@ class Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::variant<Pulse, Boolean, Integer, Scalar, String, DataPtr> value_;
|
std::variant<Pulse, Boolean, Integer, Scalar, String, Vector, DataPtr> value_;
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -151,6 +165,23 @@ struct serializer<
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <size_t F>
|
||||||
|
struct serializer<
|
||||||
|
type_prop::not_a_fundamental,
|
||||||
|
ser_case::use_internal_serializer,
|
||||||
|
F,
|
||||||
|
nf7::Value::Vector> {
|
||||||
|
public:
|
||||||
|
template <typename Archive>
|
||||||
|
static Archive& save(Archive&, const nf7::Value::Vector&) {
|
||||||
|
throw nf7::Exception("cannot serialize Value::Vector");
|
||||||
|
}
|
||||||
|
template <typename Archive>
|
||||||
|
static Archive& load(Archive&, nf7::Value::Vector&) {
|
||||||
|
throw nf7::DeserializeException("cannot deserialize Value::Vector");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <size_t F>
|
template <size_t F>
|
||||||
struct serializer<
|
struct serializer<
|
||||||
type_prop::not_a_fundamental,
|
type_prop::not_a_fundamental,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user