Realm C++ SDK版本 v 2.2.0

managed_mixed.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_MIXED_HPP
20#define CPPREALM_MANAGED_MIXED_HPP
21
22#include <cpprealm/macros.hpp>
23#include <cpprealm/rbool.hpp>
24#include <cpprealm/types.hpp>
25
26命名空间域 {
27 template < typename T>
28 struct 托管 <T, std::enable_if_t< 域::internal::type_info::<T> MixedPersistableConcept::value>> : publicmanaged_base {
29 运用 托管 <T>::managed_base:: 操作符=;
30
31 枚举 storage_type {
32 Int = 0 ,
33 Bool = 1 ,
34 string = 2,
35二进制 = 4 ,
36混合 = 6 ,
37时间戳 = 8 ,
38浮点 = 9 ,
39 double = 10,
40 Decimal = 11 ,
41链接 = 12 ,
42 LinkList = 13 ,
43 ObjectId = 15 ,
44 TypedLink = 16,
45 UUID = 17,
46 Null = 18 ,
47 };
48
49 Managed & Operator =( const T& v) {
50 m_obj-> 设立(m_key, std::visit([]( auto && arg) {
51 using M = typenameInternal ::type_info::type_info <std::decay_t< decltype (arg)>>::internal_type;
52 return Internal::bridge::Mixed (M(arg));
53 }, v));
54 return * this ;
55 }
56
57 template < typename U>
58 托管 & 操作符 =( const U& v) {
59 m_obj->set(m_key, internal::bridge::mixed (v));
60 return * this ;
61 }
62
63 [[nodiscard]] Stored_type get_stored_type() const {
64 auto val = m_obj->get< realm::internal::bridge::mixed >(m_key);
65 if (val.is_null()) {
66 return Stored_Type::Null;
67 } else {
68 return static_cast< stored_type > (val.type());
69 }
70 }
71
72 [[nodiscard]] T 分离 () const {
73 return deserialize<T>(m_obj->get< realm::internal::bridge::mixed >(m_key));
74 }
75
76 [[nodiscard]] T操作符*() const {
77 return detach();
78 }
79
80 //MARK:-比较操作符
81 rbool 操作符==(const T& rhs) const noException {
82 if (this->m_rbool_query) {
83 return this->m_rbool_query->mixed_equal(m_key,serialize(rhs));
84 }
85 return detach() == rhs;
86 }
87
88 rbool操作符!=( const T& rhs) const noException {
89 if (this->m_rbool_query) {
90 return this->m_rbool_query->mixed_not_equal(m_key,serialize(rhs));
91 }
92 return detach() != rhs;
93 }
94
95 rbool 操作符==( const std::nullopt_t&) const noException {
96 if (this->m_rbool_query) {
97 return this->m_rbool_query->mixed_equal(m_key, internal::bridge::mixed (std::monostate()));
98 }
99 return detach() == T(std::monostate());
100 }
101
102 rbool操作符!=( const std::nullopt_t&) const noException {
103 if (this->m_rbool_query) {
104 return this->m_rbool_query->mixed_not_equal(m_key, internal::bridge::mixed (std::monostate()));
105 }
106 return detach() != T(std::monostate());
107 }
108
109 bool has_link() const {
110 return (get_stored_type() == Stored_type::TypedLink);
111 }
112
113 template < typename U>
114 typename 托管<U>::ref_type get_stored_link() const {
115 m_realm->read_group();
117
118 auto obj = Internal::bridge::Object (*m_realm, m.operatorInternal ::bridge::obj_link ());
119 uint 32 _t Alternative_key = m_realm->table_for_object_type( 托管 <std::remove_pointer_t<U>>:: 模式.name).get_key();
120 uint32_t stored_table = obj.get_obj().get_table().get_key();
121
122 if (alternative_key != Stored_table) {
123 throw std::runtime_error( "Different link type Stored in Mixed Type. Stored type: " + obj.get_object_schema().get_name());
124 }
125 return typename managed<U>::ref_type ( managed <std::remove_pointer_t<U>>(obj.get_obj(), *m_realm));
126 }
127
128 template < typename U>
129 void set_link(U &&v) {
130 static_assert ( sizeof ( managed<U> ), "Must describe schema for T" );
131 static_assert ( managed<U>::object_type == ObjectType::TopLevel, "Mixed properties can only store Top Level objects." );
132 auto class = m_realm->table_for_object_type( 托管<U>:: 模式 .name);
134 if constexpr ( managed<U>:: schema.HasPrimaryKeyProperty) {
135 auto pk = v.*( 托管<U>:: 模式 .primary_key().ptr);
136 o =table.create_object_with_primary_key( 域::internal::bridge::mixed (serialize(pk.value)));
137 } else {
138 o = Table.create_object();
139 }
140
141 std:: 应用([&o, &v, this ]( auto && ...p) {
142 ( accessor < typename std::decay_t< decltype (p)>::Result>::set(
143 o, o.get_table().get_column_key(p.name), *this->m_realm, v.*(std::decay_t< decltype (p)>::ptr)
144 ), ...);
145 }, 托管<U>:: 模式 .ps);
146 m_obj-> 设立(m_key, internal::bridge::mixed (o.get_link()));
147 }
148
149 template < typename U>
150 std::enable_if_t< 托管<U>::is_object && 托管<U>::object_type == ObjectType::TopLevel, void >
151 set_link( 托管<U> & link) {
152 m_obj-> 设立(m_key, internal::bridge::mixed ( internal::bridge::obj_link (link.m_obj.get_table().get_key(), link.m_obj.get_key())));
153 }
154
155 private :
156 托管() = 默认;
157 托管 ( const 托管 &) = delete ;
158 托管 ( 托管 &&) = delete ;
159 托管 & 操作符=( const 托管 &) = delete ;
160 托管 & 操作符=( 托管 &&) = delete ;
161 template < typename , typename >
162 朋友 struct 托管
163 };
164
165 template < typename T, typename Types>
166 boolholds_alternative ( const 域:: 托管<Types> & v) noException {
167 auto val = v.get_stored_type();
168 switch (val) {
170 if constexpr (std::is_same_v<T, int64_t>)
171 return true
172 中断
174 if constexpr (std::is_same_v<T, bool>)
175 return true
176 中断
178 if constexpr (std::is_same_v<T, std::string>)
179 return true
180 中断
182 if constexpr (std::is_same_v<T, std::vector<uint8_t>>)
183 return true
184 中断
186 if constexpr (std::is_same_v<T, Types>)
187 return true
188 中断
190 if constexpr (std::is_same_v<T, std::chrono::time_point<std::chrono::system_clock>>)
191 return true
192 中断
194 if constexpr (std::is_same_v<T, float>)
195 return true
196 中断
198 if constexpr (std::is_same_v<T, double>)
199 return true
200 中断
202 if constexpr (std::is_same_v<T, realm::decimal 128 >)
203 return true
204 中断
206 if constexpr (std::is_same_v<T, realm::object_id>)
207 return true
208 中断
209
211 if constexpr (std::is_same_v<T, realm::uuid>)
212 return true
213 中断
215 if constexpr (std::is_same_v<T, std::monostate>)
216 return true
217 中断
219 if constexpr (std::is_pointer_v<T>) {
220 auto m = v.m_obj->template get<internal::bridge::mixed>(v.m_key);
221 uint 32 _t Alternative_key = v.m_realm->table_for_object_type( managed <std::remove_pointer_t<T>>::schema.name).get_key();
222 uint 32 _tstored_key= internal::bridge:: 对象 (*v.m_realm, m。 操作符 ::bridge::obj_link ()).get_object_schema().table_key();
223 returnternal_key ==stored_key;
224 }
225 中断
226 }
229 中断
230 默认值
231 中断
232 }
233
234 return false
235 }
236 }
237
238#endif//CPREALM_MANAGED_MIXED_HPP
定义: rbool.hpp:36
定义: accessors.hpp:33
定义: mixed.hpp:69
定义: obj.hpp:123
定义: object.hpp: 154
定义: macros.hpp:286
定义: obj.hpp:62