summaryrefslogtreecommitdiffhomepage
path: root/entity/metadata.hpp
blob: c9b9a1d54e044ba4ab3d4db0ada2f462116462ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#pragma once
#include "name-of.hpp"
#include "accessor.hpp"
#include "constraints.hpp"
#include "util.hpp"
#include "concepts.hpp"
#include "compat/defs.hpp"
#include <cstddef>
#include <concepts>
#include <type_traits>
#include <limits>
#include <utility>
#include <tuple>
#include <array>
#include <compat/function2.hpp>
#include <Corrade/Containers/StringView.h>

namespace floormat::entities::detail {

template<typename F, typename Tuple, std::size_t N>
requires std::invocable<F, decltype(std::get<N>(std::declval<Tuple>()))>
constexpr CORRADE_ALWAYS_INLINE void visit_tuple(F&& fun, Tuple&& tuple)
{
    using Size = std::tuple_size<std::remove_cvref_t<Tuple>>;
    static_assert(N < Size());

    fun(std::get<N>(tuple));
    if constexpr(N+1 < Size())
        visit_tuple<F, Tuple, N+1>(std::forward<F>(fun), std::forward<Tuple>(tuple));
}

template<typename F, typename Tuple, std::size_t N>
requires std::is_invocable_r_v<bool, F, decltype(std::get<N>(std::declval<Tuple>()))>
constexpr CORRADE_ALWAYS_INLINE bool find_in_tuple(F&& fun, Tuple&& tuple)
{
    using Size = std::tuple_size<std::remove_cvref_t<Tuple>>;
    static_assert(N < Size());

    if (fun(std::get<N>(tuple)))
        return true;
    if constexpr(N+1 < Size())
        return find_in_tuple<F, Tuple, N+1>(std::forward<F>(fun), std::forward<Tuple>(tuple));
    return false;
}

template<typename T> struct decay_tuple_;
template<typename... Ts> struct decay_tuple_<std::tuple<Ts...>> { using type = std::tuple<std::decay_t<Ts>...>; };
template<typename T> using decay_tuple = typename decay_tuple_<T>::type;
template<typename T> struct accessors_for_ { using type = decay_tuple<std::decay_t<decltype(T::accessors())>>; };
template<typename T> using accessors_for = typename accessors_for_<T>::type;

template<typename Obj, typename Type, typename Default, std::size_t I, typename... Fs> struct find_reader;

template<typename Obj, typename Type, typename Default, std::size_t I> struct find_reader<Obj, Type, Default, I> {
    using type = Default;
    static constexpr std::size_t index = I;
};

template<typename Obj, typename Type, typename Default, std::size_t I, typename F, typename... Fs>
struct find_reader<Obj, Type, Default, I, F, Fs...> {
    using type = typename find_reader<Obj, Type, Default, I+1, Fs...>::type;
    static constexpr std::size_t index = find_reader<Obj, Type, Default, I+1, Fs...>::index;
};

template<typename Obj, typename Type, typename Default, std::size_t I, typename F, typename... Fs>
requires FieldReader<F, Obj, Type>
struct find_reader<Obj, Type, Default, I, F, Fs...> { using type = F; static constexpr std::size_t index = I; };

} // namespace floormat::entities::detail

namespace floormat::entities {

template<typename... Ts> constexpr auto constantly(const auto& x) noexcept { return [x](const Ts&...) constexpr { return x; }; }

template<typename Obj, typename Type> struct entity_field_base {};

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
struct entity_field : entity_field_base<Obj, Type> {
private:
    static constexpr auto default_predicate = constantly<Obj>(field_status::enabled);
    static constexpr auto default_c_range   = constantly<Obj>(constraints::range<Type>{});
    static constexpr auto default_c_length  = constantly<Obj>(constraints::length{std::size_t(-1)});
    static constexpr auto default_c_group   = constantly<Obj>(StringView{});
    using default_predicate_t = std::decay_t<decltype(default_predicate)>;
    using default_c_range_t   = std::decay_t<decltype(default_c_range)>;
    using default_c_length_t  = std::decay_t<decltype(default_c_length)>;
    using default_c_group_t   = std::decay_t<decltype(default_c_group)>;
    using c_predicate = detail::find_reader<Obj, field_status, default_predicate_t, 0, Ts...>;
    using c_range  = detail::find_reader<Obj, constraints::range<Type>, default_c_range_t, 0, Ts...>;
    using c_length = detail::find_reader<Obj, constraints::length, default_c_length_t, 0, Ts...>;
    using c_group  = detail::find_reader<Obj, constraints::group, default_c_group_t, 0, Ts...>;
    static constexpr std::size_t good_arguments =
        unsigned(c_predicate::index != sizeof...(Ts)) +
        unsigned(c_range::index != sizeof...(Ts)) +
        unsigned(c_length::index != sizeof...(Ts)) +
        unsigned(c_group::index != sizeof...(Ts));
    static_assert(sizeof...(Ts) == good_arguments, "ignored arguments");

public:
    using ObjectType = Obj;
    using FieldType  = Type;
    using Reader     = R;
    using Writer     = W;
    using Predicate  = typename c_predicate::type;
    using Range      = typename c_range::type;
    using Length     = typename c_length::type;
    using Group      = typename c_group::type;

    StringView name;
    [[no_unique_address]] R reader;
    [[no_unique_address]] W writer;
    [[no_unique_address]] Predicate predicate;
    [[no_unique_address]] Range range;
    [[no_unique_address]] Length length;
    [[no_unique_address]] Group group;

    fm_DECLARE_DEFAULT_MOVE_COPY_ASSIGNMENTS(entity_field);

    static constexpr decltype(auto) read(const R& reader, const Obj& x) { return detail::read_field<Obj, Type, R>::read(x, reader); }
    static constexpr void write(const W& writer, Obj& x, move_qualified<Type> v);
    constexpr decltype(auto) read(const Obj& x) const { return read(reader, x); }
    constexpr void write(Obj& x, move_qualified<Type> value) const { write(writer, x, value); }
    static constexpr bool can_write = !std::is_same_v<std::nullptr_t, decltype(entity_field<Obj, Type, R, W, Ts...>::writer)>;

    static constexpr field_status is_enabled(const Predicate & p, const Obj& x);
    constexpr field_status is_enabled(const Obj& x) const { return is_enabled(predicate, x); }

    static constexpr std::pair<Type, Type> get_range(const Range& r, const Obj& x);
    constexpr std::pair<Type, Type> get_range(const Obj& x) const { return get_range(range, x); }
    static constexpr std::size_t get_max_length(const Length& l, const Obj& x);
    constexpr std::size_t get_max_length(const Obj& x) const { return get_max_length(length, x); }
    static constexpr StringView get_group(const Group& g, const Obj& x);
    constexpr StringView get_group(const Obj& x) const { return get_group(group, x); }

    constexpr entity_field(StringView name, R r, W w, Ts&&... ts) noexcept :
        name{name}, reader{r}, writer{w},
        predicate { std::get<c_predicate::index>(std::forward_as_tuple(ts..., default_predicate)) },
        range     { std::get<c_range::index>    (std::forward_as_tuple(ts..., default_c_range))   },
        length    { std::get<c_length::index>   (std::forward_as_tuple(ts..., default_c_length))  },
        group     { std::get<c_group::index>    (std::forward_as_tuple(ts..., default_c_group))   }
    {}
    constexpr erased_accessor erased() const;
};

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
constexpr void entity_field<Obj, Type, R, W, Ts...>::write(const W& writer, Obj& x, move_qualified<Type> v)
{
    static_assert(can_write); detail::write_field<Obj, Type, W>::write(x, writer, v);
}

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
constexpr erased_accessor entity_field<Obj, Type, R, W, Ts...>::erased() const
{
    using reader_t = typename erased_accessor::erased_reader_t;
    using writer_t = typename erased_accessor::erased_writer_t;
    using predicate_t = typename erased_accessor::erased_predicate_t ;
    constexpr auto obj_name = name_of<Obj>, field_name = name_of<Type>;
    using P = Predicate;

    constexpr auto reader_fn = [](const void* obj, const reader_t* reader, void* value) {
        const auto& obj_ = *reinterpret_cast<const Obj*>(obj);
        const auto& reader_ = *reinterpret_cast<const R*>(reader);
        auto& value_ = *reinterpret_cast<Type*>(value);
        value_ = read(reader_, obj_);
    };
    constexpr auto writer_fn = [](void* obj, const writer_t* writer, void* value) {
        auto& obj_ = *reinterpret_cast<Obj*>(obj);
        const auto& writer_ = *reinterpret_cast<const W*>(writer);
        move_qualified<Type> value_ = std::move(*reinterpret_cast<Type*>(value));
        write(writer_, obj_, value_);
    };
    constexpr auto predicate_fn = [](const void* obj, const predicate_t* predicate) {
        const auto& obj_ = *reinterpret_cast<const Obj*>(obj);
        const auto& predicate_ = *reinterpret_cast<const P*>(predicate);
        return is_enabled(predicate_, obj_);
    };
    constexpr auto writer_stub_fn = [](void*, const writer_t*, void*) {
        fm_abort("no writer for this accessor");
    };

    return erased_accessor {
        (void*)&reader, writer ? (void*)&writer : nullptr, (void*)&predicate,
        name, obj_name, field_name,
        reader_fn, writer ? writer_fn : writer_stub_fn, predicate_fn,
    };
}

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
constexpr field_status entity_field<Obj, Type, R, W, Ts...>::is_enabled(const Predicate& p, const Obj& x)
{ return detail::read_field<Obj, field_status, Predicate>::read(x, p); }

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
constexpr std::pair<Type, Type> entity_field<Obj, Type, R, W, Ts...>::get_range(const Range& r, const Obj& x)
{ return detail::read_field<Obj, constraints::range<Type>, Range>::read(x, r); }

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
constexpr std::size_t entity_field<Obj, Type, R, W, Ts...>::get_max_length(const Length& l, const Obj& x)
{ return detail::read_field<Obj, constraints::length, Length>::read(x, l); }

template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
constexpr StringView entity_field<Obj, Type, R, W, Ts...>::get_group(const Group& g, const Obj& x)
{ return detail::read_field<Obj, constraints::group, Group>::read(x, g); }

template<typename Obj>
struct Entity final {
    static_assert(std::is_same_v<Obj, std::decay_t<Obj>>);

    template<typename Type>
    struct type final
    {
        template<FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
        struct field final : entity_field<Obj, Type, R, W, Ts...>
        {
            constexpr field(StringView field_name, R r, W w, Ts&&... ts) noexcept :
                entity_field<Obj, Type, R, W, Ts...>{field_name, r, w, std::forward<Ts>(ts)...}
            {}
        };

        template<FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts>
        field(StringView name, R r, W w, Ts&&... ts) -> field<R, W, Ts...>;
    };
};

template<typename F, typename Tuple>
constexpr void visit_tuple(F&& fun, Tuple&& tuple)
{
    using Size = std::tuple_size<std::decay_t<Tuple>>;
    if constexpr(Size() > 0)
        detail::visit_tuple<F, Tuple, 0>(std::forward<F>(fun), std::forward<Tuple>(tuple));
}

template<typename F, typename Tuple>
constexpr bool find_in_tuple(F&& fun, Tuple&& tuple)
{
    using Size = std::tuple_size<std::decay_t<Tuple>>;
    if constexpr(Size() > 0)
        return detail::find_in_tuple<F, Tuple, 0>(std::forward<F>(fun), std::forward<Tuple>(tuple));
    else
        return false;
}

} // namespace floormat::entities

namespace floormat {

template<typename T>
requires std::is_same_v<T, std::decay_t<T>>
class entity_metadata final {
    template<typename... Ts>
    static consteval auto erased_helper(const std::tuple<Ts...>& tuple)
    {
        std::array<entities::erased_accessor, sizeof...(Ts)> array { std::get<Ts>(tuple).erased()..., };
        return array;
    }
public:
    static constexpr StringView class_name = name_of<T>;
    static constexpr std::size_t size = std::tuple_size_v<entities::detail::accessors_for<T>>;
    static constexpr entities::detail::accessors_for<T> accessors = T::accessors();
    static constexpr auto erased_accessors = erased_helper(accessors);
};

} // namespace floormat