Realm C++ SDK Version v2.2.0

obj.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_BRIDGE_OBJ_HPP
20#define CPPREALM_BRIDGE_OBJ_HPP
21
22#include <any>
23#include <chrono>
24#include <functional>
25#include <memory>
26#include <optional>
27#include <string>
28
29#include <cpprealm/types.hpp>
30#include <cpprealm/internal/bridge/binary.hpp>
31#include <cpprealm/internal/bridge/col_key.hpp>
32#include <cpprealm/internal/bridge/decimal128.hpp>
33#include <cpprealm/internal/bridge/dictionary.hpp>
34#include <cpprealm/internal/bridge/object.hpp>
35#include <cpprealm/internal/bridge/object_id.hpp>
36#include <cpprealm/internal/bridge/table.hpp>
37#include <cpprealm/internal/bridge/utils.hpp>
38
39namespace realm {
40 class Group;
41 class Realm;
42 class Timestamp;
43 class Object;
44 class ObjectSchema;
45 class Obj;
46 class TableRef;
47 class Query;
48 struct ColKey;
49 class LnkLst;
50 struct NotificationToken;
51
52 namespace internal::bridge {
53 struct obj_key;
54 struct obj_link;
55 }
56 namespace internal::type_info {
57 template <typename, typename>
58 struct type_info;
59 }
60
61 template <typename, typename>
62 struct managed;
63 template <typename, typename>
64 struct accessor;
65}
66
67namespace realm::internal::bridge {
68 struct obj;
69 struct mixed;
70 struct realm;
71 struct lnklst;
72 struct query;
73 struct object_schema;
74 struct timestamp;
75 struct obj_key;
76 struct obj_link;
77 struct table;
78 struct dictionary;
79 struct uuid;
80 struct object_id;
81 struct decimal128;
82 struct list;
83 struct row;
84 struct table_view;
85
86 namespace {
87 template <typename T>
88 struct is_optional : std::false_type {
89 using underlying = T;
90 };
91
92 template <typename T>
93 struct is_optional<std::optional<T>> : std::true_type {
94 using underlying = T;
95 };
96 }
97
98 template <typename T>
99 [[nodiscard]] T get(const obj&, const col_key& col_key);
100 template <>
101 [[nodiscard]] std::string get(const obj&, const col_key& col_key);
102 template <>
103 [[nodiscard]] uuid get(const obj&, const col_key& col_key);
104 template <>
105 [[nodiscard]] object_id get(const obj&, const col_key& col_key);
106 template <>
107 [[nodiscard]] decimal128 get(const obj&, const col_key& col_key);
108 template <>
109 [[nodiscard]] binary get(const obj&, const col_key& col_key);
110 template <>
111 [[nodiscard]] timestamp get(const obj&, const col_key& col_key);
112 template <>
113 [[nodiscard]] int64_t get(const obj&, const col_key& col_key);
114 template <>
115 [[nodiscard]] double get(const obj&, const col_key& col_key);
116 template <>
117 [[nodiscard]] bool get(const obj&, const col_key& col_key);
118 template <>
119 [[nodiscard]] mixed get(const obj&, const col_key& col_key);
120 template <>
121 [[nodiscard]] core_dictionary get(const obj&, const col_key& col_key);
122
123 struct obj {
124 obj();
125 obj(const obj& other) ;
126 obj& operator=(const obj& other) ;
127 obj(obj&& other);
128 obj& operator=(obj&& other);
129 ~obj();
130 obj(const Obj&); //NOLINT google-explicit-constructor
131 operator Obj() const; //NOLINT google-explicit-constructor
132 [[nodiscard]] table get_table() const noexcept;
133 [[nodiscard]] table get_target_table(col_key) const noexcept;
134 [[nodiscard]] bool is_null(const col_key& col_key) const;
135 [[nodiscard]] bool is_valid() const;
136 [[nodiscard]] obj get_linked_object(const col_key& col_key);
137 template <typename T>
138 T get(const col_key& col_key) const {
139 return internal::bridge::get<T>(*this, col_key);
140 }
141
142 template <typename T>
143 std::optional<T> get_optional(const col_key& col_key) const {
144 if (is_null(col_key)) {
145 return std::nullopt;
146 }
147 return internal::bridge::get<T>(*this, col_key);
148 }
149
150 void set(const col_key& col_key, const int64_t& value);
151 void set(const col_key& col_key, const double& value);
152 void set(const col_key& col_key, const std::string& value);
153 void set(const col_key& col_key, const mixed& value);
154 void set(const col_key& col_key, const bool& value);
155 void set(const col_key& col_key, const timestamp& value);
156 void set(const col_key& col_key, const binary& value);
157 void set(const col_key& col_key, const uuid& value);
158 void set(const col_key& col_key, const object_id& value);
159 void set(const col_key& col_key, const decimal128& value);
160 void set(const col_key& col_key, const obj_key& value);
161 void set(const col_key& col_key, const std::chrono::time_point<std::chrono::system_clock>& value);
162 template<typename T>
163 std::enable_if_t<std::is_enum_v<T>> set(const col_key& col_key, const T& value) {
164 set<int64_t>(col_key, static_cast<int64_t>(value));
165 }
166 template<typename T>
167 void set(const col_key& col_key, const std::optional<T>& value) {
168 if (value) {
169 set(col_key, *value);
170 } else {
171 set_null(col_key);
172 }
173 }
174
175 void set_list_values(const col_key& col_key, const std::vector<obj_key>& values);
176 void set_list_values(const col_key& col_key, const std::vector<std::string>& values);
177 void set_list_values(const col_key& col_key, const std::vector<bool>& values);
178 void set_list_values(const col_key& col_key, const std::vector<int64_t>& values);
179 void set_list_values(const col_key& col_key, const std::vector<double>& values);
180 void set_list_values(const col_key& col_key, const std::vector<internal::bridge::uuid>& values);
181 void set_list_values(const col_key& col_key, const std::vector<internal::bridge::object_id>& values);
182 void set_list_values(const col_key& col_key, const std::vector<internal::bridge::decimal128>& values);
183 void set_list_values(const col_key& col_key, const std::vector<binary>& values);
184 void set_list_values(const col_key& col_key, const std::vector<mixed>& values);
185 void set_list_values(const col_key& col_key, const std::vector<timestamp>& values);
186
187 void set_list_values(const col_key& col_key, const std::vector<std::optional<int64_t>>& values);
188 void set_list_values(const col_key& col_key, const std::vector<std::optional<bool>>& values);
189 void set_list_values(const col_key& col_key, const std::vector<std::optional<double>>& values);
190 void set_list_values(const col_key& col_key, const std::vector<std::optional<std::string>>& values);
191 void set_list_values(const col_key& col_key, const std::vector<std::optional<obj_key>>& values);
192 void set_list_values(const col_key& col_key, const std::vector<std::optional<internal::bridge::uuid>>& values);
193 void set_list_values(const col_key& col_key, const std::vector<std::optional<internal::bridge::object_id>>& values);
194 void set_list_values(const col_key& col_key, const std::vector<std::optional<binary>>& values);
195 void set_list_values(const col_key& col_key, const std::vector<std::optional<timestamp>>& values);
196
197 template <typename ValueType>
198 void set_list_values(const col_key& col_key, const std::vector<ValueType>& values) {
199 std::vector<typename internal::type_info::type_info<ValueType, void>::internal_type> v2;
200 for (auto v : values) {
201 if constexpr (std::is_pointer_v<ValueType>) {
203 if constexpr (managed<std::remove_pointer_t<ValueType>, void>::schema.HasPrimaryKeyProperty) {
204 auto pk = (*v).*(managed<std::remove_pointer_t<ValueType>, void>::schema.primary_key().ptr);
205 m_obj = this->get_table().create_object_with_primary_key(internal::bridge::mixed(serialize(pk.value)));
206 } else {
207 m_obj = m_obj = this->get_table().create_object();
208 }
209 std::apply([&m_obj, &v](auto && ...p) {
210 (accessor<typename std::decay_t<decltype(p)>::Result, void>::set(
211 m_obj, m_obj.get_table().get_column_key(p.name),
212 (*v).*(std::decay_t<decltype(p)>::ptr)), ...);
213 }, managed<std::remove_pointer_t<ValueType>, void>::schema.ps);
214 v2.push_back(m_obj.get_key());
215 } else {
216 v2.push_back(::realm::serialize(v));
217 }
218 }
219 set_list_values(col_key, v2);
220 }
221
222 [[nodiscard]] obj_key get_key() const;
223 [[nodiscard]] obj_link get_link() const;
224 lnklst get_linklist(const col_key& col_key);
225 core_dictionary get_dictionary(const col_key& col_key);
226 void set_null(const col_key&);
227 obj create_and_set_linked_object(const col_key&);
228 table_view get_backlink_view(table, col_key);
229 void to_json(std::ostream& out) const noexcept;
230 private:
231 inline const Obj* get_obj() const;
232 inline Obj* get_obj();
233 friend inline const Obj* get_obj(const obj&);
234 friend inline Obj* get_obj(obj&);
235 template <typename T>
236 friend T get(const obj&, const col_key& col_key);
237#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
238 storage::Obj m_obj[1];
239#else
240 std::shared_ptr<Obj> m_obj;
241#endif
242 };
243
244 struct group {
245 group(realm&);
246 table get_table(uint32_t table_key);
247 table get_table(const std::string& table_key);
248 private:
249 std::reference_wrapper<realm> m_realm;
250 };
251
252 std::string table_name_for_object_type(const std::string&);
253}
254
255
256#endif //CPPREALM_BRIDGE_OBJ_HPP
Definition: accessors.hpp:33
Definition: types.hpp:75
Definition: binary.hpp:30
Definition: col_key.hpp:28
Definition: dictionary.hpp:106
Definition: decimal128.hpp:30
Definition: obj.hpp:244
Definition: lnklst.hpp:33
Definition: mixed.hpp:69
Definition: obj_key.hpp:33
Definition: obj.hpp:123
Definition: object_id.hpp:31
Definition: realm.hpp:67
Definition: set.hpp:48
Definition: table.hpp:79
Definition: table.hpp:40
Definition: timestamp.hpp:30
Definition: uuid.hpp:32
Definition: obj.hpp:62
Definition: types.hpp:56
Definition: types.hpp:35