19#ifndef CPPREALM_MANAGED_NUMERIC_HPP
20#define CPPREALM_MANAGED_NUMERIC_HPP
22#include <cpprealm/macros.hpp>
23#include <cpprealm/rbool.hpp>
28 using managed<int64_t>::managed_base::operator=;
31 this->m_obj->template set<int64_t>(m_key, v);
35 [[nodiscard]] int64_t detach()
const {
36 return m_obj->template get<int64_t>(m_key);
39 [[nodiscard]] int64_t operator *()
const {
43 [[nodiscard]]
operator int64_t()
const {
48 std::enable_if_t<std::is_integral_v<T>,
rbool> operator==(
const T& rhs)
const noexcept {
49 if (this->m_rbool_query) {
50 return this->m_rbool_query->equal(m_key, (int64_t)rhs);
52 return serialize(detach()) == rhs;
56 std::enable_if_t<std::is_integral_v<T>,
rbool> operator!=(
const T& rhs)
const noexcept {
57 if (this->m_rbool_query) {
58 return this->m_rbool_query->not_equal(m_key, (int64_t)rhs);
60 return serialize(detach()) != rhs;
64 std::enable_if_t<std::is_integral_v<T>,
rbool> operator>(
const T& rhs)
const noexcept {
65 if (this->m_rbool_query) {
66 return this->m_rbool_query->greater(m_key, (int64_t)rhs);
68 return serialize(detach()) > rhs;
72 std::enable_if_t<std::is_integral_v<T>,
rbool> operator<(
const T& rhs)
const noexcept {
73 if (this->m_rbool_query) {
74 return this->m_rbool_query->less(m_key, (int64_t)rhs);
76 return serialize(detach()) < rhs;
80 std::enable_if_t<std::is_integral_v<T>,
rbool> operator>=(
const T& rhs)
const noexcept {
81 if (this->m_rbool_query) {
82 return this->m_rbool_query->greater_equal(m_key, (int64_t)rhs);
84 return serialize(detach()) >= rhs;
88 std::enable_if_t<std::is_integral_v<T>,
rbool> operator<=(
const T& rhs)
const noexcept {
89 if (this->m_rbool_query) {
90 return this->m_rbool_query->less_equal(m_key, (int64_t)rhs);
92 return serialize(detach()) <= rhs;
95 managed& operator+=(
const int64_t& o) {
96 auto old_val = m_obj->template get<int64_t>(m_key);
97 m_obj->template set<int64_t>(this->m_key, old_val + o);
100 void operator++(
int) {
101 auto old_val = m_obj->template get<int64_t>(m_key);
102 m_obj->template set<int64_t>(this->m_key, old_val + 1);
105 auto old_val = m_obj->template get<int64_t>(m_key);
106 m_obj->template set<int64_t>(this->m_key, old_val + 1);
108 managed& operator-=(
const int64_t& o) {
109 auto old_val = m_obj->template get<int64_t>(m_key);
110 m_obj->template set<int64_t>(this->m_key, old_val - o);
113 void operator--(
int) {
114 auto old_val = m_obj->template get<int64_t>(m_key);
115 m_obj->template set<int64_t>(this->m_key, old_val - 1);
118 auto old_val = m_obj->template get<int64_t>(m_key);
119 m_obj->template set<int64_t>(this->m_key, old_val - 1);
121 managed& operator*=(
const int64_t& o) {
122 auto old_val = m_obj->template get<int64_t>(m_key);
123 m_obj->template set<int64_t>(this->m_key, old_val * o);
133 template<
typename,
typename>
139 using managed<
double>::managed_base::operator=;
142 this->m_obj->template set<double>(m_key, v);
147 this->m_obj->template set<double>(m_key, (
double)v);
151 [[nodiscard]]
double detach()
const {
152 return m_obj->template get<double>(m_key);
155 double operator *()
const {
158 [[nodiscard]]
operator double()
const {
163 std::enable_if_t< std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>,
rbool> operator==(
const T& rhs)
const noexcept {
164 if (this->m_rbool_query) {
165 return this->m_rbool_query->equal(m_key, (
double)rhs);
167 return serialize(detach()) == rhs;
171 std::enable_if_t< std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>,
rbool> operator!=(
const T& rhs)
const noexcept {
172 if (this->m_rbool_query) {
173 return this->m_rbool_query->not_equal(m_key, (
double)rhs);
175 return serialize(detach()) != rhs;
179 std::enable_if_t< std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>,
rbool> operator>(
const T& rhs)
const noexcept {
180 if (this->m_rbool_query) {
181 return this->m_rbool_query->greater(m_key, (
double)rhs);
183 return serialize(detach()) > rhs;
187 std::enable_if_t< std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>,
rbool> operator<(
const T& rhs)
const noexcept {
188 if (this->m_rbool_query) {
189 return this->m_rbool_query->less(m_key, (
double)rhs);
191 return serialize(detach()) < rhs;
195 std::enable_if_t< std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>,
rbool> operator>=(
const T& rhs)
const noexcept {
196 if (this->m_rbool_query) {
197 return this->m_rbool_query->greater_equal(m_key, (
double)rhs);
199 return serialize(detach()) >= rhs;
203 std::enable_if_t< std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>,
rbool> operator<=(
const T& rhs)
const noexcept {
204 if (this->m_rbool_query) {
205 return this->m_rbool_query->less_equal(m_key, (
double)rhs);
207 return serialize(detach()) <= rhs;
210 void operator+=(
const double& o) {
211 auto old_val = m_obj->template get<double>(m_key);
212 m_obj->template set<double>(this->m_key, old_val + o);
214 void operator++(
int) {
215 auto old_val = m_obj->template get<double>(m_key);
216 m_obj->template set<double>(this->m_key, old_val + 1.0);
219 auto old_val = m_obj->template get<double>(m_key);
220 m_obj->template set<double>(this->m_key, old_val + 1.0);
222 void operator-=(
const double& o) {
223 auto old_val = m_obj->template get<double>(m_key);
224 m_obj->template set<double>(this->m_key, old_val - o);
226 void operator--(
int) {
227 auto old_val = m_obj->template get<double>(m_key);
228 m_obj->template set<double>(this->m_key, old_val - 1.0);
231 auto old_val = m_obj->template get<double>(m_key);
232 m_obj->template set<double>(this->m_key, old_val - 1.0);
234 void operator*=(
const double& o) {
235 auto old_val = m_obj->template get<double>(m_key);
236 m_obj->template set<double>(this->m_key, old_val * o);
245 template<
typename,
typename>
251 using managed<
bool>::managed_base::operator=;
253 [[nodiscard]]
bool detach()
const {
254 return m_obj->template get<bool>(m_key);
256 [[nodiscard]]
operator bool()
const {
259 bool operator *()
const {
263 rbool operator==(
const bool& rhs)
const noexcept;
264 rbool operator!=(
const bool& rhs)
const noexcept;
272 template<
typename,
typename>
276#define CPP_REALM_MANAGED_OPTIONAL_NUMERIC(type) \
278 struct managed<std::optional<type>> : managed_base { \
279 using managed<std::optional<type>>::managed_base::operator=; \
281 managed<std::optional<type>>& operator =(const double& v) { \
282 this->m_obj->template set<type>(m_key, v); \
286 managed<std::optional<type>>& operator =(const int& v) { \
287 this->m_obj->template set<type>(m_key, (double)v); \
291 [[nodiscard]] std::optional<type> detach() const { \
292 return m_obj->get_optional<type>(m_key); \
295 [[nodiscard]] std::optional<type> operator *() const { \
298 [[nodiscard]] operator std::optional<type>() const { \
301 rbool operator==(const std::optional<type>& rhs) const noexcept; \
302 rbool operator!=(const std::optional<type>& rhs) const noexcept; \
303 void operator+=(const type& o) { \
304 auto old_val = m_obj->get_optional<type>(m_key); \
306 throw std::runtime_error("Cannot perform arithmetic on null value."); \
308 m_obj->template set<type>(this->m_key, (*old_val) + o); \
310 void operator++(int) { \
311 auto old_val = m_obj->get_optional<type>(m_key); \
313 throw std::runtime_error("Cannot perform arithmetic on null value."); \
315 m_obj->template set<type>(this->m_key, (*old_val) + 1); \
317 void operator-=(const type& o) { \
318 auto old_val = m_obj->get_optional<type>(m_key); \
320 throw std::runtime_error("Cannot perform arithmetic on null value."); \
322 m_obj->template set<type>(this->m_key, (*old_val) - o); \
324 void operator--(int) { \
325 auto old_val = m_obj->get_optional<type>(m_key); \
327 throw std::runtime_error("Cannot perform arithmetic on null value."); \
329 m_obj->template set<type>(this->m_key, (*old_val) - 1); \
331 void operator*=(const type& o) { \
332 auto old_val = m_obj->get_optional<type>(m_key); \
334 throw std::runtime_error("Cannot perform arithmetic on null value."); \
336 m_obj->template set<type>(this->m_key, (*old_val) * o); \
338 void operator*(const type& o) { \
339 auto old_val = m_obj->get_optional<type>(m_key); \
341 throw std::runtime_error("Cannot perform arithmetic on null value."); \
343 m_obj->template set<type>(this->m_key, (*old_val) * o); \
345 void operator/(const type& o) { \
346 auto old_val = m_obj->get_optional<type>(m_key); \
348 throw std::runtime_error("Cannot perform arithmetic on null value."); \
350 m_obj->template set<type>(this->m_key, (*old_val) / o); \
353 managed() = default; \
354 managed(const managed&) = delete; \
355 managed(managed &&) = delete; \
356 managed& operator=(const managed&) = delete; \
357 managed& operator=(managed&&) = delete; \
358 template<typename, typename> \
359 friend struct managed; \
362CPP_REALM_MANAGED_OPTIONAL_NUMERIC(int64_t)
363CPP_REALM_MANAGED_OPTIONAL_NUMERIC(
double)
367 using managed<std::optional<bool>>::managed_base::operator=;
369 [[nodiscard]] std::optional<bool> detach()
const {
370 return m_obj->template get_optional<bool>(m_key);
373 [[nodiscard]]
operator std::optional<bool>()
const {
374 return m_obj->template get_optional<bool>(m_key);
377 std::optional<bool> operator *()
const {
381 rbool operator==(
const std::optional<bool>& rhs)
const noexcept;
382 rbool operator!=(
const std::optional<bool>& rhs)
const noexcept;
390 template<
typename,
typename>
394 template <
typename T>
397 m_obj->template set<int64_t>(m_key,
static_cast<int64_t
>(v));
401 [[nodiscard]] T detach()
const {
402 return static_cast<T
>(m_obj->get<int64_t>(m_key));
405 [[nodiscard]] T operator *()
const {
409 [[nodiscard]]
operator T()
const {
414 rbool operator==(
const T& rhs)
const noexcept {
415 if (this->m_rbool_query) {
416 return this->m_rbool_query->equal(m_key, serialize(rhs));
418 return detach() == rhs;
420 rbool operator!=(
const T& rhs)
const noexcept {
421 if (this->m_rbool_query) {
422 return this->m_rbool_query->not_equal(m_key, serialize(rhs));
424 return detach() != rhs;
426 rbool operator>(
const T& rhs)
const noexcept {
427 if (this->m_rbool_query) {
428 return this->m_rbool_query->greater(m_key, serialize(rhs));
430 return detach() > rhs;
432 rbool operator<(
const T& rhs)
const noexcept {
433 if (this->m_rbool_query) {
434 return this->m_rbool_query->greater(m_key, serialize(rhs));
436 return detach() < rhs;
438 rbool operator>=(
const T& rhs)
const noexcept {
439 if (this->m_rbool_query) {
440 return this->m_rbool_query->greater_equal(m_key, serialize(rhs));
442 return detach() >= rhs;
444 rbool operator<=(
const T& rhs)
const noexcept {
445 if (this->m_rbool_query) {
446 return this->m_rbool_query->less_equal(m_key, serialize(rhs));
448 return detach() <= rhs;
457 template<
typename,
typename>
461 template <
typename T>
465 m_obj->template set<std::optional<int64_t>>(m_key,
static_cast<int64_t
>(*v));
467 m_obj->set_null(m_key);
472 [[nodiscard]] std::optional<T> detach()
const {
473 if (
auto v = m_obj->get_optional<int64_t>(m_key)) {
474 return static_cast<T
>(*v);
479 [[nodiscard]] std::optional<T> operator *()
const {
483 [[nodiscard]]
operator std::optional<T>()
const {
488 rbool operator==(
const std::optional<T>& rhs)
const noexcept {
489 if (this->m_rbool_query) {
490 return this->m_rbool_query->equal(m_key, serialize(rhs));
492 return detach() == rhs;
494 rbool operator!=(
const std::optional<T>& rhs)
const noexcept {
495 if (this->m_rbool_query) {
496 return this->m_rbool_query->not_equal(m_key, serialize(rhs));
498 return detach() != rhs;
500 rbool operator>(
const T& rhs)
const noexcept {
501 if (this->m_rbool_query) {
502 return this->m_rbool_query->greater(m_key, rhs);
504 return detach() > rhs;
506 rbool operator<(
const T& rhs)
const noexcept {
507 if (this->m_rbool_query) {
508 return this->m_rbool_query->less(m_key, rhs);
510 return detach() < rhs;
512 rbool operator>=(
const T& rhs)
const noexcept {
513 if (this->m_rbool_query) {
514 return this->m_rbool_query->greater_equal(m_key, rhs);
516 return detach() >= rhs;
518 rbool operator<=(
const T& rhs)
const noexcept {
519 if (this->m_rbool_query) {
520 return this->m_rbool_query->less_equal(m_key, rhs);
522 return detach() <= rhs;
531 template<
typename,
typename>
Definition: macros.hpp:286