Realm C++ SDK Version v2.2.0

managed_set.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_SET_HPP
20#define CPPREALM_MANAGED_SET_HPP
21
22#include <cpprealm/macros.hpp>
23#include <cpprealm/notifications.hpp>
24#include <cpprealm/observation.hpp>
25#include <cpprealm/types.hpp>
26
27#include <set>
28
29namespace realm {
30
31 template<typename T>
32 struct managed<std::set<T>, std::enable_if_t<internal::type_info::is_primitive<T>::value>> : managed_base {
33 using managed<std::set<T>>::managed_base::operator=;
34 using value_type = T;
35
36 class iterator {
37 public:
38 using value_type = T;
39
40 using difference_type = std::ptrdiff_t;
41 using pointer = T*;
42 using reference = T&;
43 using iterator_category = std::forward_iterator_tag;
44
45 bool operator!=(const iterator& other) const
46 {
47 return !(*this == other);
48 }
49
50 bool operator==(const iterator& other) const
51 {
52 return (m_parent == other.m_parent) && (m_i == other.m_i);
53 }
54
55 T operator*() const noexcept
56 {
57 auto s = realm::internal::bridge::set(*m_parent->m_realm, *m_parent->m_obj, m_parent->m_key);
58 return deserialize<T>(s.get_any(m_i));
59 }
60
61 iterator& operator++()
62 {
63 this->m_i++;
64 return *this;
65 }
66
67 const iterator& operator++(int i)
68 {
69 this->m_i += i;
70 return *this;
71 }
72 private:
73 template<typename, typename>
74 friend struct managed;
75
76 iterator(size_t i, managed<std::set<T>>* parent)
77 : m_i(i), m_parent(parent)
78 {
79 }
80 size_t m_i;
81 managed<std::set<T>>* m_parent;
82 };
83 iterator begin()
84 {
85 return iterator(0, this);
86 }
87
88 iterator end()
89 {
90 return iterator(size(), this);
91 }
92 [[nodiscard]] std::set<T> detach() const {
93 auto set = realm::internal::bridge::set(*m_realm, *m_obj, m_key);
94 auto ret = std::set<T>();
95 for(size_t i = 0; i < set.size(); i++) {
96 ret.insert(deserialize<T>(set.get_any(i)));
97 }
98 return ret;
99 }
100
101 realm::notification_token observe(std::function<void(realm::collection_change)>&& fn) {
102 auto set = std::make_shared<realm::internal::bridge::set>(*m_realm, *m_obj, m_key);
103 realm::notification_token token = set->add_notification_callback(
104 std::make_shared<realm::collection_callback_wrapper>(
105 std::move(fn),
106 false));
107 token.m_realm = *m_realm;
108 token.m_set = set;
109 return token;
110 }
111
112 void erase(const iterator& it)
113 {
114 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
115 set.remove(serialize(*it));
116 }
117
118 std::pair<iterator, bool> insert(const T& v)
119 {
120 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
121 if constexpr (internal::type_info::MixedPersistableConcept<T>::value) {
122 std::pair<size_t, bool> res = set.insert(serialize<T>(v));
123 return std::pair<iterator, bool>(iterator(res.first, this), res.second);
124 } else {
125 std::pair<size_t, bool> res = set.insert(v);
126 return std::pair<iterator, bool>(iterator(res.first, this), res.second);
127 }
128 }
129
130 iterator insert(const iterator&, const T& v)
131 {
132 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
133 std::pair<size_t, bool> res = set.insert(v);
134 return iterator(res.first, this);
135 }
136
137 iterator find(const T& v)
138 {
139 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
140 size_t idx = set.find(serialize(v));
141 if (idx == realm::not_in_collection)
142 return iterator(size(), this);
143 return iterator(idx, this);
144 }
145 void clear() {
146 internal::bridge::set(*m_realm, *m_obj, m_key).remove_all();
147 }
148
149 size_t size()
150 {
151 return internal::bridge::set(*m_realm, *m_obj, m_key).size();
152 }
153
154 private:
155 managed() = default;
156 managed(const managed&) = delete;
157 managed(managed &&) = delete;
158 managed& operator=(const managed&) = delete;
159 managed& operator=(managed&&) = delete;
160 template<typename, typename>
161 friend struct managed;
162 };
163
164 template<typename T>
165 struct managed<std::set<T*>> : managed_base {
166 using managed<std::set<T*>>::managed_base::operator=;
167 using value_type = managed<T>;
168
169 class iterator {
170 public:
171 using value_type = managed<T>;
172
173 using difference_type = std::ptrdiff_t;
174 using pointer = T*;
175 using reference = T&;
176 using iterator_category = std::forward_iterator_tag;
177
178 bool operator!=(const iterator& other) const
179 {
180 return !(*this == other);
181 }
182
183 bool operator==(const iterator& other) const
184 {
185 return (m_parent == other.m_parent) && (m_i == other.m_i);
186 }
187
188 managed<T> operator*() const noexcept
189 {
190 auto s = realm::internal::bridge::set(*m_parent->m_realm, *m_parent->m_obj, m_parent->m_key);
191 managed<T> m(s.get_obj(m_i), *m_parent->m_realm);
192 return {std::move(m)};
193 }
194
195 iterator& operator++()
196 {
197 this->m_i++;
198 return *this;
199 }
200
201 const iterator& operator++(int i)
202 {
203 this->m_i += i;
204 return *this;
205 }
206 private:
207 template<typename, typename>
208 friend struct managed;
209
210 iterator(size_t i, managed<std::set<T*>>* parent)
211 : m_i(i), m_parent(parent)
212 {
213 }
214 size_t m_i;
215 managed<std::set<T*>>* m_parent;
216 };
217 iterator begin()
218 {
219 return iterator(0, this);
220 }
221
222 iterator end()
223 {
224 return iterator(size(), this);
225 }
226 [[nodiscard]] std::set<T*> detach() const {
227 auto s = realm::internal::bridge::set(*m_realm, *m_obj, m_key);
228 size_t count = s.size();
229 if (count == 0)
230 return std::set<T*>();
231 auto ret = std::set<T*>();
232 for(size_t i = 0; i < count; i++) {
233 managed<T> m(s.get_obj(i), *m_realm);
234 T* v = new T();
235 auto assign = [&m, &v](auto& pair) {
236 (*v).*(std::decay_t<decltype(pair.first)>::ptr) = (m.*(pair.second)).detach();
237 };
238 auto zipped = internal::zip_tuples(managed<T>::schema.ps, managed<T>::managed_pointers());
239 std::apply([&v, &m, &assign](auto && ...pair) {
240 (assign(pair), ...);
241 }, zipped);
242
243 ret.insert(v);
244 }
245 return ret;
246 }
247
248 realm::notification_token observe(std::function<void(realm::collection_change)>&& fn) {
249 auto set = std::make_shared<realm::internal::bridge::set>(*m_realm, *m_obj, m_key);
250 realm::notification_token token = set->add_notification_callback(
251 std::make_shared<realm::collection_callback_wrapper>(
252 std::move(fn),
253 false));
254 token.m_realm = *m_realm;
255 token.m_set = set;
256 return token;
257 }
258
259 void erase(const iterator& it)
260 {
261 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
262 set.remove(it.operator*().m_obj.get_key());
263 }
264
265 std::pair<iterator, bool> insert(T* value)
266 {
267 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
268 auto table = m_obj->get_target_table(m_key);
269 internal::bridge::obj m_obj;
270 if constexpr (managed<T>::schema.HasPrimaryKeyProperty) {
271 auto pk = (*value).*(managed<T>::schema.primary_key().ptr);
272 m_obj = table.create_object_with_primary_key(realm::internal::bridge::mixed(serialize(pk.value)));
273 } else {
274 m_obj = table.create_object();
275 }
276 std::apply([&m_obj, &value, realm = *m_realm](auto && ...p) {
277 (accessor<typename std::decay_t<decltype(p)>::Result>::set(
278 m_obj, m_obj.get_table().get_column_key(p.name), realm,
279 (*value).*(std::decay_t<decltype(p)>::ptr)), ...);
280 }, managed<T, void>::schema.ps);
281 if (!managed<T>::schema.is_embedded()) {
282 set.insert(m_obj.get_key());
283 }
284 std::pair<size_t, bool> res = set.insert(m_obj.get_key());
285 return std::pair<iterator, bool>(iterator(res.first, this), res.second);
286 }
287
288 iterator insert(const iterator&, T* value)
289 {
290 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
291 auto table = m_obj->get_target_table(m_key);
292 internal::bridge::obj m_obj;
293 if constexpr (managed<T>::schema.HasPrimaryKeyProperty) {
294 auto pk = (*value).*(managed<T>::schema.primary_key().ptr);
295 m_obj = table.create_object_with_primary_key(realm::internal::bridge::mixed(serialize(pk.value)));
296 } else {
297 m_obj = table.create_object();
298 }
299 std::apply([&m_obj, &value, realm = *m_realm](auto && ...p) {
300 (accessor<typename std::decay_t<decltype(p)>::Result>::set(
301 m_obj, m_obj.get_table().get_column_key(p.name), realm,
302 (*value).*(std::decay_t<decltype(p)>::ptr)), ...);
303 }, managed<T, void>::schema.ps);
304 std::pair<size_t, bool> res = set.insert(m_obj.get_key());
305 return iterator(res.first, this);
306 }
307
308 std::pair<iterator, bool> insert(const managed<T>& value)
309 {
310 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
311 std::pair<size_t, bool> res = set.insert(value.m_obj.get_key());
312 return std::pair<iterator, bool>(iterator(res.first, this), res.second);
313
314 }
315
316 iterator insert(const iterator&, const managed<T>& value)
317 {
318 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
319 std::pair<size_t, bool> res = set.insert(value.m_obj.get_key());
320 return iterator(res.first, this);
321 }
322
323 std::pair<iterator, bool> insert(const managed<T*>& value)
324 {
325 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
326 std::pair<size_t, bool> res = set.insert(value.m_obj.get_key());
327 return std::pair<iterator, bool>(iterator(res.first, this), res.second);
328
329 }
330
331 iterator insert(const iterator&, const managed<T*>& value)
332 {
333 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
334 std::pair<size_t, bool> res = set.insert(value.m_obj.get_key());
335 return iterator(res.first, this);
336 }
337
338 iterator find(const managed<T>& v)
339 {
340 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
341 size_t idx = set.find(v.m_obj.get_key());
342 if (idx == realm::not_in_collection)
343 return iterator(size(), this);
344 return iterator(idx, this);
345 }
346
347 iterator find(const managed<T*>& v)
348 {
349 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
350 size_t idx = set.find(v.m_obj->get_key());
351 if (idx == realm::not_in_collection)
352 return iterator(size(), this);
353 return iterator(idx, this);
354 }
355 void clear() {
356 internal::bridge::set(*m_realm, *m_obj, m_key).remove_all();
357 }
358
359 size_t size()
360 {
361 return internal::bridge::set(*m_realm, *m_obj, m_key).size();
362 }
363
364 private:
365 managed() = default;
366 managed(const managed&) = delete;
367 managed(managed &&) = delete;
368 managed& operator=(const managed&) = delete;
369 managed& operator=(managed&&) = delete;
370 template<typename, typename>
371 friend struct managed;
372 };
373} // namespace realm
374
375#endif//CPPREALM_MANAGED_SET_HPP
Definition: observation.hpp:176
Definition: mixed.hpp:69
Definition: set.hpp:48
Definition: macros.hpp:286
Definition: obj.hpp:62
Definition: notifications.hpp:38