19#ifndef CPPREALM_TYPES_HPP
20#define CPPREALM_TYPES_HPP
27#include <cpprealm/internal/bridge/realm.hpp>
28#include <cpprealm/internal/bridge/schema.hpp>
29#include <cpprealm/internal/type_info.hpp>
33 const size_t not_in_collection = -1;
36 explicit uuid(
const std::string &);
37 uuid(
const std::array<uint8_t, 16>&);
39 [[nodiscard]] std::string to_string()
const;
40 [[nodiscard]] std::string to_base64()
const;
41 [[nodiscard]] std::array<uint8_t, 16> to_bytes()
const;
45 template <
typename mapped_type>
49 inline bool operator ==(
const uuid& lhs,
const uuid& rhs) {
return lhs.m_uuid == rhs.m_uuid; }
50 inline bool operator !=(
const uuid& lhs,
const uuid& rhs) {
return lhs.m_uuid != rhs.m_uuid; }
51 inline bool operator >(
const uuid& lhs,
const uuid& rhs) {
return lhs.m_uuid > rhs.m_uuid; }
52 inline bool operator >=(
const uuid& lhs,
const uuid& rhs) {
return lhs.m_uuid >= rhs.m_uuid; }
53 inline bool operator <(
const uuid& lhs,
const uuid& rhs) {
return lhs.m_uuid < rhs.m_uuid; }
54 inline bool operator <=(
const uuid& lhs,
const uuid& rhs) {
return lhs.m_uuid <= rhs.m_uuid; }
59 [[nodiscard]]
static object_id generate();
60 [[nodiscard]] std::string to_string()
const;
64 template <
typename mapped_type>
68 inline bool operator ==(
const object_id& lhs,
const object_id& rhs) {
return lhs.m_object_id == rhs.m_object_id; }
69 inline bool operator !=(
const object_id& lhs,
const object_id& rhs) {
return lhs.m_object_id != rhs.m_object_id; }
70 inline bool operator <(
const object_id& lhs,
const object_id& rhs) {
return lhs.m_object_id < rhs.m_object_id; }
71 inline bool operator >(
const object_id& lhs,
const object_id& rhs) {
return lhs.m_object_id > rhs.m_object_id; }
72 inline bool operator <=(
const object_id& lhs,
const object_id& rhs) {
return lhs.m_object_id <= rhs.m_object_id; }
73 inline bool operator >=(
const object_id& lhs,
const object_id& rhs) {
return lhs.m_object_id >= rhs.m_object_id; }
79 [[nodiscard]] std::string to_string()
const;
80 [[nodiscard]]
bool is_NaN()
const;
84 template <
typename mapped_type>
98 inline bool operator >(
const decimal128& lhs,
const decimal128& rhs) {
return lhs.m_decimal > rhs.m_decimal; }
99 inline bool operator >=(
const decimal128& lhs,
const decimal128& rhs) {
return lhs.m_decimal >= rhs.m_decimal; }
100 inline bool operator <(
const decimal128& lhs,
const decimal128& rhs) {
return lhs.m_decimal < rhs.m_decimal; }
101 inline bool operator <=(
const decimal128& lhs,
const decimal128& rhs) {
return lhs.m_decimal <= rhs.m_decimal; }
102 inline bool operator ==(
const decimal128& lhs,
const decimal128& rhs) {
return lhs.m_decimal == rhs.m_decimal; }
103 inline bool operator !=(
const decimal128& lhs,
const decimal128& rhs) {
return lhs.m_decimal != rhs.m_decimal; }
108 static inline int64_t serialize(int64_t v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
111 static inline int64_t deserialize(int64_t v) {
115 static inline std::optional<int64_t> serialize(
const std::optional<int64_t>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
118 static inline std::optional<int64_t> deserialize(
const std::optional<int64_t>& v) {
122 static inline double serialize(
const double& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
125 static inline double deserialize(
const double& v) {
129 static inline std::optional<double> serialize(
const std::optional<double>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
132 static inline std::optional<double> deserialize(
const std::optional<double>& v) {
136 static inline bool serialize(
bool v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
139 static inline bool deserialize(
bool v) {
143 static inline std::optional<bool> serialize(std::optional<bool> v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
146 static inline std::optional<bool> deserialize(std::optional<bool> v) {
151 static inline typename std::enable_if_t<std::is_enum_v<T>, int64_t> serialize(
const T& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
152 return static_cast<int64_t
>(v);
154 template<
typename T, std::enable_if_t<std::is_enum_v<T>>>
155 static inline T deserialize(int64_t v) {
156 return static_cast<T
>(v);
160 static inline std::enable_if_t<std::is_enum_v<typename T::value_type>, std::optional<int64_t>> serialize(
const T& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
162 return static_cast<int64_t
>(*v);
167 template<
typename T, std::enable_if_t<std::is_enum_v<
typename T::value_type>>>
168 static inline std::optional<int64_t> deserialize(std::optional<int64_t> v) {
170 return static_cast<T
>(*v);
176 static inline std::string serialize(
const std::string& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
179 static inline std::string deserialize(
const std::string& v) {
183 static inline std::optional<std::string> serialize(
const std::optional<std::string>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
186 static inline std::optional<std::string> deserialize(
const std::optional<std::string>& v) {
190 static inline internal::bridge::uuid serialize(
const uuid& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
193 static inline uuid deserialize(
const internal::bridge::uuid& v) {
194 return v.operator ::realm::uuid();
197 static inline std::optional<internal::bridge::uuid> serialize(
const std::optional<uuid>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
200 static inline std::optional<uuid> deserialize(
const std::optional<internal::bridge::uuid>& v) {
202 return v->operator ::realm::uuid();
208 static inline internal::bridge::binary serialize(
const std::vector<uint8_t>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
211 static inline std::vector<uint8_t> deserialize(
const internal::bridge::binary& v) {
212 return v.operator std::vector<uint8_t>();
215 static inline std::optional<internal::bridge::binary> serialize(
const std::optional<std::vector<uint8_t>>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
218 static inline std::optional<std::vector<uint8_t>> deserialize(
const std::optional<internal::bridge::binary>& v) {
220 return v->operator std::vector<uint8_t>();
226 static inline internal::bridge::timestamp serialize(
const std::chrono::time_point<std::chrono::system_clock>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
229 static inline std::chrono::time_point<std::chrono::system_clock> deserialize(
const internal::bridge::timestamp& v) {
230 return v.operator std::chrono::time_point<std::chrono::system_clock>();
233 static inline std::optional<internal::bridge::timestamp> serialize(
const std::optional<std::chrono::time_point<std::chrono::system_clock>>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
236 static inline std::optional<std::chrono::time_point<std::chrono::system_clock>> deserialize(
const std::optional<internal::bridge::timestamp>& v) {
238 return v->operator std::chrono::time_point<std::chrono::system_clock>();
244 static inline internal::bridge::object_id serialize(
const realm::object_id& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
247 static inline realm::object_id deserialize(
const internal::bridge::object_id& v) {
248 return v.operator ::realm::object_id();
251 static inline std::optional<internal::bridge::object_id> serialize(
const std::optional<realm::object_id>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
254 static inline std::optional<realm::object_id> deserialize(
const std::optional<internal::bridge::object_id>& v) {
256 return v->operator ::realm::object_id();
262 static inline internal::bridge::decimal128 serialize(
const realm::decimal128& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
265 static inline realm::decimal128 deserialize(
const internal::bridge::decimal128& v) {
266 return v.operator ::realm::decimal128();
269 static inline std::optional<internal::bridge::decimal128> serialize(
const std::optional<realm::decimal128>& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
272 static inline std::optional<realm::decimal128> deserialize(
const std::optional<internal::bridge::decimal128>& v) {
274 return v->operator ::realm::decimal128();
280 template <
typename T>
281 static inline typename std::enable_if_t<internal::type_info::MixedPersistableConcept<T>::value, internal::bridge::mixed> serialize(
const T& v,
const std::optional<internal::bridge::realm>& = std::nullopt) {
282 return std::visit([&](
auto&& arg) {
283 using StoredType = std::decay_t<
decltype(arg)>;
284 using M =
typename internal::type_info::type_info<StoredType>::internal_type;
285 return internal::bridge::mixed(M(arg));
289 template <
typename T>
290 static inline typename std::enable_if_t<!internal::type_info::MixedPersistableConcept<T>::value, T>
291 deserialize(
const internal::bridge::mixed& value) {
292 if constexpr (internal::type_info::is_optional<T>::value) {
294 }
else if constexpr (std::is_same_v<T, int64_t>) {
295 return value.operator int64_t();
296 }
else if constexpr (std::is_same_v<T, bool>) {
297 return value.operator bool();
298 }
else if constexpr (std::is_same_v<T, std::string>) {
299 return static_cast<std::string
>(value);
300 }
else if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
301 return static_cast<std::vector<uint8_t>
>(
static_cast<internal::bridge::binary
>(value));
302 }
else if constexpr (std::is_same_v<T, std::chrono::time_point<std::chrono::system_clock>>) {
303 return static_cast<internal::bridge::timestamp
>(value);
304 }
else if constexpr (std::disjunction_v<std::is_same<T, double>, std::is_same<T, float>>) {
305 return static_cast<double>(value);
306 }
else if constexpr (std::is_same_v<T, realm::uuid>) {
307 return value.operator internal::bridge::uuid().operator ::realm::uuid();
308 }
else if constexpr (std::is_same_v<T, realm::object_id>) {
309 return value.operator internal::bridge::object_id().operator ::realm::object_id();
310 }
else if constexpr (std::is_same_v<T, realm::decimal128>) {
311 return value.operator internal::bridge::decimal128() .operator ::realm::decimal128();
312 }
else if constexpr (std::is_enum_v<T>) {
313 return static_cast<T
>(value.operator int64_t());
319 template <
typename T>
320 static inline typename std::enable_if_t<internal::type_info::MixedPersistableConcept<T>::value, T>
321 deserialize(
const internal::bridge::mixed& value) {
322 if (value.is_null()) {
323 return std::monostate();
326 switch (value.type()) {
327 case internal::bridge::data_type::Int:
328 return value.operator int64_t();
329 case internal::bridge::data_type::Bool:
330 return value.operator bool();
331 case internal::bridge::data_type::String:
332 return static_cast<std::string
>(value);
333 case internal::bridge::data_type::Binary:
334 return static_cast<std::vector<uint8_t>
>(
static_cast<internal::bridge::binary
>(value));
335 case internal::bridge::data_type::Timestamp:
336 return static_cast<internal::bridge::timestamp
>(value);
337 case internal::bridge::data_type::Float:
338 case internal::bridge::data_type::Double:
339 return static_cast<double>(value);
340 case internal::bridge::data_type::UUID:
341 return value.operator internal::bridge::uuid().operator ::realm::uuid();
342 case internal::bridge::data_type::ObjectId:
343 return value.operator internal::bridge::object_id().operator ::realm::object_id();
344 case internal::bridge::data_type::Decimal:
345 return value.operator internal::bridge::decimal128().operator ::realm::decimal128();
346 case internal::bridge::data_type::TypedLink:
347 throw std::runtime_error(
"Objects stored in mixed properties must be accessed via `get_object_value()`");
349 throw std::runtime_error(
"Unknown datatype while deserializing mixed property.");
Definition: managed_dictionary.hpp:31
Definition: decimal128.hpp:30
Definition: object_id.hpp:31