19#ifndef CPPREALM_BRIDGE_LINK_HPP
20#define CPPREALM_BRIDGE_LINK_HPP
22#include <cpprealm/accessors.hpp>
23#include <cpprealm/macros.hpp>
24#include <cpprealm/rbool.hpp>
26#include <cpprealm/internal/type_info.hpp>
29 template <
typename,
typename>
36 managed<T> m(m_obj->is_null(m_key) ? *m_obj : m_obj->get_linked_object(m_key), *m_realm);
37 auto assign = [&m, &v](
auto& pair) {
38 (*v).*(std::decay_t<
decltype(pair.first)>::ptr) = (m.*(pair.second)).detach();
41 std::apply([&v, &m, &assign](
auto && ...pair) {
48 explicit ref_type(
managed<T>&& value) : m_managed(std::move(value)) { }
64 if (this->m_managed.m_realm != *rhs.m_realm) {
67 return this->m_managed.m_obj.get_key() == rhs.m_obj->get_key();
69 bool operator ==(
const managed<T>& rhs)
const {
70 if (this->m_managed.m_realm != rhs.m_realm) {
73 return this->m_managed.m_obj.get_table() == rhs.m_obj.get_table() &&
74 this->m_managed.m_obj.get_key() == rhs.m_obj.get_key();
76 bool operator ==(
const ref_type& rhs)
const {
77 if (this->m_managed.m_realm != rhs.m_managed.m_realm) {
80 return this->m_managed.m_obj.get_table() == rhs.m_managed.m_obj.get_table() &&
81 this->m_managed.m_obj.get_key() == rhs.m_managed.m_obj.get_key();
84 return !this->operator==(rhs);
86 bool operator !=(
const managed<T>& rhs)
const {
87 return !this->operator==(rhs);
89 bool operator !=(
const ref_type& rhs)
const {
90 return !this->operator==(rhs);
95 ref_type operator ->()
const {
96 if (this->m_rbool_query) {
97 this->m_rbool_query->add_link_chain(m_key);
100 return ref_type(
managed<T>(m_obj->get_linked_object(m_key), *m_realm));
102 operator bool()
const {
103 if (m_obj && m_key) {
104 return !m_obj->is_null(m_key);
108 managed &operator=(
const managed<T>& obj) {
109 m_obj->set(m_key, obj.m_obj.get_key());
112 managed &operator=(
const managed<T*> &obj) {
113 m_obj->set(m_key, obj.m_obj->get_key());
116 managed &operator=(std::nullptr_t) {
117 m_obj->set_null(m_key);
120 managed &operator=(T* o) {
121 auto table = m_realm->table_for_object_type(managed<T>::schema.name);
122 internal::bridge::obj obj;
124 m_obj->set_null(m_key);
126 }
else if constexpr (managed<T>::schema.HasPrimaryKeyProperty) {
127 auto pk = (*o).*(managed<T>::schema.primary_key().ptr);
129 m_obj->set(m_key, obj.get_key());
130 }
else if (managed<T>::schema.is_embedded()) {
131 obj = m_obj->create_and_set_linked_object(m_key);
133 obj = table.create_object();
134 m_obj->set(m_key, obj.get_key());
137 std::apply([&obj, &o, realm = *m_realm](
auto && ...p) {
138 (accessor<
typename std::decay_t<
decltype(p)>::Result>::set(
139 obj, obj.get_table().get_column_key(p.name), realm, (*o).*(std::decay_t<
decltype(p)>::ptr)
141 }, managed<T>::schema.ps);
145 rbool operator ==(std::nullptr_t)
const {
147 return m_rbool_query->link_equal(m_key, std::nullopt);
149 return !m_obj->get_linked_object(m_key).is_valid();
151 rbool operator ==(
const managed<T>& rhs)
const {
153 return m_rbool_query->link_equal(m_key, rhs.m_obj);
155 if (*this->m_realm != rhs.m_realm)
157 return m_obj->get_linked_object(m_key).get_key() == rhs.m_obj.get_key();
160 rbool operator ==(
const managed<T*>& rhs)
const {
162 return m_rbool_query->link_equal(m_key, *rhs.m_obj);
164 if (*this->m_realm != *rhs.m_realm)
166 return m_obj->get_linked_object(m_key).get_key() == rhs.m_obj->get_key();
169 rbool operator !=(std::nullptr_t)
const {
171 return m_rbool_query->link_not_equal(m_key, std::nullopt);
173 return m_obj->get_linked_object(m_key).is_valid();
175 rbool operator !=(
const managed<T>& rhs)
const {
177 return m_rbool_query->link_not_equal(m_key, rhs.m_obj);
179 return m_obj->get_linked_object(m_key).is_valid();
182 rbool operator !=(
const managed<T*>& rhs)
const {
184 return m_rbool_query->link_not_equal(m_key, *rhs.m_obj);
186 return m_obj->get_linked_object(m_key).is_valid();
190 managed<T*>() =
default;
191 template<
typename,
typename>
192 friend struct managed;
Definition: macros.hpp:286