Realm C++ SDK版本 v 2.2.0
全部  功能 变量 Typedef 枚举 页面

观察.hpp

1
2//
3// 版权所有 2022 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_OBSERVATION_HPP
20#define CPPREALM_OBSERVATION_HPP
21
22#include <cpprealm/internal/bridge/list.hpp>
23#include <cpprealm/internal/bridge/obj.hpp>
24#include <cpprealm/internal/bridge/ 对象.hpp>
25#include <cpprealm/internal/bridge/realm.hpp>
26#include <cpprealm/macros.hpp>
27
28#include <cpprealm/internal/bridge/table.hpp>
29#include <cpprealm/internal/bridge/thread_safe_reference.hpp>
30
31#include <iostream>
32#include <variant>
33
34命名空间域 {
35
36 template < typename T>
38 using底层= T;
40 const T * 对象 ;
42 bool is_deleted = false ;
50 std::Exception_ptr error
54 std::vector<PropertyChange<T>> property_changes
55 };
56
57 template < typename T>
59 object_change_callback_wrapper (std::function< void ( object_change < T > )> &&b,
60 const T *obj,
61 std::shared_ptr<internal::bridge::object> 内部对象)
62 : block(std::move(b)), object (*obj), m_object(internal_object) {
63 static_cast<void> (obj);
64 }
65 std::function<void( object_change<T> )> block;
66 const T对象
67 std::shared_ptr<internal::bridge::object> m_object;
68
69 std::Optional<std::vector<std::string>> property_names = std::nullopt;
70 std::Optional<std::vector< typename decltype (T::schema)::variant_t>> old_values = std::nullopt;
71 bool Deleted = false ;
72
73 voidpopulate_properties ( internal::bridge::collection_change_set const &c) {
74 if (property_names) {
75 return ;
76 }
77 if (!c.deletions().empty()) {
78 Deleted = true ;
79 return ;
80 }
81 if (c.columns().empty()) {
82 return ;
83 }
84
85 auto properties = std::vector<std::string>();
86 auto tab = m_object->get_obj().get_table();
87
88 for ( auto i = 0 ; i < std::tuple_size< decltype (T:: 模式.properties)>{}; i++) {
89 if (c.columns().count(table.get_column_key(T::schema.names[i]).value())) {
90 properties.push_back(T::schema.names[i]);
91 }
92 }
93
94 if (!properties.empty()) {
95 property_names = properties;
96 }
97 }
98
99 std::Optional<std::vector< typename decltype (T:: 模式)::variant_t>>
100 read_values( internal::bridge::collection_change_set const &c) {
101 if (c.empty()) {
102 return std::nullopt;
103 }
104 populate_properties(c);
105 if (!property_names) {
106 return std::nullopt;
107 }
108
109 std::vector< typename decltype (T::schema)::variant_t> values;
110 for ( auto &name: *property_names) {
111 auto value = T::schema.property_value_for_name(name, object , true );
112 values.push_back(value);
113 }
114 返回值;
115 }
116
117 void before( internal::bridge::collection_change_set const &c) override {
118 old_values = read_values(c);
119 }
120
121 void after( internal::bridge::collection_change_set const &c) override {
122 auto new_values = read_values(c);
123 if (deleted) {
124 forward_change( nullptr , {}, {}, {}, nullptr );
125 } else if (new_values) {
126 forward_change(& 对象 ,
127 *property_names,
128 old_values ? *old_values : std::vector< typename decltype (T::schema)::variant_t>{},
129 *new_values,
130 nullptr);
131 }
132 property_names = std::nullopt;
133 old_values = std::nullopt;
134 }
135
136 void error(std::Exception_ptr err) {
137 forward_change( nullptr , {}, {}, {}, err);
138 }
139
140 voidforward_change ( const T *ptr,
141 std::vector<std::string> property_names,
142 std::vector< typename decltype (T:: 模式)::variant_t> old_values,
143 std::vector< typename decltype (T:: 模式)::variant_t> new_values,
144 const std::Exception_ptr &error) {
145 if (!ptr) {
146 if (error) {
147 auto oc = object_change<T> ();
148 oc.error = error;
149 block(std::forward< realm::object_change<T> >(std::move(oc)));
150 } else {
151 auto oc = object_change<T> ();
152 oc.is_deleted = true ;
153 block(std::forward< realm::object_change<T> >(std::move(oc)));
154 }
155 } else {
156 std::vector<PropertyChange<T>> property_changes;
157 for ( size_t i = 0 ; i < property_names.size(); i++) {
158 PropertyChange<T> 属性;
159 属性name = property_names[i];
160 if (!old_values.empty()) {
161 属性.old_value = old_values[i];
162 }
163 if (!new_values.empty()) {
164 属性.new_value = new_values[i];
165 }
166 property_changes.push_back(std::move(property));
167 }
168 auto oc = object_change<T> ();
169 oc.object = ptr;
170 oc.property_changes = property_changes;
171 block(std::forward< realm::object_change<T> >(std::move(oc)));
172 }
173 }
174 };
175
177 std::vector<uint 64 _t> deletes;
178 std::vector<uint 64 _t> 插入;
179 std::vector<uint 64 _t> 修改;
180
181 // 此标志指示作为此值来源的底层对象是否
182 // 集合已删除。 这适用于列表、字典和集合。
183 // 这使通知程序能够报告已删除的空集合的更改。
184 bool collection_root_was_deleted = false ;
185
186 [[nodiscard]] bool empty() const noException {
187 return deletements.empty() && inserts.empty() && Modifications.empty() &&
188 !collection_root_was_deleted;
189 }
190 };
191
193 std::function<void( collection_change )> handler;
194 boolignoreChangesInInitialNotification ;
195
196 collection_callback_wrapper (std::function< void ( collection_change )> handler,
197 boolignoreChangesInInitialNotification )
198 : handler(handler)
199 、ignoreChangesInInitialNotification(ignoreChangesInInitialNotification)
200 {}
201
202
203 void before( const 域::internal::bridge::collection_change_set &) final {}
204 void after( internal::bridge::collection_change_set const &changes) final {
205 if (ignoreChangesInInitialNotification) {
206 ignoreChangesInInitialNotification = false ;
207 handler({{},{},{}});
208 }
209 else if (changes.empty()) {
210 handler({{},{},{}});
211
212 }
213 else if (!changes.collection_root_was_deleted() || !changes.deletions().empty()) {
214 handler({to_vector(changes.deletions()),
215 to_vector(changes.insertions()),
216 to_vector(changes.modifications()),
217 });
218 }
219 }
220
221 private :
222 std::vector<uint 64 _t> to_vector( const inside ::bridge::index_set & index_set) {
223 auto vector = std::vector<uint 64 _t>();
224 for ( auto index : index_set.as_indexes()) {
225 vector.push_back(index);
226 }
227 返回向量;
228 };
229 };
230 }
231
232
233template < typename T>
234inline std::ostream& Operator<< (std::ostream& stream, const realm::object_change<T> & object )
235 {
236 stream << "{\n" ;
237 stream << "\tis_deleted: " << object .is_deleted << "\n" ;
238 stream << "\tchange_to: " << object .property.name << "\n" ;
239 if ( 对象 ) {
240 stream << "\told_value: " ;
241 std::visit([&stream]( auto && arg) {
242 using M = std::decay_t< decltype (arg)>;
243 stream << static_cast<M>(arg);
244 }, * 对象 .属性.old_value);
245 }
246 if ( 对象 ) {
247 流 << "\tnew_value: " ;
248 std::visit([&stream]( auto && arg) {
249 using M = std::decay_t< decltype (arg)>;
250 stream << static_cast<M>(arg);
251 }, * object .property.new_value);
252 }
253 stream << "\n}" ;
254 返回流;
255 }
256
257#endif //CPREALM_OBSERVATION_HPP
定义: notifications.hpp: 80
std::string name
定义: notifications.hpp: 84
定义: observation.hpp: 192
定义: observation.hpp: 176
定义: object.hpp: 63
定义: observation.hpp: 58
定义: observation.hpp: 37
bool is_deleted
该对象已从 Realm 中删除。
定义: observation.hpp: 42
std::vector< PropertyChange< T >> property_changes
定义: observation.hpp: 54
const T * 对象
被观察的对象。
定义: observation.hpp: 40
std::exception_ptr error
定义: observation.hpp: 50
定义: Flex_sync.hpp: 37