Realm C++ SDK Version v2.2.0

observation.hpp

1
2//
3// Copyright 2022 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_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/object.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
34namespace realm {
35
36 template<typename T>
38 using underlying = T;
40 const T *object;
42 bool is_deleted = false;
50 std::exception_ptr error;
54 std::vector<PropertyChange<T>> property_changes;
55 };
56
57 template<typename T>
60 const T *obj,
61 std::shared_ptr<internal::bridge::object> internal_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 object;
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 void populate_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 table = m_object->get_obj().get_table();
87
88 for (auto i = 0; i < std::tuple_size<decltype(T::schema.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::schema)::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 return values;
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(&object,
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 void forward_change(const T *ptr,
141 std::vector<std::string> property_names,
142 std::vector<typename decltype(T::schema)::variant_t> old_values,
143 std::vector<typename decltype(T::schema)::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> property;
159 property.name = property_names[i];
160 if (!old_values.empty()) {
161 property.old_value = old_values[i];
162 }
163 if (!new_values.empty()) {
164 property.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<uint64_t> deletions;
178 std::vector<uint64_t> insertions;
179 std::vector<uint64_t> modifications;
180
181 // This flag indicates whether the underlying object which is the source of this
182 // collection was deleted. This applies to lists, dictionaries and sets.
183 // This enables notifiers to report a change on empty collections that have been deleted.
184 bool collection_root_was_deleted = false;
185
186 [[nodiscard]] bool empty() const noexcept {
187 return deletions.empty() && insertions.empty() && modifications.empty() &&
188 !collection_root_was_deleted;
189 }
190 };
191
193 std::function<void(collection_change)> handler;
194 bool ignoreChangesInInitialNotification;
195
196 collection_callback_wrapper(std::function<void(collection_change)> handler,
197 bool ignoreChangesInInitialNotification)
198 : handler(handler)
199 , ignoreChangesInInitialNotification(ignoreChangesInInitialNotification)
200 {}
201
202
203 void before(const realm::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<uint64_t> to_vector(const internal::bridge::index_set& index_set) {
223 auto vector = std::vector<uint64_t>();
224 for (auto index : index_set.as_indexes()) {
225 vector.push_back(index);
226 }
227 return vector;
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 (object.property.old_value) {
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 }, *object.property.old_value);
245 }
246 if (object.property.new_value) {
247 stream << "\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 return stream;
255}
256
257#endif //CPPREALM_OBSERVATION_HPP
Definition: notifications.hpp:80
std::string name
Definition: notifications.hpp:84
Definition: observation.hpp:192
Definition: observation.hpp:176
Definition: object.hpp:63
Definition: observation.hpp:58
Definition: observation.hpp:37
bool is_deleted
The object has been deleted from the Realm.
Definition: observation.hpp:42
std::vector< PropertyChange< T > > property_changes
Definition: observation.hpp:54
const T * object
The object being observed.
Definition: observation.hpp:40
std::exception_ptr error
Definition: observation.hpp:50
Definition: flex_sync.hpp:37