Realm C++ SDK Version v2.2.0

managed_timestamp.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_MANAGED_TIMESTAMP_HPP
20#define CPPREALM_MANAGED_TIMESTAMP_HPP
21
22#include <cpprealm/internal/bridge/timestamp.hpp>
23#include <cpprealm/macros.hpp>
24
25namespace realm {
26 class rbool;
27}
28namespace realm {
29
30 template<>
31 struct managed<std::chrono::time_point<std::chrono::system_clock>> : public managed_base {
32 using value_type = std::chrono::time_point<std::chrono::system_clock>;
33 using managed<std::chrono::time_point<std::chrono::system_clock>>::managed_base::operator=;
34
35 [[nodiscard]] std::chrono::time_point<std::chrono::system_clock> detach() const {
36 return m_obj->template get<realm::internal::bridge::timestamp>(m_key);
37 }
38
39 [[nodiscard]] operator std::chrono::time_point<std::chrono::system_clock>() const {
40 return detach();
41 }
42
43 auto time_since_epoch() const {
44 auto ts = m_obj->template get<internal::bridge::timestamp>(m_key);
45 return ts.get_time_point().time_since_epoch();
46 }
47
48 template <typename S>
49 void operator+=(const std::chrono::duration<S>& rhs) {
50 auto ts = m_obj->template get<internal::bridge::timestamp>(m_key);
51 m_obj->set(m_key, internal::bridge::timestamp(ts.get_time_point() + rhs));
52 }
53
54 //MARK: - comparison operators
55 rbool operator==(const std::chrono::time_point<std::chrono::system_clock>& rhs) const noexcept;
56 rbool operator!=(const std::chrono::time_point<std::chrono::system_clock>& rhs) const noexcept;
57 rbool operator>(const std::chrono::time_point<std::chrono::system_clock>& rhs) const noexcept;
58 rbool operator>=(const std::chrono::time_point<std::chrono::system_clock>& rhs) const noexcept;
59 rbool operator<(const std::chrono::time_point<std::chrono::system_clock>& rhs) const noexcept;
60 rbool operator<=(const std::chrono::time_point<std::chrono::system_clock>& rhs) const noexcept;
61
62 private:
63 managed() = default;
64 managed(const managed&) = delete;
65 managed(managed &&) = delete;
66 managed& operator=(const managed&) = delete;
67 managed& operator=(managed&&) = delete;
68 template<typename, typename>
69 friend struct managed;
70 };
71
72 template<>
73 struct managed<std::optional<std::chrono::time_point<std::chrono::system_clock>>> : managed_base {
74 using value_type = std::optional<std::chrono::time_point<std::chrono::system_clock>>;
75 using managed<std::optional<std::chrono::time_point<std::chrono::system_clock>>>::managed_base::operator=;
76
77 [[nodiscard]] std::optional<std::chrono::time_point<std::chrono::system_clock>> detach() const {
78 return m_obj->get_optional<realm::internal::bridge::timestamp>(m_key);
79 }
80
81 [[nodiscard]] operator std::optional<std::chrono::time_point<std::chrono::system_clock>>() const {
82 return detach();
83 }
84
85 struct box {
86 template <typename S>
87 void operator+=(const std::chrono::duration<S>& rhs) {
88 auto ts = m_parent.get().m_obj->get_optional<internal::bridge::timestamp>(m_parent.get().m_key);
89 m_parent.get().m_obj->set(m_parent.get().m_key, internal::bridge::timestamp(ts->get_time_point() + rhs));
90 }
91 auto time_since_epoch() const {
92 auto ts = m_parent.get().m_obj->get_optional<internal::bridge::timestamp>(m_parent.get().m_key);
93 return ts->get_time_point().time_since_epoch();
94 }
95 private:
96 box(managed& parent) : m_parent(parent) { }
97 std::reference_wrapper<managed<std::optional<std::chrono::time_point<std::chrono::system_clock>>>> m_parent;
98 friend struct managed<std::optional<std::chrono::time_point<std::chrono::system_clock>>>;
99 };
100
101 std::unique_ptr<box> operator->()
102 {
103 return std::make_unique<box>(box(*this));
104 }
105 [[nodiscard]] box operator*() {
106 return box(*this);
107 }
108
109 //MARK: - comparison operators
110 rbool operator==(const std::optional<std::chrono::time_point<std::chrono::system_clock>>& rhs) const noexcept;
111 rbool operator!=(const std::optional<std::chrono::time_point<std::chrono::system_clock>>& rhs) const noexcept;
112
113 private:
114 managed() = default;
115 managed(const managed&) = delete;
116 managed(managed &&) = delete;
117 managed& operator=(const managed&) = delete;
118 managed& operator=(managed&&) = delete;
119 template<typename, typename>
120 friend struct managed;
121 };
122
123} // namespace realm
124
125
126#endif//CPPREALM_MANAGED_TIMESTAMP_HPP
Definition: rbool.hpp:36
Definition: managed_dictionary.hpp:129
Definition: timestamp.hpp:30
Definition: macros.hpp:286
Definition: obj.hpp:62