Realm C++ SDK版本 v 2.2.0

managed_list.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_LIST_HPP
20#define CPPREALM_MANAGED_LIST_HPP
21
22#include <cpprealm/macros.hpp>
23#include <cpprealm/notifications.hpp>
24#include <cpprealm/observation.hpp>
25#include <cpprealm/types.hpp>
26
27命名空间域 {
28
29 template < typename T>
30 struct 托管 <std::vector<T>, std::enable_if_t<internal::type_info::is_primitive<T>::value>> : Managed_base {
31 运用 托管 <std::vector<T>>::managed_base:: 操作符=;
32
33 迭代器 {
34 public :
35 using iterator_category = std::input_iterator_tag;
36
37 bool操作符!=( const iterator& other) const
38 {
39 return !(* this == other);
40 }
41
42 bool 操作符==( const iterator& other) const
43 {
44 return (m_parent == other.m_parent) && (m_i == other.m_i);
45 }
46
47 T操作符*() const noException
48 {
49 return (*m_parent)[m_i];
50 }
51
52 iterator& Operator++()
53 {
54 this->m_i++;
55 return * this ;
56 }
57
58 const iterator& Operator++( int i)
59 {
60 this->m_i += i;
61 return * this ;
62 }
63 private :
64 template < typename , typename >
65 朋友 struct 托管
66
67 iterator( size_t i, 托管 <std::vector<T>>*parent)
68 :m_i(i), m_parent(parent)
69 {
70 }
71 size_t m_i;
72 托管<std::vector<T> >* m_parent;
73 };
74迭代器 begin()
75 {
76 return iterator( 0 , this );
77 }
78
79迭代器 end()
80 {
81 return iterator(size(), this );
82 }
83 [[nodiscard]] std::vector<T> 分离() const {
84 auto list = realm::internal::bridge::list (*m_realm, *m_obj, m_key);
85 using U = typenameInternal ::type_info::type_info<T>::internal_type;
86
87 size_t count = list.size();
88 if (count == 0 )
89 return std::vector<T>();
90
91 auto ret = std::vector<T>();
92 ret.reserve(count);
93 for(size_t i = 0; i < count; i++) {
94 if constexpr (internal::type_info::MixedPersistableConcept<T>::value) {
95 ret.push_back(deserialize<T>(域::internal::bridge::get<U>(list, i)));
96 } else if constexpr (std::is_enum_v<T>) {
97 ret.push_back(deserialize<T>(域::internal::bridge::get<U>(list, i)));
98 } else {
99 ret.push_back(deserialize(域::internal::bridge::get<U>(list, i)));
100 }
101 }
102
103 return ret;
104 }
105
106 [[nodiscard]] 结果<T> as_results() const {
107 return结果<T>( 域::internal::bridge::list (*m_realm, *m_obj, m_key).as_results());
108 }
109
110 域::notification_token Watch(std::function< void ( 域::collection_change )>&& fn) {
111 auto list = std::make_shared(*m_realm,<realm::internal::bridge::list> *m_obj, m_key);
112 域::notification_token token = list->add_notification_callback(
113<realm::collection_callback_wrapper>std::make_shared(
114 std::move(fn),
115 false ));
116 token.m_realm = *m_realm;
117 token.m_list = list;
118 返回令牌;
119 }
120
121 // TODO:模拟对该值的引用。
122 T操作符[]( size_t idx) const {
123 auto list = realm::internal::bridge::list (*m_realm, *m_obj, m_key);
124 using U = typenameInternal ::type_info::type_info<T>::internal_type;
125 if constexpr (internal::type_info::MixedPersistableConcept<T>::value) {
126 return deserialize<T>(realm::internal::bridge::get<U>(list, idx));
127 } else if constexpr (std::is_enum_v<T>) {
128 return static_cast< T > (deserialize<T>(域::internal::bridge::get<U>(list, idx)));
129 } else {
130 return deserialize(域::internal::bridge::get<U>(list, idx));
131 }
132 }
133
134 voidpop_back () {
135 internal::bridge::list(*m_realm, *m_obj, m_key)。 删除(size() - 1 );
136 }
137 void delete( size_t idx) {
138 internal::bridge::list(*m_realm, *m_obj, m_key)。删除(idx);
139 }
140 void clear() {
141 Internal::bridge::list(*m_realm, *m_obj, m_key).remove_all();
142 }
143 void push_back( const T& value) {
144 auto list =Internal::bridge::list(*m_realm, *m_obj, m_key);
145 list.add(serialize(value));
146 }
147 size_t size() {
148 returninternal::bridge::list(* m_realm , *m_obj, m_key).size();
149 }
150 size_t find( const T& a) {
151 if constexpr (std::is_enum_v<T>) {
152 returninternal::bridge::list(* m_realm , *m_obj, m_key).find( static_cast< int 64 _t > (a));
153 } else {
154 returninternal::bridge::list(* m_realm , *m_obj, m_key).find(a);
155 }
156 }
157 void set( size_t pos, const T& a) {
158 internal::bridge::list(*m_realm, *m_obj, m_key)。设立(pos, a);
159 }
160
161结果 <T> sort( bool ascending) {
162 return结果<T>(internal::bridge::list(*m_realm, *m_obj, m_key)
163 .sort(std::vector<internal::bridge::sort_descriptor>({{ "self" , 升序}})));
164 }
165
166 private :
167 managed() = default ;
168 Managed( const Managed&) = delete ;
169 托管(托管 &&) = 删除 ;
170 Managed& 操作符=( const Managed&) = 删除 ;
171 Managed& Operator=(managed&&) = delete ;
172 template < typename , typename >
173 朋友 struct 托管;
174 };
175
176 template < typename T>
177 struct managed <std::vector<T*>> : Managed_base {
178 public :
179 [[nodiscard]] std::vector<T*> detach() const {
180 auto list = realm::internal::bridge::list (*m_realm, *m_obj, m_key);
181 size_t count = list.size();
182 if (count == 0 )
183 return std::vector<T*>();
184 auto ret = std::vector<T*>();
185 ret.reserve(count);
186 for(size_t i = 0; i < count; i++) {
187 托管<T> m(域::internal::bridge::get<internal::bridge::obj>(list, i), *m_realm);
188 T* v = new T();
189 auto assign = [&m, &v]( auto &pair) {
190 (*v).*(std::decay_t< decltype (pair.first)>::ptr) = (m.*(pair.second)).detach();
191 };
192 auto zipped =Internal::zip_tuples( 托管<T>:: 模式 .ps, 托管<T>::managed_pointers ());
193 std:: 应用([&v, &m, & assign]( auto && ...pair) {
194 (分配(对), ...);
195 },已压缩);
196
197 ret.push_back(v);
198 }
199 return ret;
200 }
201
202 [[nodiscard]]结果<T*> as_results() const {
203 return Results <T*> ( realm::internal::bridge::list (*m_realm, *m_obj, m_key).as_results());
204 }
205
206 迭代器 {
207 public :
208 运用 value_type = Managed<T> ;
209
210 using different_type = std::ptrdiff_t;
211 usingpointer = T*;
212 using reference = T&;
213 using iterator_category = std::forward_iterator_tag;
214
215 bool操作符!=( const iterator& other) const
216 {
217 return !(* this == other);
218 }
219
220 bool 操作符==( const iterator& other) const
221 {
222 return (m_parent == other.m_parent) && (m_i == other.m_i);
223 }
224
225 托管<T>操作符*() const noException
226 {
227 auto list = realm::internal::bridge::list (*m_parent->m_realm, *m_parent->m_obj, m_parent->m_key);
228 managed<T> m(realm::internal::bridge::get<realm::internal::bridge::obj>(list, m_i), *m_parent->m_realm);
229 return {std::move(m)};
230 }
231
232 iterator& Operator++()
233 {
234 this->m_i++;
235 return * this ;
236 }
237
238 const iterator& Operator++( int i)
239 {
240 this->m_i += i;
241 return * this ;
242 }
243 private :
244 template < typename , typename >
245 朋友 struct 托管
246
247 iterator( size_t i, 托管 <std::vector<T*>>*parent)
248 :m_i(i), m_parent(parent)
249 {
250 }
251 size_t m_i;
252 managed<std::vector<T*> >* m_parent;
253 };
254
255迭代器 begin()
256 {
257 return iterator( 0 , this );
258 }
259
260迭代器 end()
261 {
262 return iterator(size(), this );
263 }
264
265 voidpop_back () {
266 internal::bridge::list(*m_realm, *m_obj, m_key)。 删除(size() - 1 );
267 }
268 void delete( size_t idx) {
269 internal::bridge::list(*m_realm, *m_obj, m_key)。删除(idx);
第 270
271 void clear() {
272 Internal::bridge::list(*m_realm, *m_obj, m_key).remove_all();
273 }
274 void push_back(T* value)
275 {
276 auto list =Internal::bridge::list(*m_realm, *m_obj, m_key);
277 auto table = m_obj->get_target_table(m_key);
278内部::桥::obj m_obj;
279 if constexpr (托管<T>:: 模式.HasPrimaryKeyProperty) {
280 auto pk = (*value).*(managed<T>::schema.primary_key().ptr);
281 m_obj = Table.create_object_with_primary_key( 域::internal::bridge::mixed (serialize(pk.value)));
282 } else if (托管<T>:: 模式.is_embedded()) {
283 m_obj = list.add_embedded();
284 } else {
285 m_obj = table.create_object();
286 }
287 std:: 应用([&m_obj, &value, 域 = *m_realm]( auto && ...p) {
288 (accessor< typename std::decay_t< decltype (p)>::Result>:: 设立(
289 m_obj, m_obj.get_table().get_column_key(p.name), Realm,
290 (*value).*(std::decay_t< decltype (p)>::ptr)), ...);
291 }, 托管<T, void>:: 模式.ps);
292 if (!managed<T>::schema.is_embedded()) {
293 list.add(m_obj.get_key());
294 }
295 }
296 void push_back( constmanaged <T>& value)
297 {
298 auto list =Internal::bridge::list(*m_realm, *m_obj, m_key);
299 if (!managed<T>::schema.is_embedded()) {
300 list.add(value.m_obj.get_key());
301 } else {
302 throw std::logic_error( "Cannot add existing 嵌入式对象 to 托管 List." );
303 }
304 }
305 void 托管( constmanaged <T*>& value)
306 {
307 if (!managed<T>::schema.is_embedded()) {
308 auto list =Internal::bridge::list(*m_realm, *m_obj, m_key);
309 list.add(value.m_obj->get_key());
310 } else {
311 throw std::logic_error( "Cannot add existing 嵌入式对象 to 托管 List." );
312 }
313 }
314
315 size_t size() const
316 {
317 returninternal::bridge::list(* m_realm , *m_obj, m_key).size();
318 }
319 size_t find( constmanaged <T>& a) {
320 returninternal::bridge::list(* m_realm , *m_obj, m_key).find(a.m_obj.get_key());
321 }
322 size_t find( const typenamemanaged <T*>::ref_type& a) const {
323 returninternal::bridge::list(* m_realm , *m_obj, m_key).find(a->m_obj.get_key());
324 }
325 typename 托管<T*>::ref_type 操作符[]( size_t idx) const {
326 auto list = realm::internal::bridge::list (*m_realm, *m_obj, m_key);
327 return typenamemanaged <T*>:: 托管(<T> <realm::internal::bridge::obj>域::internal::bridge::get(list, idx), *m_realm));
328 }
329
330 域::notification_token Watch(std::function< void ( 域::collection_change )>&& fn) {
331 auto list = std::make_shared(*m_realm,<realm::internal::bridge::list> *m_obj, m_key);
332 域::notification_token token = list->add_notification_callback(
333<realm::collection_callback_wrapper>std::make_shared(
334 std::move(fn),
335 false ));
336 token.m_realm = *m_realm;
337 token.m_list = list;
338 返回令牌;
339 }
340
341结果 <T> where( const std::string &query, const std::vector<realm::mixed> &arguments) {
342 std::vector<internal::bridge::mixed> Mixed_args;
343 for ( auto & a : 参数)
344 Mixed_args.push_back(serialize(a));
345 return结果<T>(internal::bridge::results(*m_realm, m_obj->get_target_table(m_key). 查询(查询, std::move(mixed_args))));
346 }
347
348结果<T> where(std::function<rbool(托管<T> &)> &&fn) {
349 static_assert ( sizeof (managed<T>), "Must describe schema for T" );
350 auto 模式 = m_realm-> 模式().find(托管<T>:: 模式.name);
351 auto group = m_realm->read_group();
352 autotable_ref = group.get_table(schema.table_key());
353 rbool query = rbool(internal::bridge::query(table_ref));
354 auto query_object = 托管<T>::prepare_for_query(*m_realm, &query);
355 auto full_query = fn(query_object).q;
356 return结果<T>(internal::bridge::results(*m_realm, full_query));
第 357 章
358
359结果 <T> sort( const std::string &key_path, bool ascending) {
360 auto 模式 = m_realm-> 模式().find(托管<T>:: 模式.name);
361 autotable_ref = m_obj->get_target_table(m_key);
362 return Results<T>(internal::bridge::results(*m_realm,table_ref)).sort({{key_path, 升序}});
363 }
364
365结果 <T> sort( const std::vector<internal::bridge::sort_descriptor> &sort_descriptor) {
366 auto 模式 = m_realm-> 模式().find(托管<T>:: 模式.name);
367 autotable_ref = m_obj->get_target_table(m_key);
368 return Results<T>(internal::bridge::results(*m_realm,table_ref)).sort(sort_descriptors);
369 }
370
371 private :
372 managed() = default ;
373 Managed( const Managed&) = delete ;
374 托管(托管 &&) = 删除 ;
375 Managed& 操作符=( const Managed&) = 删除 ;
376 Managed& Operator=(managed&&) = delete ;
377 template < typename , typename >
378 朋友 struct 托管;
379 };
380 } // 命名空间 Realm
381
382#endif//CPREALM_MANAGED_LIST_HPP
定义: observation.hpp: 176
定义: list.hpp: 49
定义: mixed.hpp:69
定义: macros.hpp:286
定义: obj.hpp:62
定义: notifications.hpp:38
定义: results.hpp:411