Realm C++ SDK Version v2.2.0

managed_objectid.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_OBJECTID_HPP
20#define CPPREALM_MANAGED_OBJECTID_HPP
21
22#include <cpprealm/macros.hpp>
23#include <cpprealm/types.hpp>
24
25#include <cpprealm/internal/bridge/object_id.hpp>
26
27namespace realm {
28 class rbool;
29}
30
31namespace realm {
32 template<>
33 struct managed<realm::object_id> : managed_base {
34 using managed<realm::object_id>::managed_base::operator=;
35 [[nodiscard]] realm::object_id detach() const {
36 return m_obj->template get<realm::internal::bridge::object_id>(m_key).operator ::realm::object_id();
37 }
38
39 [[nodiscard]] realm::object_id operator *() const {
40 return detach();
41 }
42
43 [[nodiscard]] operator realm::object_id() const {
44 return detach();
45 }
46
47 //MARK: - comparison operators
48 rbool operator==(const realm::object_id& rhs) const noexcept;
49 rbool operator!=(const realm::object_id& rhs) const noexcept;
50
51 private:
52 managed() = default;
53 managed(const managed&) = delete;
54 managed(managed &&) = delete;
55 managed& operator=(const managed&) = delete;
56 managed& operator=(managed&&) = delete;
57 template<typename, typename>
58 friend struct managed;
59 };
60
61 template<>
62 struct managed<std::optional<realm::object_id>> : managed_base {
63 using managed<std::optional<realm::object_id>>::managed_base::operator=;
64
65 [[nodiscard]] std::optional<realm::object_id> detach() const {
66 auto v = m_obj->template get_optional<realm::internal::bridge::object_id>(m_key);
67 if (v) {
68 return v.value().operator ::realm::object_id();
69 } else {
70 return std::nullopt;
71 }
72 }
73
74 [[nodiscard]] std::optional<realm::object_id> operator *() const {
75 return detach();
76 }
77
78 [[nodiscard]] operator std::optional<realm::object_id>() const {
79 return detach();
80 }
81
82 //MARK: - comparison operators
83 rbool operator==(const std::optional<realm::object_id>& rhs) const noexcept;
84 rbool operator!=(const std::optional<realm::object_id>& rhs) const noexcept;
85
86 private:
87 managed() = default;
88 managed(const managed&) = delete;
89 managed(managed &&) = delete;
90 managed& operator=(const managed&) = delete;
91 managed& operator=(managed&&) = delete;
92 template<typename, typename>
93 friend struct managed;
94 };
95
96} // namespace realm
97
98#endif//CPPREALM_MANAGED_OBJECTID_HPP
Definition: rbool.hpp:36
Definition: macros.hpp:286
Definition: obj.hpp:62
Definition: types.hpp:56