diff --git a/.gitignore b/.gitignore index 2130f4d..395b6c8 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ tests/json-tokenizer-partial-test tests/json-tree-test tests/json-tree-printer-test +# CLion files +/.idea +/cmake-build-* diff --git a/examples/03_optional_types.cpp b/examples/03_optional_types.cpp index 9dc3aa8..4b30d1f 100644 --- a/examples/03_optional_types.cpp +++ b/examples/03_optional_types.cpp @@ -63,8 +63,8 @@ int main() dataStruct.number, dataStruct.boolean, dataStruct.opt.data, - dataStruct.opt_checked.assigned, - dataStruct.opt_checked.data); + dataStruct.opt_checked.has_value(), + dataStruct.opt_checked.value()); return 0; } diff --git a/include/json_struct/json_struct.h b/include/json_struct/json_struct.h index 1060dfe..e883f1a 100644 --- a/include/json_struct/json_struct.h +++ b/include/json_struct/json_struct.h @@ -2230,11 +2230,27 @@ struct Nullable } T data; - T &operator()() + T &operator()() &noexcept { return data; } - const T &operator()() const + constexpr const T &operator()() const &noexcept + { + return data; + } + T &operator*() &noexcept + { + return data; + } + constexpr const T &operator*() const &noexcept + { + return data; + } + T &value() &noexcept + { + return data; + } + constexpr const T &value() const &noexcept { return data; } @@ -2265,14 +2281,38 @@ struct NullableChecked return *this; } - T &operator()() + T &operator()() &noexcept + { + return data; + } + constexpr const T &operator()() const &noexcept { return data; } - const T &operator()() const + T &operator*() &noexcept { return data; } + constexpr const T &operator*() const &noexcept + { + return data; + } + T &value() &noexcept + { + return data; + } + constexpr const T &value() const &noexcept + { + return data; + } + constexpr explicit operator bool() const noexcept + { + return null; + } + constexpr bool has_value() const noexcept + { + return null; + } T data; bool null; }; @@ -2300,11 +2340,35 @@ struct Optional } T data; - T &operator()() + T &operator()() &noexcept + { + return data; + } + constexpr const T &operator()() const &noexcept + { + return data; + } + T &operator*() &noexcept + { + return data; + } + constexpr const T &operator*() const &noexcept { return data; } - const T &operator()() const + constexpr const T *operator->() const noexcept + { + return &data; + } + T *operator->() noexcept + { + return &data; + } + T &value() &noexcept + { + return data; + } + constexpr const T &value() const &noexcept { return data; } @@ -2335,17 +2399,63 @@ struct OptionalChecked assigned = true; return *this; } + OptionalChecked &operator=(const T &&other) + { + data = std::move(other); + assigned = true; + return *this; + } - T &operator()() + T &operator()() &noexcept { + assert(assigned); return data; } - const T &operator()() const + constexpr const T &operator()() const &noexcept { + assert(assigned); return data; } + T &operator*() &noexcept + { + assert(assigned); + return data; + } + constexpr const T &operator*() const &noexcept + { + assert(assigned); + return data; + } + constexpr const T *operator->() const noexcept + { + assert(assigned); + return &data; + } + T *operator->() noexcept + { + assert(assigned); + return &data; + } + T &value() &noexcept + { + assert(assigned); + return data; + } + constexpr const T &value() const &noexcept + { + assert(assigned); + return data; + } + constexpr explicit operator bool() const noexcept + { + return assigned; + } + constexpr bool has_value() const noexcept + { + return assigned; + } #ifdef JS_STD_OPTIONAL - std::optional opt() const + operator std::optional() const { return assigned ? std::optional(data) : std::nullopt; } @@ -7036,12 +7146,12 @@ struct TypeHandler> public: static inline Error to(Optional &to_type, ParseContext &context) { - return TypeHandler::to(to_type.data, context); + return TypeHandler::to(to_type.value(), context); } static inline void from(const Optional &opt, Token &token, Serializer &serializer) { - TypeHandler::from(opt(), token, serializer); + TypeHandler::from(*opt, token, serializer); } }; @@ -7053,13 +7163,13 @@ struct TypeHandler> static inline Error to(OptionalChecked &to_type, ParseContext &context) { to_type.assigned = true; - return TypeHandler::to(to_type.data, context); + return TypeHandler::to(to_type.value(), context); } static inline void from(const OptionalChecked &opt, Token &token, Serializer &serializer) { - if (opt.assigned) - TypeHandler::from(opt(), token, serializer); + if (opt) + TypeHandler::from(*opt, token, serializer); } };