19#ifndef CPPREALM_MANAGED_SET_HPP
20#define CPPREALM_MANAGED_SET_HPP
22#include <cpprealm/macros.hpp>
23#include <cpprealm/notifications.hpp>
24#include <cpprealm/observation.hpp>
25#include <cpprealm/types.hpp>
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=;
40 using difference_type = std::ptrdiff_t;
43 using iterator_category = std::forward_iterator_tag;
45 bool operator!=(
const iterator& other)
const
47 return !(*
this == other);
50 bool operator==(
const iterator& other)
const
52 return (m_parent == other.m_parent) && (m_i == other.m_i);
55 T operator*()
const noexcept
58 return deserialize<T>(s.get_any(m_i));
61 iterator& operator++()
67 const iterator& operator++(
int i)
73 template<
typename,
typename>
76 iterator(
size_t i,
managed<std::set<T>>* parent)
77 : m_i(i), m_parent(parent)
85 return iterator(0,
this);
90 return iterator(size(),
this);
92 [[nodiscard]] std::set<T> detach()
const {
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)));
102 auto set = std::make_shared<realm::internal::bridge::set>(*m_realm, *m_obj, m_key);
104 std::make_shared<realm::collection_callback_wrapper>(
107 token.m_realm = *m_realm;
112 void erase(
const iterator& it)
114 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
115 set.remove(serialize(*it));
118 std::pair<iterator, bool> insert(
const T& v)
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);
125 std::pair<size_t, bool> res = set.insert(v);
126 return std::pair<iterator, bool>(iterator(res.first,
this), res.second);
130 iterator insert(
const iterator&,
const T& v)
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);
137 iterator find(
const T& v)
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);
146 internal::bridge::set(*m_realm, *m_obj, m_key).remove_all();
151 return internal::bridge::set(*m_realm, *m_obj, m_key).size();
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;
166 using managed<std::set<T*>>::managed_base::operator=;
173 using difference_type = std::ptrdiff_t;
175 using reference = T&;
176 using iterator_category = std::forward_iterator_tag;
178 bool operator!=(
const iterator& other)
const
180 return !(*
this == other);
183 bool operator==(
const iterator& other)
const
185 return (m_parent == other.m_parent) && (m_i == other.m_i);
191 managed<T> m(s.get_obj(m_i), *m_parent->m_realm);
192 return {std::move(m)};
195 iterator& operator++()
201 const iterator& operator++(
int i)
207 template<
typename,
typename>
210 iterator(
size_t i,
managed<std::set<T*>>* parent)
211 : m_i(i), m_parent(parent)
219 return iterator(0,
this);
224 return iterator(size(),
this);
226 [[nodiscard]] std::set<T*> detach()
const {
228 size_t count = s.size();
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);
235 auto assign = [&m, &v](
auto& pair) {
236 (*v).*(std::decay_t<
decltype(pair.first)>::ptr) = (m.*(pair.second)).detach();
238 auto zipped = internal::zip_tuples(managed<T>::schema.ps, managed<T>::managed_pointers());
239 std::apply([&v, &m, &assign](
auto && ...pair) {
249 auto set = std::make_shared<realm::internal::bridge::set>(*m_realm, *m_obj, m_key);
251 std::make_shared<realm::collection_callback_wrapper>(
254 token.m_realm = *m_realm;
259 void erase(
const iterator& it)
261 auto set = internal::bridge::set(*m_realm, *m_obj, m_key);
262 set.remove(it.operator*().m_obj.get_key());
265 std::pair<iterator, bool> insert(T* value)
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);
274 m_obj = table.create_object();
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());
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);
288 iterator insert(
const iterator&, T* value)
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);
297 m_obj = table.create_object();
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);
308 std::pair<iterator, bool> insert(
const managed<T>& value)
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);
316 iterator insert(
const iterator&,
const managed<T>& value)
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);
323 std::pair<iterator, bool> insert(
const managed<T*>& value)
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);
331 iterator insert(
const iterator&,
const managed<T*>& value)
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);
338 iterator find(
const managed<T>& v)
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);
347 iterator find(
const managed<T*>& v)
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);
356 internal::bridge::set(*m_realm, *m_obj, m_key).remove_all();
361 return internal::bridge::set(*m_realm, *m_obj, m_key).size();
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;
Definition: observation.hpp:176
Definition: macros.hpp:286
Definition: notifications.hpp:38