Skip to content

Commit

Permalink
Change: Always construct empty optionals by passing std::nullopt
Browse files Browse the repository at this point in the history
This is mainly meant for the transform/and_then functions to force a optional-like type to be returned. std::optional itself enforces another std::optional to be returned.
  • Loading branch information
spnda committed Aug 20, 2024
1 parent 890c1dd commit 4647c1d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions include/fastgltf/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,74 +1186,74 @@ namespace fastgltf {
[[nodiscard]] auto and_then(F&& func)& {
using U = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<F, T&>>>;
if (!has_value())
return U();
return U(std::nullopt);
return std::invoke(std::forward<F>(func), **this);
}

template <typename F>
[[nodiscard]] auto and_then(F&& func) const& {
using U = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<F, const T&>>>;
if (!has_value())
return U();
return U(std::nullopt);
return std::invoke(std::forward<F>(func), **this);
}

template <typename F>
[[nodiscard]] auto and_then(F&& func)&& {
using U = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<F, T>>>;
if (!has_value())
return U();
return U(std::nullopt);
return std::invoke(std::forward<F>(func), std::move(**this));
}

template <typename F>
[[nodiscard]] auto and_then(F&& func) const&& {
using U = std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<F, const T>>>;
if (!has_value())
return U();
return U(std::nullopt);
return std::invoke(std::forward<F>(func), std::move(**this));
}

template <typename F>
[[nodiscard]] auto transform(F&& func)& {
using U = std::remove_cv_t<std::invoke_result_t<F, T&>>;
if (!has_value())
return Optional<U>();
return Optional<U>(std::nullopt);
return Optional<U>(std::invoke(std::forward<F>(func), **this));
}

template <typename F>
[[nodiscard]] auto transform(F&& func) const& {
using U = std::remove_cv_t<std::invoke_result_t<F, const T&>>;
if (!has_value())
return Optional<U>();
return Optional<U>(std::nullopt);
return Optional<U>(std::invoke(std::forward<F>(func), **this));
}

template <typename F>
[[nodiscard]] auto transform(F&& func)&& {
using U = std::remove_cv_t<std::invoke_result_t<F, T>>;
if (!has_value())
return Optional<U>();
return Optional<U>(std::nullopt);
return Optional<U>(std::invoke(std::forward<F>(func), std::move(**this)));
}

template <typename F>
[[nodiscard]] auto transform(F&& func) const&& {
using U = std::remove_cv_t<std::invoke_result_t<F, const T>>;
if (!has_value())
return Optional<U>();
return Optional<U>(std::nullopt);
return Optional<U>(std::invoke(std::forward<F>(func), std::move(**this)));
}

template <typename F>
[[nodiscard]] T or_else(F&& func) const& {
return *this ? *this : std::forward<F>(func)();
return *this ? *this : std::invoke(std::forward<F>(func));
}

template <typename F>
[[nodiscard]] T or_else(F&& func)&& {
return *this ? std::move(*this) : std::forward<F>(func)();
return *this ? std::move(*this) : std::invoke(std::forward<F>(func));
}

void swap(OptionalWithFlagValue<T>& other) noexcept(std::is_nothrow_move_constructible_v<T> &&
Expand Down

0 comments on commit 4647c1d

Please sign in to comment.