Realm C++ SDK版本 v 2.2.0

db.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_DB_HPP
20#define CPPREALM_DB_HPP
21
22#include <cpprealm/accessors.hpp>
23#include <cpprealm/macros.hpp>
24#include <cpprealm/results.hpp>
25#include <cpprealm/scheduler.hpp>
26#include <cpprealm/schema.hpp>
27#include <cpprealm/types.hpp>
28
29#include <cpprealm/internal/bridge/sync_session.hpp>
30#include <cpprealm/internal/bridge/thread_safe_reference.hpp>
31#include <cpprealm/internal/bridge/sync_session.hpp>
32
33#include <文件系统>
34#include<optional>
35#include <string>
36#include <utility>
37
38命名空间域 {
39 命名空间{
40 template < typename T>
41 using is_Optional =Internal::Type_Info::is_Optional<T>;
42 }
43 命名空间schemagen {
44 template < typename Class, typename 属性>
45 结构模式;
46 template <auto Ptr, bool IsPrimaryKey>
47 struct属性;
48 }
49
50 using sync_config =Internal::bridge::域::sync_config;
51 using db_config = external::bridge::域::config;
52 using sync_session =Internal::bridge::sync_session;
53 using sync_error =Internal::bridge::sync_error;
54
55 struct sync_subscription_set;
56
57 template < typename T>
58 structthread_safe_reference
59 }
60
61命名空间域 {
62
63 struct db {
64 静态 inline std::vector<internal::bridge::object_schema> schemas;
65 Internal::bridge:: Realm m_realm;
66 显式 db ( realm::db_config config)
67 {
68 if (!config.get_schema())
69 config.set_schema(db::schemas);
70 m_realm = external ::bridge:: 域 (config);
71 }
72
73 void begin_write() const { m_realm.begin_transaction(); }
74 void commit_write() const { m_realm.commit_transaction(); }
75
76 template < typename Fn>
77 std::invoke_result_t<Fn> 写入(Fn&& fn) const {
78 begin_write();
79 if constexpr (!std::is_void_v<std::invoke_result_t<Fn>>) {
80 auto val = fn();
81 commit_write();
82 return val;
83 } else {
84 fn();
85 commit_write();
86 }
87 }
88 template < typename U>
90 using T = std::remove_const_t<U>;
91 static_assert ( sizeof ( 托管<T> ), "Must describe schema for T" );
92 auto tab = m_realm.table_for_object_type( managed <std::remove_const_t<T>>::schema.name);
94 if constexpr ( managed<std::remove_const_t<T> >::schema.HasPrimaryKeyProperty) {
95 auto pk = v.*( 托管<std::remove_const_t<T> >::schema.primary_key().ptr);
96 m_obj = Table.create_object_with_primary_key( 域::internal::bridge::mixed (serialize(pk.value)));
97 } else {
98 m_obj = table.create_object();
99 }
100
101 std:: 应用([&m_obj, &v, this ]( auto && ...p) {
102 ( accessor < typename std::decay_t< decltype (p)>::Result>::set(
103 m_obj, m_obj.get_table().get_column_key(p.name), m_realm, v.*(std::decay_t< decltype (p)>::ptr)
104 ), ...);
105 }, 托管<T>:: 模式 .ps);
106 return 托管<T> (std::move(m_obj), m_realm);
107 }
108 template < typename T>
109 void remove(T& 对象 )
110 {
111 auto tab = m_realm.table_for_object_type(T::schema.name);
112 table.remove_object( 对象 .m_obj.get_key());
113 }
114 template < typename T>
115 void insert( const std::vector<T> &v) {
116 static_assert ( sizeof ( 托管<T> ), "Must describe schema for T" );
117 Internal::bridge::table Table = Realm.table_for_object_type( 托管<T>::schema .name);
118 for ( auto & obj : v) {
119 Internal::bridge::obj m_obj;
120 if constexpr ( 托管<T>:: schema.HasPrimaryKeyProperty) {
121 auto pk = obj.*( managed<T>::schema .primary_key().ptr);
122 m_obj = Table.create_object_with_primary_key( 域::internal::bridge::mixed (serialize(pk.value)));
123 } else {
124 m_obj = table.create_object();
125 }
126 std:: 应用([&m_obj, &obj]( auto && ...p) {
127 ( accessor < typename std::decay_t< decltype (p)>::Result>::set(
128 m_obj, m_obj.get_table().get_column_key(p.name), obj.*(std::decay_t< decltype (p)>::ptr)
129 ), ...);
130 }, 托管<T>:: 模式 .ps);
131 }
132 }
133
134 private :
135 template < size_t N, typename Tpl, typename ...Ts> auto v_add( const Tpl& tpl, const std::tuple<Ts...>& vs) {
136 if constexpr (N + 1 == sizeof ...(Typescript)) {
137 auto 托管 = add(std::move(std::get<N>(vs)));
138 return std::tuple_cat(tpl, std::make_tuple(std::move( 托管 )));
139 } else {
140 auto 托管 = add(std::move(std::get<N>(vs)));
141 return v_add<N + 1>(std::tuple_cat(tpl, std::make_tuple(std::move( 托管 ))), vs);
142 }
143 }
144 public :
145 template < typename ...Ts>
146 std::tuple<managed<Ts>...> insert(Ts&&... v) {
147 std::tuple<> tpl;
148 return v_add<0>(tpl, std::make_tuple(v...));
149 }
150 template < typename T>
151 Results<T> 对象()
152 {
153 return 结果<T> ( internal::bridge::results (Realm, Realm.table_for_object_type( 托管<T>::schema .name)));
154 }
155
156 [[maybe_unused]] bool refresh()
157 {
158 return m_realm.refresh();
159 }
160
162
163 std::Optional<sync_session> get_sync_session() const {
164 return m_realm.get_sync_session();
165 }
166
167 template < typename T>
168 托管<T> resolve( thread_Safe_reference<T> &&tsr)
169 {
170 auto object =Internal::bridge::resolve<internal::bridge::object>(m_realm, std::move(tsr.m_tsr));
171 Internal::bridge::obj m_obj = object .get_obj();
172 return 托管<T> (std::move(m_obj), m_realm);
173 }
174
175 bool is_frozen() const ;
176 db Freeze();
177 db thaw();
178 void invalidate();
179 void close();
180 bool is_close();
181 Friend struct::域:: thread_Safe_reference <db>;
182 template < typename , typename > Friend struct 托管 ;
183 template < typename T>
184 朋友 voidinternal ::bridge::域::config::set_client_reset_handler( const client_reset_mode_base<T>&);
185 private :
187 {
188 m_realm = std::move(r);
189 }
190 db ( constInternal ::bridge::域 & R)
191 {
192 m_realm = r;
193 }
194 };
195
196 bool 操作符==(const db&, const db&);
197 bool操作符!=( const db &, const db &);
198
199 template < typename ...Ts>
200 内联 db open( const db_config & config) {
201 auto config_copy = config;
202 if constexpr ( sizeof ...(Typescript) == 0) {
203 config_copy.set_schema(db::schemas);
204 } else {
205 std::vector<internal::bridge::object_schema> 模式;
206 (模式.push_back(托管<Ts>:: 模式.to_core_schema()), ...);
207 config_copy.set_schema(模式);
208 }
209 return db(config_copy);
210 }
211 template < typename ...Ts>
212 [[deprecated( "This function is deprecated and is returned by `db::open(const db_config& config)`." )]]
213 inline db open( const std::string& path, const std::shared_ptr<scheduler>& scheduler) {
214 return open<Ts...>(db_config(path, scheduler));
215 }
216
217 template < typename T>
218 inline std::ostream& 操作符<< (std::ostream& stream, const T*& 对象 )
219 {
220 stream << "link:" << object << std::endl;
221 返回流;
222 }
223 }
224
225#endif //CPREALM_DB_HPP
定义: accessors.hpp:33
Definition: db.hpp:63
定义: mixed.hpp:69
定义: obj.hpp:123
定义: realm.hpp: 67
定义: results.hpp:46
定义: table.hpp:40
定义: obj.hpp:62
定义: results.hpp:411
定义: Flex_sync.hpp:150
定义: thread_safe_reference.hpp:27