Realm C++ SDK版本 v 2.2.0

managed_set.hpp

1
2//
3// 版权所有 2024 Realm Inc.
4//
5// 根据 Apache 许可证 2.0 版(“许可证”)获得许可;
6// 除非符合合规,否则不得使用此文件。
7// 您可以在以下网址获取许可证副本:
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// 除非适用法律要求或书面同意,否则软件
12// 根据许可证分发是按“原样”分发的,
13// 不提供任何类型的Express或暗示的保证或条件。
14// 请参阅管理权限的特定语言的许可证和
15// 许可证下的限制。
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
29命名空间域 {
30
31 template < typename T>
32 struct 托管 <std:: 设立<T>, std::enable_if_t<internal::type_info::is_primitive<T>::value>> : Managed_base {
33 运用 托管 <std:: 设立<T>>::managed_base:: 操作符=;
34 using value_type = T;
35
36 迭代器 {
37 public :
38 using value_type = T;
39
40 using different_type = std::ptrdiff_t;
41 usingpointer = T*;
42 using reference = T&;
43 using iterator_category = std::forward_iterator_tag;
44
45 bool操作符!=( const iterator& other) const
46 {
47 return !(* this == other);
48 }
49
50 bool 操作符==( const iterator& other) const
51 {
52 return (m_parent == other.m_parent) && (m_i == other.m_i);
53 }
54
55 T操作符*() const noException
56 {
57 auto s = 域::internal::bridge:: 设立 (*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 朋友 struct 托管
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 托管<std:: 设立<T> >* m_parent;
82 };
83迭代器 begin()
84 {
85 return iterator( 0 , this );
86 }
87
88迭代器 end()
89 {
90 return iterator(size(), this );
91 }
92 [[nodiscard]] std:: 设立<T> detach() const {
93 auto 设立 = 域::internal::bridge:: 设立 (*m_realm, *m_obj, m_key);
94 auto ret = std::set<T>();
95 for ( size_t i = 0 ; i < 设立.size(); i++) {
96 ret.insert(deserialize<T>(set.get_any(i)));
97 }
98 return ret;
99 }
100
101 域::notification_token Watch(std::function< void ( 域::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<realm::collection_callback_wrapper>std::make_shared(
105 std::move(fn),
106 false ));
107 token.m_realm = *m_realm;
108 token.m_set = set;
109 返回令牌;
110 }
111
112 void delete( const iterator& it)
113 {
114 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
115设立。删除(serialize(*it));
116 }
117
118 std::pair<iterator, bool> insert( const T& v)
119 {
120 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
121 if constexpr (internal::type_info::MixedPersistableConcept<T>::value) {
122 std::pair<size_t, bool> res = 设立.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 = 设立.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 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
133 std::pair<size_t, bool> res = 设立.insert(v);
134 return iterator(res.first, this );
135 }
136
137 iterator find( const T& v)
138 {
139 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
140 size_t idx = 设立.find(serialize(v));
141 if (idx == 域::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 returninternal::bridge:: 设立(* m_realm , *m_obj, m_key).size();
152 }
153
154 private :
155 managed() = default ;
156 Managed( const Managed&) = delete ;
157 托管(托管 &&) = 删除 ;
158 Managed& 操作符=( const Managed&) = 删除 ;
159 Managed& Operator=(managed&&) = delete ;
160 template < typename , typename >
161 朋友 struct 托管;
162 };
163
164 template < typename T>
165 struct managed <std::set<T*>> : Managed_base {
166 运用 托管 <std:: 设立<T*>>::managed_base:: 操作符=;
167 运用 value_type = Managed<T> ;
168
169 迭代器 {
170 public :
171 运用 value_type = Managed<T> ;
172
173 using different_type = std::ptrdiff_t;
174 usingpointer = T*;
175 using reference = T&;
176 using iterator_category = std::forward_iterator_tag;
177
178 bool操作符!=( const iterator& other) const
179 {
180 return !(* this == other);
181 }
182
183 bool 操作符==( const iterator& other) const
184 {
185 return (m_parent == other.m_parent) && (m_i == other.m_i);
186 }
187
188 托管<T>操作符*() const noException
189 {
190 auto s = 域::internal::bridge:: 设立 (*m_parent->m_realm, *m_parent->m_obj, m_parent->m_key);
191 托管<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 朋友 struct 托管
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迭代器 begin()
218 {
219 return iterator( 0 , this );
220 }
221
222迭代器 end()
223 {
224 return iterator(size(), this );
225 }
226 [[nodiscard]] std:: 设立<T*> detach() const {
227 auto s = 域::internal::bridge:: 设立 (*m_realm, *m_obj, m_key);
228 size_t count = s.size();
229 if (count == 0 )
230 return std:: 设立<T*>();
231 auto ret = std::set<T*>();
232 for(size_t i = 0; i < count; i++) {
233 托管<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(托管<T>:: 模式.ps, 托管<T>::managed_pointers());
239 std:: 应用([&v, &m, & assign]( auto && ...pair) {
240 (分配(对), ...);
241 },已压缩);
242
243 ret.insert(v);
244 }
245 return ret;
246 }
247
248 域::notification_token Watch(std::function< void ( 域::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<realm::collection_callback_wrapper>std::make_shared(
252 std::move(fn),
253 false ));
254 token.m_realm = *m_realm;
255 token.m_set = set;
256 返回令牌;
257 }
258
259 void delete( const iterator& it)
260 {
261 auto 设立 = external::bridge:: 设立(*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 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
268 auto table = m_obj->get_target_table(m_key);
269内部::桥::obj m_obj;
270 if constexpr (托管<T>:: 模式.HasPrimaryKeyProperty) {
271 auto pk = (*value).*(managed<T>::schema.primary_key().ptr);
272 m_obj = Table.create_object_with_primary_key( 域::internal::bridge::mixed (serialize(pk.value)));
273 } else {
274 m_obj = table.create_object();
275 }
276 std:: 应用([&m_obj, &value, 域 = *m_realm]( auto && ...p) {
277 (accessor< typename std::decay_t< decltype (p)>::Result>:: 设立(
278 m_obj, m_obj.get_table().get_column_key(p.name), Realm,
279 (*value).*(std::decay_t< decltype (p)>::ptr)), ...);
280 }, 托管<T, void>:: 模式.ps);
281 if (!managed<T>::schema.is_embedded()) {
282 set.insert(m_obj.get_key());
283 }
284 std::pair<size_t, bool> res = 设立.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 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
291 auto table = m_obj->get_target_table(m_key);
292内部::桥::obj m_obj;
293 if constexpr (托管<T>:: 模式.HasPrimaryKeyProperty) {
294 auto pk = (*value).*(managed<T>::schema.primary_key().ptr);
295 m_obj = Table.create_object_with_primary_key( 域::internal::bridge::mixed (serialize(pk.value)));
296 } else {
297 m_obj = table.create_object();
298 }
299 std:: 应用([&m_obj, &value, 域 = *m_realm]( auto && ...p) {
300 (accessor< typename std::decay_t< decltype (p)>::Result>:: 设立(
301 m_obj, m_obj.get_table().get_column_key(p.name), Realm,
302 (*value).*(std::decay_t< decltype (p)>::ptr)), ...);
303 }, 托管<T, void>:: 模式.ps);
304 std::pair<size_t, bool> res = 设立.insert(m_obj.get_key());
305 return iterator(res.first, this );
306 }
307
308 std::pair<iterator , bool> insert( constmanaged <T>& value)
309 {
310 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
311 std::pair<size_t, bool> res = 设立.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 托管&, constmanaged <T>& value)
317 {
318 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
319 std::pair<size_t, bool> res = 设立.insert(value.m_obj.get_key());
320 return iterator(res.first, this );
321 }
322
323 std::pair<iterator, bool> insert( constmanaged <T*>& value)
324 {
325 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
326 std::pair<size_t, bool> res = 设立.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&, constmanaged <T*>& value)
332 {
333 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
334 std::pair<size_t, bool> res = 设立.insert(value.m_obj.get_key());
335 return iterator(res.first, this );
336 }
337
338 iterator find( constmanaged <T>& v)
339 {
340 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
341 size_t idx = 设立.find(v.m_obj.get_key());
342 if (idx == 域::not_in_collection)
343 return iterator(size(), this );
344 return iterator(idx, this );
345 }
346
347 iterator find( constmanaged <T*>& v)
348 {
349 auto 设立 = external::bridge:: 设立(*m_realm, *m_obj, m_key);
350 size_t idx = 设立.find(v.m_obj->get_key());
351 if (idx == 域::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 returninternal::bridge:: 设立(* m_realm , *m_obj, m_key).size();
362 }
363
364 private :
365 managed() = default ;
366 Managed( const Managed&) = delete ;
367 托管(托管 &&) = 删除 ;
368 Managed& 操作符=( const Managed&) = 删除 ;
369 Managed& Operator=(managed&&) = delete ;
370 template < typename , typename >
371 朋友 struct 托管;
372 };
373 } // 命名空间 Realm
374
375#endif//CPREALM_MANAGED_SET_HPP
定义: observation.hpp: 176
定义: mixed.hpp:69
定义: set.hpp: 48
定义: macros.hpp:286
定义: obj.hpp:62
定义: notifications.hpp:38