Realm C++ SDK Version v2.2.0

db.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_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 <filesystem>
34#include <optional>
35#include <string>
36#include <utility>
37
38namespace realm {
39 namespace {
40 template<typename T>
41 using is_optional = internal::type_info::is_optional<T>;
42 }
43 namespace schemagen {
44 template <typename Class, typename ...Properties>
45 struct schema;
46 template <auto Ptr, bool IsPrimaryKey>
47 struct property;
48 }
49
50 using sync_config = internal::bridge::realm::sync_config;
51 using db_config = internal::bridge::realm::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 struct thread_safe_reference;
59}
60
61namespace realm {
62
63 struct db {
64 static inline std::vector<internal::bridge::object_schema> schemas;
66 explicit db(realm::db_config config)
67 {
68 if (!config.get_schema())
69 config.set_schema(db::schemas);
70 m_realm = internal::bridge::realm(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> write(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(managed<T>), "Must declare schema for T");
92 auto table = 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.*(managed<std::remove_const_t<T>>::schema.primary_key().ptr);
96 m_obj = table.create_object_with_primary_key(realm::internal::bridge::mixed(serialize(pk.value)));
97 } else {
98 m_obj = table.create_object();
99 }
100
101 std::apply([&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 }, managed<T>::schema.ps);
106 return managed<T>(std::move(m_obj), m_realm);
107 }
108 template <typename T>
109 void remove(T& object)
110 {
111 auto table = m_realm.table_for_object_type(T::schema.name);
112 table.remove_object(object.m_obj.get_key());
113 }
114 template <typename T>
115 void insert(const std::vector<T> &v) {
116 static_assert(sizeof(managed<T>), "Must declare schema for T");
117 internal::bridge::table table = m_realm.table_for_object_type(managed<T>::schema.name);
118 for (auto& obj : v) {
120 if constexpr (managed<T>::schema.HasPrimaryKeyProperty) {
121 auto pk = obj.*(managed<T>::schema.primary_key().ptr);
122 m_obj = table.create_object_with_primary_key(realm::internal::bridge::mixed(serialize(pk.value)));
123 } else {
124 m_obj = table.create_object();
125 }
126 std::apply([&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 }, managed<T>::schema.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...(Ts)) {
137 auto managed = add(std::move(std::get<N>(vs)));
138 return std::tuple_cat(tpl, std::make_tuple(std::move(managed)));
139 } else {
140 auto managed = add(std::move(std::get<N>(vs)));
141 return v_add<N + 1>(std::tuple_cat(tpl, std::make_tuple(std::move(managed))), 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> objects()
152 {
153 return results<T>(internal::bridge::results(m_realm, m_realm.table_for_object_type(managed<T>::schema.name)));
154 }
155
156 [[maybe_unused]] bool refresh()
157 {
158 return m_realm.refresh();
159 }
160
161 ::realm::sync_subscription_set subscriptions();
162
163 std::optional<sync_session> get_sync_session() const {
164 return m_realm.get_sync_session();
165 }
166
167 template <typename T>
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 managed<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_closed();
181 friend struct ::realm::thread_safe_reference<db>;
182 template <typename, typename> friend struct managed;
183 template<typename T>
184 friend void internal::bridge::realm::config::set_client_reset_handler(const client_reset_mode_base<T>&);
185 private:
187 {
188 m_realm = std::move(r);
189 }
191 {
192 m_realm = r;
193 }
194 };
195
196 bool operator==(const db&, const db&);
197 bool operator!=(const db&, const db&);
198
199 template <typename ...Ts>
200 inline db open(const db_config& config) {
201 auto config_copy = config;
202 if constexpr (sizeof...(Ts) == 0) {
203 config_copy.set_schema(db::schemas);
204 } else {
205 std::vector<internal::bridge::object_schema> schema;
206 (schema.push_back(managed<Ts>::schema.to_core_schema()), ...);
207 config_copy.set_schema(schema);
208 }
209 return db(config_copy);
210 }
211 template <typename ...Ts>
212 [[deprecated("This function is deprecated and is replaced 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& operator<< (std::ostream& stream, const T*& object)
219 {
220 stream << "link:" << object << std::endl;
221 return stream;
222 }
223}
224
225#endif //CPPREALM_DB_HPP
Definition: accessors.hpp:33
Definition: db.hpp:63
Definition: mixed.hpp:69
Definition: obj.hpp:123
Definition: realm.hpp:67
Definition: results.hpp:46
Definition: table.hpp:40
Definition: obj.hpp:62
Definition: results.hpp:411
Definition: flex_sync.hpp:150
Definition: thread_safe_reference.hpp:27