improve an interface of nf7::Value

This commit is contained in:
falsycat 2022-11-02 22:44:38 +09:00
parent 3b25790f3c
commit 5e515e23fa
2 changed files with 30 additions and 20 deletions

View File

@ -109,6 +109,7 @@ class Value {
bool isTuple() const noexcept { return std::holds_alternative<ConstTuple>(value_); }
bool isData() const noexcept { return std::holds_alternative<DataPtr>(value_); }
// direct accessors
Integer integer() const { return get<Integer>(); }
Boolean boolean() const { return get<Boolean>(); }
Scalar scalar() const { return get<Scalar>(); }
@ -117,6 +118,13 @@ class Value {
const ConstTuple& tuple() const { return get<ConstTuple>(); }
const DataPtr& data() const { return get<DataPtr>(); }
// direct reference accessor
Integer& integer() { return get<Integer>(); }
Boolean& boolean() { return get<Boolean>(); }
Scalar& scalar() { return get<Scalar>(); }
String& string() { return get<String>(); }
// conversion accessor
template <typename N>
N integer() const {
return SafeCast<N>(integer());
@ -141,7 +149,13 @@ class Value {
return SafeCast<N>(integer());
}
}
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");
}
// tuple element accessor
const Value& tuple(size_t idx) const {
auto& tup = *tuple();
return idx < tup.size()? tup[idx].second:
@ -161,20 +175,18 @@ class Value {
return v;
}
}
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");
// extended accessor
nf7::File& file(const nf7::File& base) const {
if (isInteger()) {
return base.env().GetFileOrThrow(integerOrScalar<nf7::File::Id>());
} else if (isString()) {
return base.ResolveOrThrow(string());
} else {
throw IncompatibleException {"expected file id or file path"};
}
}
Integer& integer() { return get<Integer>(); }
Boolean& boolean() { return get<Boolean>(); }
Scalar& scalar() { return get<Scalar>(); }
String& string() { return get<String>(); }
Vector vectorUniq() { return getUniq<ConstVector>(); }
Tuple tupleUniq() { return getUniq<ConstTuple>(); }
const char* typeName() const noexcept {
struct Visitor final {
public:

View File

@ -511,8 +511,8 @@ struct Texture {
const auto dim = gl::GetDimension(target_);
for (size_t i = 0; i < dim; ++i) {
offset[i] = v.tupleOr(kOffsetNames[i], nf7::Value::Integer {0}).integer<uint32_t>();
size[i] = v.tuple(kSizeNames[i]).integer<uint32_t>();
offset[i] = v.tupleOr(kOffsetNames[i], nf7::Value::Integer {0}).integerOrScalar<uint32_t>();
size[i] = v.tuple(kSizeNames[i]).integerOrScalar<uint32_t>();
if (size[i] == 0) {
return false;
}
@ -886,8 +886,8 @@ struct Program {
if (p.in.name == "draw") {
const auto mode = gl::ToEnum<gl::DrawMode>(v.tuple("mode").string());
const auto count = v.tuple("count").integer<GLsizei>();
const auto inst = v.tupleOr("instance", nf7::Value::Integer{1}).integer<GLsizei>();
const auto count = v.tuple("count").integerOrScalar<GLsizei>();
const auto inst = v.tupleOr("instance", nf7::Value::Integer{1}).integerOrScalar<GLsizei>();
const auto uni = v.tupleOr("uniform", nf7::Value::Tuple {}).tuple();
const auto tex = v.tupleOr("texture", nf7::Value::Tuple {}).tuple();
@ -914,8 +914,7 @@ struct Program {
std::optional<nf7::gl::FramebufferFactory::Product> fbo_fu;
std::optional<nf7::gl::Framebuffer::Meta::LockedAttachmentsFuture> fbo_lock_fu;
{
fbo_fu = base.
ResolveOrThrow(v.tuple("fbo").string()).
fbo_fu = v.tuple("fbo").file(base).
interfaceOrThrow<nf7::gl::FramebufferFactory>().Create();
nf7::gl::Framebuffer::Meta::LockedAttachmentsFuture::Promise fbo_lock_pro;
@ -931,8 +930,7 @@ struct Program {
std::optional<nf7::gl::VertexArrayFactory::Product> vao_fu;
std::optional<nf7::gl::VertexArray::Meta::LockedBuffersFuture> vao_lock_fu;
{
vao_fu = base.
ResolveOrThrow(v.tuple("vao").string()).
vao_fu = v.tuple("vao").file(base).
interfaceOrThrow<nf7::gl::VertexArrayFactory>().Create();
nf7::gl::VertexArray::Meta::ValidationHint vhint;