-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathschema.hpp
352 lines (317 loc) · 15.3 KB
/
schema.hpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#ifndef CPPREALM_SCHEMA_HPP
#define CPPREALM_SCHEMA_HPP
#include <cpprealm/link.hpp>
#include <cpprealm/internal/bridge/lnklst.hpp>
#include <cpprealm/internal/bridge/object_schema.hpp>
#include <cpprealm/internal/bridge/realm.hpp>
#include <cpprealm/internal/bridge/table.hpp>
#include <cpprealm/internal/type_info.hpp>
#include <variant>
#include <type_traits>
#include <iostream>
namespace realm {
enum class ObjectType {
None,
TopLevel,
Embedded,
Asymmetric
};
namespace internal {
template <typename T>
struct ptr_type_extractor_base;
template <typename Result, typename Class>
struct ptr_type_extractor_base<Result Class::*>
{
using class_type = Class;
using member_type = Result;
};
template <auto T>
struct ptr_type_extractor : ptr_type_extractor_base<decltype(T)> {
};
template<typename... Ts>
constexpr auto make_subpack_tuple(Ts&&... xs)
{
return std::tuple<Ts...>(std::forward<Ts>(xs)...);
}
template <typename T>
struct persisted_type_extractor {
using Result = T;
};
}
// MARK: schema
namespace schemagen {
/**
* @brief Internal use only, for use with automatic schema discovery.
*
* @param schema Optionally append a new object schema to the
* registered schemas if it does not already exist.
* @return A vector of object schemas
*/
std::vector<internal::bridge::object_schema> registered_schemas(const std::optional<internal::bridge::object_schema>& schema = std::nullopt);
template <auto Ptr, bool IsPrimaryKey = false>
struct property {
using Result = typename internal::persisted_type_extractor<typename internal::ptr_type_extractor<Ptr>::member_type>::Result;
using VariantResult =
std::conditional_t<std::is_pointer_v<Result>, managed<Result>, Result>;
using Class = typename internal::ptr_type_extractor<Ptr>::class_type;
static constexpr auto ptr = Ptr;
static constexpr bool is_primary_key = IsPrimaryKey || internal::type_info::is_primary_key<Result>::value;
internal::bridge::property::type type;
const char* name = "";
constexpr property() : type(internal::type_info::type_info<Result>::type())
{
}
explicit constexpr property(const char* actual_name)
: type(internal::type_info::type_info<Result>::type())
{
name = actual_name;
}
operator internal::bridge::property() const {
internal::bridge::property property(name, type, is_primary_key);
if constexpr (realm::internal::type_info::is_vector<Result>::value) {
if constexpr (std::is_pointer_v<typename Result::value_type>) {
property.set_object_link(managed<std::remove_pointer_t<typename Result::value_type>, void>::schema.name);
}
} else if constexpr (realm::internal::type_info::is_set<Result>::value) {
if constexpr (std::is_pointer_v<typename Result::value_type>) {
property.set_object_link(managed<std::remove_pointer_t<typename Result::value_type>, void>::schema.name);
}
} else if constexpr (realm::internal::type_info::is_map<Result>::value) {
if constexpr (internal::type_info::is_optional<typename Result::mapped_type>::value) {
if constexpr (internal::type_info::is_link<typename Result::mapped_type::value_type>::value) {
property.set_object_link(managed<std::remove_pointer_t<typename Result::mapped_type::value_type>, void>::schema.name);
property.set_type(type | internal::bridge::property::type::Nullable);
}
} else if constexpr (std::is_pointer_v<typename Result::mapped_type>) {
property.set_object_link(managed<std::remove_pointer_t<typename Result::mapped_type>, void>::schema.name);
property.set_type(type | internal::bridge::property::type::Nullable);
}
} else if constexpr (std::is_pointer_v<Result>) {
property.set_object_link(managed<typename std::remove_pointer_t<Result>, void>::schema.name);
property.set_type(type | internal::bridge::property::type::Nullable);
} else if constexpr (internal::type_info::is_backlink<Result>::value) {
return internal::bridge::property{};
}
return property;
}
};
template <typename T, typename ... Types>
struct unique_variant {
using type = T;
};
template <template<typename...> typename Variant, typename... VariantTypes, typename NextType, typename... RemainingTypes>
struct unique_variant<Variant<VariantTypes...> , NextType, RemainingTypes...>
: std::conditional<
std::disjunction< std::is_same<NextType, VariantTypes> ... >::value
, unique_variant<Variant<VariantTypes...>, RemainingTypes...>
, unique_variant<Variant<VariantTypes..., NextType>, RemainingTypes...>
>::type
{};
template <typename Class, typename ...Properties>
struct schema {
const char *name;
const char *names[sizeof...(Properties)] = {};
const char *primary_key_name = "";
static constexpr std::tuple<Properties...> properties{};
using variant_t = typename unique_variant<std::variant<>, std::monostate, typename Properties::VariantResult...>::type;
template<size_t N>
constexpr auto do_apply_name(const std::tuple<Properties...> &tup) {
if constexpr (N + 1 == sizeof...(Properties)) {
names[N] = std::get<N>(tup).name;
if (std::get<N>(tup).is_primary_key) {
primary_key_name = std::get<N>(tup).name;
}
return;
} else {
names[N] = std::get<N>(tup).name;
if (std::get<N>(tup).is_primary_key) {
primary_key_name = std::get<N>(tup).name;
}
return do_apply_name<N + 1>(tup);
}
}
constexpr auto apply_name(const std::tuple<Properties...> &tup) {
return do_apply_name<0>(tup);
}
std::tuple<Properties...> ps;
explicit constexpr schema(const char *name_, Properties &&... props)
: name(name_)
, ps(props...) {
auto tup = std::make_tuple(props...);
apply_name(tup);
}
explicit constexpr schema(const char *name_, std::tuple<Properties...>&& props)
: name(name_)
, ps(props) {
apply_name(props);
}
explicit constexpr schema(const char *name_, ObjectType object_type, std::tuple<Properties...>&& props)
: name(name_)
, ps(props), m_object_type(object_type) {
apply_name(props);
}
template<size_t N, typename P>
static constexpr auto primary_key(P &) {
if constexpr (P::is_primary_key) {
return P();
} else {
if constexpr (N + 1 == sizeof...(Properties)) {
return;
} else {
return primary_key<N + 1>(std::get<N + 1>(properties));
}
}
}
static constexpr auto primary_key() {
return primary_key<0>(std::get<0>(properties));
}
using PrimaryKeyProperty = decltype(primary_key());
static constexpr bool HasPrimaryKeyProperty = !std::is_void_v<PrimaryKeyProperty>;
bool is_embedded() const {
return m_object_type == ObjectType::Embedded;
}
[[nodiscard]] internal::bridge::object_schema to_core_schema() const {
internal::bridge::object_schema schema;
schema.set_name(name);
auto add_property = [&](const internal::bridge::property &p) {
if (!p.name().empty()) {
schema.add_property(p);
}
};
std::apply([&](const auto&... p) {
(add_property(p), ...);
}, ps);
if constexpr (HasPrimaryKeyProperty) {
schema.set_primary_key(primary_key_name);
}
if (m_object_type == ObjectType::Embedded) {
schema.set_object_type(internal::bridge::object_schema::object_type::Embedded);
}
if (m_object_type == ObjectType::Asymmetric) {
schema.set_object_type(internal::bridge::object_schema::object_type::TopLevelAsymmetric);
}
return schema;
}
template<size_t N, typename P>
constexpr auto set(Class &object, P &property) const {
if constexpr (N + 1 == sizeof...(Properties)) {
property.set(object, names[N]);
return;
} else {
property.set(object, names[N]);
return set<N + 1>(object, std::get<N + 1>(properties));
}
}
template<size_t N, typename P>
constexpr variant_t
property_value_for_name(std::string_view property_name, const managed<Class, void> &cls, P &property, bool excluding_collections = true) const {
bool is_array = realm::internal::bridge::property_has_flag(property.type, realm::internal::bridge::property::type::Array);
bool is_dictionary = realm::internal::bridge::property_has_flag(property.type, realm::internal::bridge::property::type::Dictionary);
bool is_set = realm::internal::bridge::property_has_flag(property.type, realm::internal::bridge::property::type::Set);
bool is_collection = is_array || is_dictionary || is_set;
if (excluding_collections && is_collection) {
return variant_t{std::monostate()};
}
if constexpr (N + 1 == sizeof...(Properties)) {
if (property_name == std::string_view(names[N])) {
auto ptr = managed<Class, void>::template unmanaged_to_managed_pointer(property.ptr);
if constexpr (std::is_pointer_v<typename P::Result>) {
return (cls.*ptr);
} else {
return (cls.*ptr).detach();
}
}
return variant_t{};
} else {
if (property_name == std::string_view(names[N])) {
auto ptr = managed<Class, void>::template unmanaged_to_managed_pointer(property.ptr);
if constexpr (std::is_pointer_v<typename P::Result>) {
return (cls.*ptr);
} else {
return (cls.*ptr).detach();
}
}
return property_value_for_name<N + 1>(property_name, cls, std::get<N + 1>(properties), excluding_collections);
}
}
constexpr auto property_value_for_name(std::string_view property_name, const managed<Class, void> &cls, bool excluding_collections = true) const {
return property_value_for_name<0>(property_name, cls, std::get<0>(properties), excluding_collections);
}
template<size_t N, typename T, typename P>
constexpr const char*
name_for_property(T ptr, P &property) const {
if constexpr (N + 1 == sizeof...(Properties)) {
if constexpr (std::is_same_v<decltype(ptr), std::remove_const_t<decltype(property.ptr)>>) {
if (ptr == property.ptr) {
return property.name;
}
}
return "";
} else {
if constexpr (std::is_same_v<decltype(ptr), std::remove_const_t<decltype(property.ptr)>>) {
if (ptr == property.ptr) {
return property.name;
}
}
return name_for_property<N + 1>(ptr, std::get<N + 1>(ps));
}
}
template <auto ptr>
constexpr const char* name_for_property() const {
return name_for_property<0>(ptr, std::get<0>(ps));
}
template <typename T>
constexpr const char* name_for_property(T ptr) const {
return name_for_property<0>(ptr, std::get<0>(ps));
}
private:
ObjectType m_object_type = ObjectType::None;
};
}
template <auto Ptr, bool IsPrimaryKey = false>
static constexpr auto property(const char* name)
{
return schemagen::property<Ptr, IsPrimaryKey>(name);
}
template <typename ...T>
static constexpr auto schema(const char * name,
T&&... props) {
auto tup = internal::make_subpack_tuple(props...);
auto i = std::get<0>(tup);
using Cls = typename decltype(i)::Class;
return schemagen::schema<Cls, T...>(name, std::move(props)...);
}
template <typename ...T>
static constexpr auto schema(const char * name,
std::tuple<T...>&& props) {
auto i = std::get<0>(props);
using Cls = typename decltype(i)::Class;
return schemagen::schema<Cls, T...>(name, std::move(props));
}
template <typename ...T>
static constexpr auto schema(const char * name,
ObjectType object_type,
std::tuple<T...>&& props) {
auto i = std::get<0>(props);
using Cls = typename decltype(i)::Class;
return schemagen::schema<Cls, T...>(name, object_type, std::move(props));
}
}
#endif /* CPPREALM_SCHEMA_HPP */