diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index 78f88725..fa0dc6ba 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -1,6 +1,8 @@ #ifndef RFL_RESULT_HPP_ #define RFL_RESULT_HPP_ +#include +#include #include #include #include @@ -11,6 +13,7 @@ #include #include +#include "internal/is_array.hpp" #include "internal/to_std_array.hpp" namespace rfl { @@ -39,35 +42,47 @@ template class Result { static_assert(!std::is_same(), "The result type cannot be Error."); + using TOrErr = std::array; + public: using Type = T; - Result(const T& _val) : t_or_err_(_val) {} + Result(const T& _val) : success_(true) { new (&get_t()) T(_val); } - Result(T&& _val) noexcept : t_or_err_(std::move(_val)) {} + Result(T&& _val) noexcept : success_(true) { + new (&get_t()) T(std::move(_val)); + } - Result(const Error& _err) : t_or_err_(_err) {} + Result(const Error& _err) : success_(false) { new (&get_err()) Error(_err); } - Result(Error&& _err) noexcept : t_or_err_(std::move(_err)) {} + Result(Error&& _err) noexcept : success_(false) { + new (&get_err()) Error(std::move(_err)); + } - Result(Result&& _other) noexcept = default; + Result(Result&& _other) noexcept : success_(_other.success_) { + move_from_other(_other); + } - Result(const Result& _other) = default; + Result(const Result& _other) : success_(_other.success_) { + copy_from_other(_other); + } template , bool>::type = true> - Result(Result&& _other) - : t_or_err_(std::forward>(_other) - .transform([](U&& _u) { return T(std::forward(_u)); }) - .t_or_err_) {} + Result(Result&& _other) : success_(_other && true) { + auto temp = std::forward>(_other).transform( + [](U&& _u) { return T(std::forward(_u)); }); + move_from_other(temp); + } template , bool>::type = true> - Result(const Result& _other) - : t_or_err_( - _other.transform([](const U& _u) { return T(_u); }).t_or_err_) {} + Result(const Result& _other) : success_(_other && true) { + auto temp = _other.transform([](const U& _u) { return T(_u); }); + move_from_other(temp); + } - ~Result() = default; + ~Result() { destroy(); } /// Returns Result, if successful and error otherwise. /// Inspired by .and(...) in the Rust std::result type. @@ -82,18 +97,11 @@ class Result { auto and_then(const F& _f) { /// Result_U is expected to be of type Result. using Result_U = typename std::invoke_result::type; - - const auto handle_variant = - [&](TOrError&& _t_or_err) -> Result_U { - if constexpr (!std::is_same, Error>()) { - return _f(std::forward(_t_or_err)); - } else { - return std::forward(_t_or_err); - } - }; - - return std::visit(handle_variant, - std::forward>(t_or_err_)); + if (success_) { + return Result_U(_f(std::forward(get_t()))); + } else { + return Result_U(std::forward(get_err())); + } } /// Monadic operation - F must be a function of type T -> Result. @@ -101,107 +109,95 @@ class Result { auto and_then(const F& _f) const { /// Result_U is expected to be of type Result. using Result_U = typename std::invoke_result::type; - - const auto handle_variant = - [&](const TOrError& _t_or_err) -> Result_U { - if constexpr (!std::is_same()) { - return _f(_t_or_err); - } else { - return _t_or_err; - } - }; - - return std::visit(handle_variant, t_or_err_); + if (success_) { + return Result_U(_f(get_t())); + } else { + return Result_U(get_err()); + } } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. T* begin() noexcept { - const auto get_ptr = - [this](const TOrError& _t_or_err) -> T* { - if constexpr (!std::is_same()) { - return &_t_or_err; - } else { - return nullptr; - } - }; - return std::visit(get_ptr, t_or_err_); + if (success_) { + return &get_t(); + } else { + return nullptr; + } } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. const T* begin() const noexcept { - const auto get_ptr = - [this](const TOrError& _t_or_err) -> const T* { - if constexpr (!std::is_same()) { - return &_t_or_err; - } else { - return nullptr; - } - }; - return std::visit(get_ptr, t_or_err_); + if (success_) { + return &get_t(); + } else { + return nullptr; + } } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. T* end() noexcept { - const auto get_ptr = - [this](const TOrError& _t_or_err) -> T* { - if constexpr (!std::is_same()) { - return &_t_or_err + 1; - } else { - return nullptr; - } - }; - return std::visit(get_ptr, t_or_err_); + if (success_) { + return &get_t() + 1; + } else { + return nullptr; + } } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. const T* end() const noexcept { - const auto get_ptr = - [](const TOrError& _t_or_err) -> const T* { - if constexpr (!std::is_same()) { - return &_t_or_err + 1; - } else { - return nullptr; - } - }; - return std::visit(get_ptr, t_or_err_); + if (success_) { + return &get_t() + 1; + } else { + return nullptr; + } } /// Returns an std::optional if this does in fact contain an error /// or std::nullopt otherwise. std::optional error() const noexcept { - const auto get_err = - [](const TOrError& _t_or_err) -> std::optional { - if constexpr (!std::is_same()) { - return std::nullopt; - } else { - return _t_or_err; - } - }; - return std::visit(get_err, t_or_err_); + if (success_) { + return std::nullopt; + } else { + return get_err(); + } } /// Returns true if the result contains a value, false otherwise. - operator bool() const noexcept { - return !std::holds_alternative(t_or_err_); - } + operator bool() const noexcept { return success_; } /// Allows access to the underlying value. Careful: Will result in undefined /// behavior, if the result contains an error. - T& operator*() { return *std::get_if(&t_or_err_); } + T& operator*() noexcept { return get_t(); } /// Allows read access to the underlying value. Careful: Will result in /// undefined behavior, if the result contains an error. - const T& operator*() const { return *std::get_if(&t_or_err_); } + const T& operator*() const noexcept { return get_t(); } /// Assigns the underlying object. - Result& operator=(const Result& _other) = default; + Result& operator=(const Result& _other) { + if (this == &_other) { + return *this; + } + destroy(); + success_ = _other.success_; + copy_from_other(_other); + return *this; + } /// Assigns the underlying object. - Result& operator=(Result&& _other) = default; + Result& operator=(Result&& _other) noexcept { + if (this == &_other) { + return *this; + } + destroy(); + success_ = _other.success_; + move_from_other(_other); + return *this; + } /// Assigns the underlying object. template , @@ -216,31 +212,22 @@ class Result { /// Result. template Result or_else(const F& _f) { - const auto handle_variant = - [&](TOrError&& _t_or_err) -> Result { - if constexpr (std::is_same, Error>()) { - return _f(std::forward(_t_or_err)); - } else { - return std::forward(_t_or_err); - } - }; - return std::visit(handle_variant, - std::forward>(t_or_err_)); + if (success_) { + return std::forward(get_t()); + } else { + return _f(std::forward(get_err())); + } } /// Expects a function that takes of type Error -> Result and returns /// Result. template Result or_else(const F& _f) const { - const auto handle_variant = - [&_f](const TOrError& _t_or_err) -> Result { - if constexpr (std::is_same()) { - return _f(_t_or_err); - } else { - return _t_or_err; - } - }; - return std::visit(handle_variant, t_or_err_); + if (success_) { + return get_t(); + } else { + return _f(get_err()); + } } /// Returns the value contained if successful or the provided result r if @@ -255,18 +242,11 @@ class Result { auto transform(const F& _f) { /// Result_U is expected to be of type Result. using U = typename std::invoke_result::type; - - const auto handle_variant = - [&](TOrError&& _t_or_err) -> rfl::Result { - if constexpr (!std::is_same, Error>()) { - return _f(std::forward(_t_or_err)); - } else { - return std::forward(_t_or_err); - } - }; - - return std::visit(handle_variant, - std::forward>(t_or_err_)); + if (success_) { + return rfl::Result(_f(std::forward(get_t()))); + } else { + return rfl::Result(std::forward(get_err())); + } } /// Functor operation - F must be a function of type T -> U. @@ -274,75 +254,98 @@ class Result { auto transform(const F& _f) const { /// Result_U is expected to be of type Result. using U = typename std::invoke_result::type; - - const auto handle_variant = - [&](const TOrError& _t_or_err) -> rfl::Result { - if constexpr (!std::is_same()) { - return _f(_t_or_err); - } else { - return _t_or_err; - } - }; - - return std::visit(handle_variant, t_or_err_); + if (success_) { + return rfl::Result(_f(get_t())); + } else { + return rfl::Result(get_err()); + } } /// Returns the value if the result does not contain an error, throws an /// exceptions if not. Similar to .unwrap() in Rust. - T value() { - const auto handle_variant = [&](TOrError& _t_or_err) -> T { - if constexpr (!std::is_same()) { - return std::forward(_t_or_err); - } else { - throw std::runtime_error(_t_or_err.what()); - } - }; - return std::visit(handle_variant, t_or_err_); + T& value() { + if (success_) { + return get_t(); + } else { + throw std::runtime_error(get_err().what()); + } } /// Returns the value if the result does not contain an error, throws an /// exceptions if not. Similar to .unwrap() in Rust. const T& value() const { - const auto handle_variant = - [&](const TOrError& _t_or_err) -> const T& { - if constexpr (!std::is_same()) { - return _t_or_err; - } else { - throw std::runtime_error(_t_or_err.what()); - } - }; - return std::visit(handle_variant, t_or_err_); + if (success_) { + return get_t(); + } else { + throw std::runtime_error(get_err().what()); + } } /// Returns the value or a default. T value_or(T&& _default) noexcept { - const auto handle_variant = [&](TOrError&& _t_or_err) -> T { - using Type = std::remove_cvref_t; - if constexpr (!std::is_same()) { - return std::forward(_t_or_err); - } else { - return std::forward(_default); - } - }; - return std::visit(handle_variant, t_or_err_); + if (success_) { + return std::forward(get_t()); + } else { + return std::forward(_default); + } } /// Returns the value or a default. T value_or(const T& _default) const noexcept { - const auto handle_variant = - [&](const TOrError& _t_or_err) -> T { - if constexpr (!std::is_same()) { - return _t_or_err; - } else { - return _default; - } - }; - return std::visit(handle_variant, t_or_err_); + if (success_) { + return get_t(); + } else { + return _default; + } } private: - /// The underlying variant, can either be T or Error. - std::variant t_or_err_; + void copy_from_other(const Result& _other) { + if (success_) { + new (&get_t()) T(_other.get_t()); + } else { + new (&get_err()) Error(_other.get_err()); + } + } + + void destroy() { + if (success_) { + if constexpr (std::is_destructible_v /*&& !internal::is_array_v*/) { + get_t().~T(); + } + } else { + get_err().~Error(); + } + } + + T& get_t() noexcept { return *(reinterpret_cast(t_or_err_.data())); } + + const T& get_t() const noexcept { + return *(reinterpret_cast(t_or_err_.data())); + } + + Error& get_err() noexcept { + return *(reinterpret_cast(t_or_err_.data())); + } + + const Error& get_err() const noexcept { + return *(reinterpret_cast(t_or_err_.data())); + } + + void move_from_other(Result& _other) { + if (success_) { + new (&get_t()) T(std::move(_other.get_t())); + } else { + new (&get_err()) Error(std::move(_other.get_err())); + } + } + + private: + /// Signifies whether this was a success. + bool success_; + + /// The underlying data, can either be T or Error. + alignas(std::max(alignof(T), alignof(Error))) TOrErr t_or_err_; }; } // namespace rfl diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index ed247ff4..3f3eb5c9 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -27,17 +27,15 @@ class ViewReader { template void read(const std::string_view& _name, const InputVarType& _var) const { if constexpr (_i < size_) { - constexpr auto current_name = - std::tuple_element_t<_i, typename ViewType::Fields>::name(); - using OriginalType = - typename std::tuple_element_t<_i, typename ViewType::Fields>::Type; + using FieldType = std::tuple_element_t<_i, typename ViewType::Fields>; + using OriginalType = std::remove_cvref_t; using CurrentType = std::remove_cvref_t>; + constexpr auto current_name = FieldType::name(); if (!std::get<_i>(*found_) && _name == current_name) { auto res = Parser::read(*r_, _var); if (res) { - if constexpr (std::is_pointer_v || - internal::is_array_v) { + if constexpr (std::is_pointer_v) { move_to(rfl::get<_i>(*view_), &(*res)); } else { rfl::get<_i>(*view_) = *res; @@ -100,6 +98,8 @@ class ViewReader { !std::is_array_v) { ::new (_t) Target(std::move(*_s)); } else if constexpr (internal::is_array_v) { + static_assert(std::is_array_v, + "Expected target to be a c-array."); for (size_t i = 0; i < _s->arr_.size(); ++i) { move_to(&((*_t)[i]), &(_s->arr_[i])); }