diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-02-21 04:18:06 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-02-21 04:18:06 +0100 |
commit | eabcbed30df8ff5fdb63b5a4f844d01f436f9a2d (patch) | |
tree | 02e4bb4bad1381e61402c68592fb5cff3f53dfbf | |
parent | 91467e77de6ac3acff4326f4cd9d5a4390ccc3ce (diff) |
wip inspector stuff
-rw-r--r-- | editor/inspect.cpp | 24 | ||||
-rw-r--r-- | editor/inspect.hpp | 9 | ||||
-rw-r--r-- | entity/accessor.hpp | 46 | ||||
-rw-r--r-- | entity/erased-constraints.hpp | 3 | ||||
-rw-r--r-- | test/entity.cpp | 16 |
5 files changed, 68 insertions, 30 deletions
diff --git a/editor/inspect.cpp b/editor/inspect.cpp new file mode 100644 index 00000000..c1f0840e --- /dev/null +++ b/editor/inspect.cpp @@ -0,0 +1,24 @@ +#include "inspect.hpp" +#include "entity/accessor.hpp" +#include <imgui.h> + +namespace floormat { + +using namespace entities; + +template<typename T> +static void do_inspect_field(const void* datum, const entities::erased_accessor& accessor) +{ + auto range = accessor.get_range(datum); + auto enabled = accessor.is_enabled(datum); + auto [min, max] = range.convert<T>(); + + +} + +template<> void inspect_field<int>(const void* datum, const entities::erased_accessor& accessor) +{ + do_inspect_field<int>(datum, accessor); +} + +} // namespace floormat diff --git a/editor/inspect.hpp b/editor/inspect.hpp new file mode 100644 index 00000000..feca67d1 --- /dev/null +++ b/editor/inspect.hpp @@ -0,0 +1,9 @@ +#pragma once +//#include "entity/accessor.hpp" +namespace floormat::entities { struct erased_accessor; } + +namespace floormat { + +template<typename T> void inspect_field(const void* datum, const entities::erased_accessor& accessor); + +} // namespace floormat diff --git a/entity/accessor.hpp b/entity/accessor.hpp index 5e6a60ce..9836b55f 100644 --- a/entity/accessor.hpp +++ b/entity/accessor.hpp @@ -1,6 +1,7 @@ #pragma once #include "util.hpp" #include "erased-constraints.hpp" +#include "name-of.hpp" #include <type_traits> #include <utility> #include <Corrade/Containers/StringView.h> @@ -27,21 +28,22 @@ struct erased_accessor final { using Object = void; using Value = void; - const reader_t* reader; - const writer_t* writer; - const predicate_t* predicate; - const c_range_t* range; - const c_length_t* length; - const c_group_t* group; + const reader_t* reader = nullptr; + const writer_t* writer = nullptr; + const predicate_t* predicate = nullptr; + const c_range_t* range = nullptr; + const c_length_t* length = nullptr; + const c_group_t* group = nullptr; StringView field_name, object_type, field_type; - void(*read_fun)(const Object*, const reader_t*, Value*); - void(*write_fun)(Object*, const writer_t*, Value*); - field_status(*predicate_fun)(const Object*, const predicate_t*); - erased_constraints::range(*range_fun)(const Object*, const c_range_t*); - erased_constraints::max_length(*length_fun)(const Object*, const c_length_t*); - erased_constraints::group(*group_fun)(const Object*, const c_group_t*); - + void(*read_fun)(const Object*, const reader_t*, Value*) = nullptr; + void(*write_fun)(Object*, const writer_t*, Value*) = nullptr; + field_status(*predicate_fun)(const Object*, const predicate_t*) = nullptr; + erased_constraints::range(*range_fun)(const Object*, const c_range_t*) = nullptr; + erased_constraints::max_length(*length_fun)(const Object*, const c_length_t*) = nullptr; + erased_constraints::group(*group_fun)(const Object*, const c_group_t*) = nullptr; + + explicit constexpr erased_accessor() noexcept = default; constexpr erased_accessor(const erased_accessor&) = default; constexpr erased_accessor(const reader_t* reader, const writer_t* writer, const predicate_t* predicate, const c_range_t* range, const c_length_t* length, const c_group_t* group, @@ -76,11 +78,11 @@ struct erased_accessor final { template<typename Obj, typename FieldType> void read(const Obj& x, FieldType& value) const noexcept; template<typename Obj, typename FieldType> void write(Obj& x, move_qualified<FieldType> value) const noexcept; - template<typename Obj> field_status is_enabled(const Obj& x) const noexcept; + field_status is_enabled(const void* x) const noexcept; constexpr bool can_write() const noexcept { return writer != nullptr; } - template<typename Obj> erased_constraints::range get_range(const Obj& x) const noexcept; - template<typename Obj> erased_constraints::max_length get_max_length(const Obj& x) const noexcept; - template<typename Obj> erased_constraints::group get_group(const Obj& x) const noexcept; + inline erased_constraints::range get_range(const void* x) const noexcept; + inline erased_constraints::max_length get_max_length(const void* x) const noexcept; + inline erased_constraints::group get_group(const void* x) const noexcept; }; template<typename T, typename FieldType> @@ -158,15 +160,13 @@ void erased_accessor::write(Obj& x, move_qualified<FieldType> value) const noexc write_unchecked<Obj, FieldType>(x, value); } -template<typename Obj> -field_status erased_accessor::is_enabled(const Obj& x) const noexcept +field_status erased_accessor::is_enabled(const void* x) const noexcept { - do_asserts<Obj>(); return predicate_fun(&x, predicate); } -template<typename T> erased_constraints::range erased_accessor::get_range(const T& x) const noexcept { do_asserts<T>();return range_fun(&x,range); } -template<typename T> erased_constraints::max_length erased_accessor::get_max_length(const T& x) const noexcept { do_asserts<T>();return length_fun(&x,length); } -template<typename T> erased_constraints::group erased_accessor::get_group(const T& x) const noexcept { do_asserts<T>();return group_fun(&x, group); } +erased_constraints::range erased_accessor::get_range(const void* x) const noexcept { return range_fun(x,range); } +erased_constraints::max_length erased_accessor::get_max_length(const void* x) const noexcept { return length_fun(x,length); } +erased_constraints::group erased_accessor::get_group(const void* x) const noexcept { return group_fun(x, group); } } // namespace floormat::entities diff --git a/entity/erased-constraints.hpp b/entity/erased-constraints.hpp index 7daec237..46829ebb 100644 --- a/entity/erased-constraints.hpp +++ b/entity/erased-constraints.hpp @@ -2,6 +2,7 @@ #include <cstddef> #include <cmath> #include <limits> +#include <Corrade/Containers/StringView.h> namespace floormat::entities::erased_constraints { @@ -40,7 +41,7 @@ template<typename T> constexpr std::pair<T, T> range::convert() const switch (type) { case type_float: - if constexpr (limits::is_integer()) + if constexpr (limits::is_integer) return { T(std::floor(min.f)), T(std::ceil(max.f)) }; else return { T(min.f), T(max.f) }; diff --git a/test/entity.cpp b/test/entity.cpp index ce7af58b..a7810863 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -168,14 +168,17 @@ void test_predicate() constexpr auto foo = entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo, [](const TestAccessors&) { return field_status::hidden; }}; static_assert(foo.is_enabled(x) == field_status::hidden); - fm_assert(foo.erased().is_enabled(x) == field_status::hidden); + fm_assert(foo.erased().is_enabled(&x) == field_status::hidden); + + foo.erased().do_asserts<TestAccessors>(); + constexpr auto foo2 = entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo, [](const TestAccessors&) { return field_status::readonly; }}; static_assert(foo2.is_enabled(x) == field_status::readonly); - fm_assert(foo2.erased().is_enabled(x) == field_status::readonly); + fm_assert(foo2.erased().is_enabled(&x) == field_status::readonly); constexpr auto foo3 = entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo}; static_assert(foo3.is_enabled(x) == field_status::enabled); - fm_assert(foo3.erased().is_enabled(x) == field_status::enabled); + fm_assert(foo3.erased().is_enabled(&x) == field_status::enabled); } constexpr bool test_names() @@ -232,9 +235,10 @@ void test_erased_constraints() static constexpr auto erased = foo.erased(); const auto x = TestAccessors{}; - fm_assert(erased.get_range(x) == constraints::range<int>{37, 42}); - fm_assert(erased.get_max_length(x) == 42); - fm_assert(erased.get_group(x) == "foo"_s); + erased.do_asserts<TestAccessors>(); + fm_assert(erased.get_range(&x) == constraints::range<int>{37, 42}); + fm_assert(erased.get_max_length(&x) == 42); + fm_assert(erased.get_group(&x) == "foo"_s); } } // namespace |