Realm C++ SDK Version v2.2.0

managed_primary_key.hpp

1
2//
3// Copyright 2024 Realm Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19#ifndef CPPREALM_MANAGED_PRIMARY_KEY_HPP
20#define CPPREALM_MANAGED_PRIMARY_KEY_HPP
21
22#include <cpprealm/macros.hpp>
23#include <cpprealm/rbool.hpp>
24
25namespace realm {
26 template <typename, typename>
27 struct managed;
28
29 template<typename T>
30 struct primary_key {
31 primary_key() = default;
32 primary_key(const T& v) {
33 this->value = v;
34 }
35 T value;
36 using internal_type = T;
37 operator T() const {
38 return value;
39 }
40 };
41
42 template<>
43 struct primary_key<int64_t> {
44 using internal_type = int64_t;
45 primary_key() = default;
46 primary_key(const int64_t& v) {
47 this->value = v;
48 }
49 primary_key(const int& v) {
50 this->value = v;
51 }
52 int64_t value;
53 operator int64_t() const {
54 return value;
55 }
56 };
57
58 template<>
61 primary_key() = default;
62 primary_key(const object_id& v) {
63 this->value = v;
64 }
65 object_id value;
66 operator object_id() const {
67 return value;
68 }
69 };
70
71 template<>
72 struct primary_key<std::string> {
73 using internal_type = std::string;
74 primary_key() = default;
75 primary_key(const std::string& v) {
76 this->value = v;
77 }
78 primary_key(const char* v) {
79 this->value = v;
80 }
81 std::string value;
82 operator std::string() const {
83 return value;
84 }
85 };
86
87 template<>
88 struct primary_key<uuid> {
90 primary_key() = default;
91 primary_key(const uuid& v) {
92 this->value = v;
93 }
94 uuid value;
95 operator uuid() const {
96 return value;
97 }
98 };
99
100 template<>
101 struct primary_key<std::optional<int64_t>> {
102 using internal_type = std::optional<int64_t>;
103 primary_key() = default;
104 primary_key(const std::nullopt_t& v) {
105 this->value = v;
106 }
107 primary_key(const std::optional<int64_t>& v) {
108 this->value = v;
109 }
110 primary_key(const std::optional<int>& v) {
111 this->value = v;
112 }
113 primary_key(const int64_t& v) {
114 this->value = v;
115 }
116 primary_key(const int& v) {
117 this->value = v;
118 }
119 std::optional<int64_t> value;
120 operator std::optional<int64_t>() const {
121 return value;
122 }
123 };
124
125 template<>
126 struct primary_key<std::optional<object_id>> {
127 using internal_type = std::optional<internal::bridge::object_id>;
128 primary_key() = default;
129 primary_key(const std::nullopt_t& v) {
130 this->value = v;
131 }
132 primary_key(const std::optional<object_id>& v) {
133 this->value = v;
134 }
135 primary_key(const object_id& v) {
136 this->value = v;
137 }
138 std::optional<object_id> value;
139 operator std::optional<object_id>() const {
140 return value;
141 }
142 };
143
144 template<>
145 struct primary_key<std::optional<std::string>> {
146 using internal_type = std::optional<std::string>;
147 primary_key() = default;
148 primary_key(const std::nullopt_t& v) {
149 this->value = v;
150 }
151 primary_key(const std::optional<std::string>& v) {
152 this->value = v;
153 }
154 primary_key(const std::string& v) {
155 this->value = v;
156 }
157 primary_key(const char* v) {
158 this->value = v;
159 }
160 std::optional<std::string> value;
161 operator std::optional<std::string>() const {
162 return value;
163 }
164 };
165
166 template<>
167 struct primary_key<std::optional<uuid>> {
168 using internal_type = std::optional<internal::bridge::uuid>;
169 primary_key() = default;
170 primary_key(const std::nullopt_t& v) {
171 this->value = v;
172 }
173 primary_key(const std::optional<uuid>& v) {
174 this->value = v;
175 }
176 primary_key(const uuid& v) {
177 this->value = v;
178 }
179 std::optional<uuid> value;
180 operator std::optional<uuid>() const {
181 return value;
182 }
183 };
184
185 template<>
186 struct managed<primary_key<int64_t>> final : managed_base {
187 primary_key<int64_t> detach() const {
188 return operator int64_t();
189 }
190
191 operator int64_t() const {
192 return m_obj->template get<int64_t>(m_key);
193 }
194
195 rbool operator==(const int64_t& rhs) const noexcept;
196 rbool operator!=(const int64_t& rhs) const noexcept;
197 rbool operator>(const int64_t& rhs) const noexcept;
198 rbool operator>=(const int64_t& rhs) const noexcept;
199 rbool operator<(const int64_t& rhs) const noexcept;
200 rbool operator<=(const int64_t& rhs) const noexcept;
201 rbool operator==(const int& rhs) const noexcept;
202 rbool operator!=(const int& rhs) const noexcept;
203 rbool operator>(const int& rhs) const noexcept;
204 rbool operator>=(const int& rhs) const noexcept;
205 rbool operator<(const int& rhs) const noexcept;
206 rbool operator<=(const int& rhs) const noexcept;
207
208 private:
209 managed() = default;
210 managed(const managed&) = delete;
211 managed(managed &&) = delete;
212 managed& operator=(const managed&) = delete;
213 managed& operator=(managed&&) = delete;
214 template<typename, typename>
215 friend struct managed;
216 };
217
218 template<>
219 struct managed<primary_key<std::string>> final : managed_base {
220 primary_key<std::string> detach() const {
221 return operator std::string();
222 }
223
224 operator std::string() const {
225 return m_obj->template get<std::string>(m_key);
226 }
227
228 rbool operator==(const std::string& rhs) const noexcept;
229 rbool operator!=(const std::string& rhs) const noexcept;
230 rbool operator==(const char* rhs) const noexcept;
231 rbool operator!=(const char* rhs) const noexcept;
232
233 private:
234 managed() = default;
235 managed(const managed&) = default;
236 managed(managed &&) = delete;
237 managed& operator=(const managed&) = delete;
238 managed& operator=(managed&&) = delete;
239 template<typename, typename>
240 friend struct managed;
241 };
242
243 template<>
245 primary_key<realm::uuid> detach() const {
246 return operator realm::uuid();
247 }
248
249 operator realm::uuid() const {
250 return m_obj->template get<internal::bridge::uuid>(m_key).operator ::realm::uuid();
251 }
252
253 rbool operator==(const realm::uuid& rhs) const noexcept;
254 rbool operator!=(const realm::uuid& rhs) const noexcept;
255
256 private:
257 managed() = default;
258 managed(const managed&) = delete;
259 managed(managed &&) = delete;
260 managed& operator=(const managed&) = delete;
261 managed& operator=(managed&&) = delete;
262 template<typename, typename>
263 friend struct managed;
264 };
265
266 template<>
267 struct managed<primary_key<realm::object_id>> final : managed_base {
268 primary_key<realm::object_id> detach() const {
269 return operator realm::object_id();
270 }
271
272 operator realm::object_id() const {
273 return m_obj->template get<internal::bridge::object_id>(m_key).operator ::realm::object_id();
274 }
275
276 rbool operator==(const realm::object_id& rhs) const noexcept;
277 rbool operator!=(const realm::object_id& rhs) const noexcept;
278
279 private:
280 managed() = default;
281 managed(const managed&) = delete;
282 managed(managed &&) = delete;
283 managed& operator=(const managed&) = delete;
284 managed& operator=(managed&&) = delete;
285 template<typename, typename>
286 friend struct managed;
287 };
288
289 template<typename T>
290 struct managed<primary_key<T>, std::enable_if_t<std::is_enum_v<T>>> final : managed_base {
291 primary_key<T> detach() const {
292 return operator T();
293 }
294
295 operator T() const {
296 return static_cast<T>(m_obj->template get<int64_t>(m_key));
297 }
298
299 rbool operator==(const T& rhs) const noexcept {
300 if (this->m_rbool_query) {
301 return this->m_rbool_query->equal(m_key, serialize(rhs));
302 }
303 return serialize(detach().value) == serialize(rhs);
304 }
305 rbool operator!=(const T& rhs) const noexcept {
306 if (this->m_rbool_query) {
307 return this->m_rbool_query->not_equal(m_key, serialize(rhs));
308 }
309 return serialize(detach().value) != serialize(rhs);
310 }
311
312 private:
313 managed() = default;
314 managed(const managed&) = delete;
315 managed(managed &&) = delete;
316 managed& operator=(const managed&) = delete;
317 managed& operator=(managed&&) = delete;
318 template<typename, typename>
319 friend struct managed;
320 };
321
322 template<>
323 struct managed<primary_key<std::optional<int64_t>>> final : managed_base {
324 primary_key<std::optional<int64_t>> detach() const {
325 return operator std::optional<int64_t>();
326 }
327
328 operator std::optional<int64_t>() const {
329 return m_obj->get_optional<int64_t>(m_key);
330 }
331
332 rbool operator==(const std::optional<int64_t>& rhs) const noexcept;
333 rbool operator!=(const std::optional<int64_t>& rhs) const noexcept;
334 rbool operator>(const int64_t& rhs) const noexcept;
335 rbool operator>=(const int64_t& rhs) const noexcept;
336 rbool operator<(const int64_t& rhs) const noexcept;
337 rbool operator<=(const int64_t& rhs) const noexcept;
338 rbool operator>(const int& rhs) const noexcept;
339 rbool operator>=(const int& rhs) const noexcept;
340 rbool operator<(const int& rhs) const noexcept;
341 rbool operator<=(const int& rhs) const noexcept;
342
343 private:
344 managed() = default;
345 managed(managed &&) = delete;
346 managed& operator=(const managed&) = delete;
347 managed& operator=(managed&&) = delete;
348 template<typename, typename>
349 friend struct managed;
350 };
351
352 template<typename T>
353 struct managed<primary_key<T>, std::enable_if_t<std::conjunction_v<typename internal::type_info::is_optional<T>,
354 std::is_enum<typename T::value_type> >>> final : managed_base {
355 primary_key<T> detach() const {
356 return operator T();
357 }
358
359 operator T() const {
360 auto v = m_obj->get_optional<int64_t>(m_key);
361 if (v) {
362 return static_cast<typename T::value_type>(*v);
363 } else {
364 return std::nullopt;
365 }
366 }
367
368 rbool operator==(const T& rhs) const noexcept {
369 if (this->m_rbool_query) {
370 return this->m_rbool_query->equal(m_key, serialize(rhs));
371 }
372 return serialize(detach().value) == serialize(rhs);
373 }
374 rbool operator!=(const T& rhs) const noexcept {
375 if (this->m_rbool_query) {
376 return this->m_rbool_query->not_equal(m_key, serialize(rhs));
377 }
378 return serialize(detach().value) != serialize(rhs);
379 }
380
381 private:
382 managed() = default;
383 managed(const managed&) = delete;
384 managed(managed &&) = delete;
385 managed& operator=(const managed&) = delete;
386 managed& operator=(managed&&) = delete;
387 template<typename, typename>
388 friend struct managed;
389 };
390
391 template<>
392 struct managed<primary_key<std::optional<std::string>>> final : managed_base {
394 return operator std::optional<std::string>();
395 }
396
397 operator std::optional<std::string>() const {
398 return m_obj->get_optional<std::string>(m_key);
399 }
400
401 rbool operator==(const std::optional<std::string>& rhs) const noexcept;
402 rbool operator!=(const std::optional<std::string>& rhs) const noexcept;
403 rbool operator==(const char* rhs) const noexcept;
404 rbool operator!=(const char* rhs) const noexcept;
405
406 private:
407 managed() = default;
408 managed(const managed&) = delete;
409 managed(managed &&) = delete;
410 managed& operator=(const managed&) = delete;
411 managed& operator=(managed&&) = delete;
412 template<typename, typename>
413 friend struct managed;
414 };
415
416 template<>
417 struct managed<primary_key<std::optional<realm::uuid>>> final : managed_base {
419 return operator std::optional<realm::uuid>();
420 }
421
422 operator std::optional<realm::uuid>() const {
423 auto v = m_obj->get_optional<internal::bridge::uuid>(m_key);
424 if (v) {
425 return v->operator ::realm::uuid();
426 }
427 return std::nullopt;
428 }
429
430 rbool operator==(const std::optional<realm::uuid>& rhs) const noexcept;
431 rbool operator!=(const std::optional<realm::uuid>& rhs) const noexcept;
432
433 private:
434 managed() = default;
435 managed(const managed&) = delete;
436 managed(managed &&) = delete;
437 managed& operator=(const managed&) = delete;
438 managed& operator=(managed&&) = delete;
439 template<typename, typename>
440 friend struct managed;
441 };
442
443 template<>
444 struct managed<primary_key<std::optional<realm::object_id>>> final : managed_base {
445 std::optional<realm::object_id> detach() const {
446 return operator std::optional<realm::object_id>();
447 }
448
449 operator std::optional<realm::object_id>() const {
450 auto v = m_obj->get_optional<internal::bridge::object_id>(m_key);
451 if (v) {
452 return v->operator ::realm::object_id();
453 }
454 return std::nullopt;
455 }
456
457 rbool operator==(const std::optional<realm::object_id>& rhs) const noexcept;
458 rbool operator!=(const std::optional<realm::object_id>& rhs) const noexcept;
459
460 private:
461 managed() = default;
462 managed(const managed&) = delete;
463 managed(managed &&) = delete;
464 managed& operator=(const managed&) = delete;
465 managed& operator=(managed&&) = delete;
466 template<typename, typename>
467 friend struct managed;
468 };
469}
470
471#endif //CPPREALM_MANAGED_PRIMARY_KEY_HPP
Definition: rbool.hpp:36
Definition: object_id.hpp:31
Definition: uuid.hpp:32
Definition: macros.hpp:286
Definition: obj.hpp:62
Definition: types.hpp:56
Definition: managed_primary_key.hpp:43
Definition: managed_primary_key.hpp:72
Definition: managed_primary_key.hpp:30
Definition: types.hpp:35