19#ifndef CPPREALM_ACCESSORS_HPP
20#define CPPREALM_ACCESSORS_HPP
22#include <cpprealm/internal/bridge/lnklst.hpp>
23#include <cpprealm/internal/bridge/obj.hpp>
24#include <cpprealm/internal/bridge/realm.hpp>
25#include <cpprealm/internal/bridge/table.hpp>
26#include <cpprealm/types.hpp>
32 template <
typename T,
typename =
void>
45 const int64_t& value) {
54 const std::optional<int64_t>& value) {
67 const double& value) {
76 const std::optional<double>& value) {
98 const std::optional<bool>& value) {
100 obj.set(key, *value);
108 template <
typename T>
109 struct accessor<T, std::enable_if_t<std::is_enum_v<T>>> {
114 obj.set(key,
static_cast<int64_t
>(value));
117 template <
typename T>
118 struct accessor<T, std::enable_if_t<std::conjunction_v<internal::type_info::is_optional<T>, std::is_enum<typename T::value_type>>>> {
124 obj.set(key,
static_cast<int64_t
>(*value));
131 template <
typename T>
132 struct accessor<T, std::enable_if_t<internal::type_info::MixedPersistableConcept<T>::value>> {
137 obj.set(key, serialize(value));
156 const std::optional<uuid>& value) {
158 obj.set(key, *value);
178 const std::optional<object_id>& value) {
180 obj.set(key, *value);
201 const std::optional<decimal128>& value) {
203 obj.set(key, *value);
211 struct accessor<std::chrono::time_point<std::chrono::system_clock>> {
215 const std::chrono::time_point<std::chrono::system_clock>& value) {
220 struct accessor<std::optional<std::chrono::time_point<std::chrono::system_clock>>> {
224 const std::optional<std::chrono::time_point<std::chrono::system_clock>>& value) {
226 obj.set(key, *value);
238 const std::string& value) {
247 const std::optional<std::string>& value) {
249 obj.set(key, *value);
261 const std::vector<uint8_t>& value) {
266 struct accessor<std::optional<std::vector<uint8_t>>> {
270 const std::optional<std::vector<uint8_t>>& value) {
272 obj.set(key, *value);
278 template <
typename T>
283 const std::vector<T>& value) {
284 obj.set_list_values(key, value);
287 template <
typename T>
292 const std::vector<T*>& value) {
293 auto list = obj.get_linklist(key);
294 for (
size_t i = 0; i < value.size(); i++) {
295 auto& lnk = value[i];
299 auto table = obj.get_target_table(key);
305 m_obj = list.create_and_insert_linked_object(i);
307 m_obj = table.create_object();
309 std::apply([&m_obj, &lnk, &realm](
auto && ...p) {
310 (
accessor<
typename std::decay_t<
decltype(p)>::Result>::set(
311 m_obj, m_obj.get_table().get_column_key(p.name), realm,
312 (*lnk).*(std::decay_t<
decltype(p)>::ptr)), ...);
315 list.add(m_obj.get_key());
320 template <
typename T>
325 const std::set<T>& value) {
327 for (
const auto& v : value) {
328 set.insert(serialize(v));
332 template <
typename T>
337 const std::set<T*>& value) {
339 for (
const auto& lnk : value) {
343 auto table = obj.get_target_table(key);
349 m_obj = table.create_object();
351 std::apply([&m_obj, &lnk, &realm](
auto && ...p) {
352 (
accessor<
typename std::decay_t<
decltype(p)>::Result>::set(
353 m_obj, m_obj.get_table().get_column_key(p.name), realm,
354 (*lnk).*(std::decay_t<
decltype(p)>::ptr)), ...);
357 set.insert(m_obj.get_key());
362 template <
typename T>
367 const std::map<std::string, T>& value) {
368 auto d = obj.get_dictionary(key);
369 for (
auto& [k, v] : value) {
370 if constexpr (internal::type_info::MixedPersistableConcept<T>::value) {
371 d.insert(k, std::visit([](
auto&& arg) {
377 if constexpr (std::is_enum_v<typename T::value_type>) {
379 d.insert(k,
static_cast<typename std::underlying_type<typename T::value_type>::type
>(*v));
395 template <
typename T>
400 const std::map<std::string, T*>& value) {
401 auto d = obj.get_dictionary(key);
402 for (
auto& [k, v] : value) {
407 m_obj = d.create_and_insert_linked_object(k, pk.value);
409 m_obj = d.create_and_insert_linked_object(k);
411 std::apply([&m_obj, &realm, o = *v](
auto && ...p) {
412 (
accessor<
typename std::decay_t<
decltype(p)>::Result>::set(
413 m_obj, m_obj.get_table().get_column_key(p.name), realm,
414 o.*(std::decay_t<
decltype(p)>::ptr)), ...);
416 d.insert(k, m_obj.get_key());
425 template <
typename T>
434 auto table = obj.get_target_table(key);
439 obj.set(key, m_obj.get_key());
441 m_obj = obj.create_and_set_linked_object(key);
443 m_obj = table.create_object();
444 obj.set(key, m_obj.get_key());
446 std::apply([&m_obj, &realm, &value](
auto && ...p) {
447 (
accessor<
typename std::decay_t<
decltype(p)>::Result>::set(
448 m_obj, m_obj.get_table().get_column_key(p.name), realm,
449 (*value).*(std::decay_t<
decltype(p)>::ptr)), ...);
463 template <
typename T>
469 if constexpr (std::is_enum_v<T>) {
470 obj.set(key,
static_cast<int64_t
>(value.value));
472 obj.set(key, value.value);
Definition: accessors.hpp:33
Definition: col_key.hpp:28
Definition: type_info.hpp:45
Definition: results.hpp:419
Definition: managed_primary_key.hpp:30