19#ifndef CPPREALM_DB_HPP
20#define CPPREALM_DB_HPP
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>
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>
41 using is_optional = internal::type_info::is_optional<T>;
44 template <
typename Class,
typename ...Properties>
46 template <auto Ptr,
bool IsPrimaryKey>
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;
55 struct sync_subscription_set;
58 struct thread_safe_reference;
64 static inline std::vector<internal::bridge::object_schema> schemas;
68 if (!config.get_schema())
69 config.set_schema(db::schemas);
73 void begin_write()
const { m_realm.begin_transaction(); }
74 void commit_write()
const { m_realm.commit_transaction(); }
76 template <
typename Fn>
77 std::invoke_result_t<Fn> write(Fn&& fn)
const {
79 if constexpr (!std::is_void_v<std::invoke_result_t<Fn>>) {
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);
98 m_obj = table.create_object();
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)
108 template <
typename T>
109 void remove(T&
object)
111 auto table = m_realm.table_for_object_type(T::schema.name);
112 table.remove_object(
object.m_obj.get_key());
114 template <
typename T>
115 void insert(
const std::vector<T> &v) {
116 static_assert(
sizeof(
managed<T>),
"Must declare schema for T");
118 for (
auto& obj : v) {
124 m_obj = table.create_object();
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)
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)));
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);
145 template <
typename ...Ts>
146 std::tuple<managed<Ts>...> insert(Ts&&... v) {
148 return v_add<0>(tpl, std::make_tuple(v...));
150 template <
typename T>
156 [[maybe_unused]]
bool refresh()
158 return m_realm.refresh();
163 std::optional<sync_session> get_sync_session()
const {
164 return m_realm.get_sync_session();
167 template <
typename T>
170 auto object = internal::bridge::resolve<internal::bridge::object>(m_realm, std::move(tsr.m_tsr));
175 bool is_frozen()
const;
181 friend struct ::realm::thread_safe_reference<db>;
182 template <
typename,
typename>
friend struct managed;
184 friend void internal::bridge::realm::config::set_client_reset_handler(
const client_reset_mode_base<T>&);
188 m_realm = std::move(r);
196 bool operator==(
const db&,
const db&);
197 bool operator!=(
const db&,
const db&);
199 template <
typename ...Ts>
201 auto config_copy = config;
202 if constexpr (
sizeof...(Ts) == 0) {
203 config_copy.set_schema(db::schemas);
205 std::vector<internal::bridge::object_schema> schema;
206 (schema.push_back(managed<Ts>::schema.to_core_schema()), ...);
207 config_copy.set_schema(schema);
209 return db(config_copy);
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));
217 template <
typename T>
218 inline std::ostream& operator<< (std::ostream& stream,
const T*&
object)
220 stream <<
"link:" <<
object << std::endl;
Definition: accessors.hpp:33
Definition: results.hpp:46
Definition: results.hpp:411
Definition: flex_sync.hpp:150
Definition: thread_safe_reference.hpp:27