Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional で不要なコピーが発生しないようにする #1005

Open
wants to merge 1 commit into
base: v6_develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Siv3D/include/Siv3D/Optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,22 @@ namespace s3d
constexpr value_type value_or_eval(Fty&& f)&&;

template <class Fty, std::enable_if_t<std::is_invocable_v<Fty, value_type&>>* = nullptr>
constexpr void then(Fty&& f);
constexpr void then(Fty&& f)&;

template <class Fty, std::enable_if_t<std::is_invocable_v<Fty, value_type>>* = nullptr>
constexpr void then(Fty&& f) const;
constexpr void then(Fty&& f) const&;

template <class Fty, std::enable_if_t<std::is_invocable_v<Fty, value_type>>* = nullptr>
constexpr void then(Fty&& f)&&;

template <class Fty, class R = std::decay_t<std::invoke_result_t<Fty, value_type&>>>
Optional<R> map(Fty f);
Optional<R> map(Fty f)&;

template <class Fty, class R = std::decay_t<std::invoke_result_t<Fty, value_type>>>
Optional<R> map(Fty f) const&;

template <class Fty, class R = std::decay_t<std::invoke_result_t<Fty, value_type>>>
Optional<R> map(Fty f) const;
Optional<R> map(Fty f)&&;

template <class CharType>
friend std::basic_ostream<CharType>& operator <<(std::basic_ostream<CharType>& output, const Optional<Type>& value)
Expand Down
68 changes: 60 additions & 8 deletions Siv3D/include/Siv3D/detail/Optional.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,62 @@ namespace s3d
[[nodiscard]]
inline constexpr typename Optional<Type>::value_type Optional<Type>::value_or_construct(Args&&... args) const&
{
return static_cast<bool>(*this) ? **this : Type(std::forward<Args>(args)...);
if (*this)
{
return **this;
}
else
{
return Type(std::forward<Args>(args)...);
}
}

template <class Type>
template <class... Args, std::enable_if_t<std::conjunction_v<std::is_move_constructible<Type>, std::is_constructible<Type, Args...>>>*>
[[nodiscard]]
inline constexpr typename Optional<Type>::value_type Optional<Type>::value_or_construct(Args&&... args)&&
{
return static_cast<bool>(*this) ? std::move(**this) : Type(std::forward<Args>(args)...);
if (*this)
{
return std::move(**this);
}
else
{
return Type(std::forward<Args>(args)...);
}
}

template <class Type>
template <class Fty, std::enable_if_t<std::conjunction_v<std::is_copy_constructible<Type>, std::is_convertible<std::invoke_result_t<Fty>, Type>>>*>
inline constexpr typename Optional<Type>::value_type Optional<Type>::value_or_eval(Fty&& f) const&
{
return static_cast<bool>(*this) ? **this : std::forward<Fty>(f)();
if (*this)
{
return **this;
}
else
{
return std::forward<Fty>(f)();
}
}

template <class Type>
template <class Fty, std::enable_if_t<std::conjunction_v<std::is_move_constructible<Type>, std::is_convertible<std::invoke_result_t<Fty>, Type>>>*>
inline constexpr typename Optional<Type>::value_type Optional<Type>::value_or_eval(Fty&& f)&&
{
return static_cast<bool>(*this) ? std::move(**this) : std::forward<Fty>(f)();
if (*this)
{
return std::move(**this);
}
else
{
return std::forward<Fty>(f)();
}
}

template <class Type>
template <class Fty, std::enable_if_t<std::is_invocable_v<Fty, Type&>>*>
inline constexpr void Optional<Type>::then(Fty&& f)
inline constexpr void Optional<Type>::then(Fty&& f)&
{
if (has_value())
{
Expand All @@ -159,17 +187,27 @@ namespace s3d

template <class Type>
template <class Fty, std::enable_if_t<std::is_invocable_v<Fty, Type>>*>
inline constexpr void Optional<Type>::then(Fty&& f) const
inline constexpr void Optional<Type>::then(Fty&& f) const&
{
if (has_value())
{
std::forward<Fty>(f)(value());
}
}

template <class Type>
template <class Fty, std::enable_if_t<std::is_invocable_v<Fty, Type>>*>
inline constexpr void Optional<Type>::then(Fty&& f)&&
{
if (has_value())
{
std::forward<Fty>(f)(std::move(*this).value());
}
}

template <class Type>
template <class Fty, class R>
inline Optional<R> Optional<Type>::map(Fty f)
inline Optional<R> Optional<Type>::map(Fty f)&
{
if (has_value())
{
Expand All @@ -183,7 +221,7 @@ namespace s3d

template <class Type>
template <class Fty, class R>
inline Optional<R> Optional<Type>::map(Fty f) const
inline Optional<R> Optional<Type>::map(Fty f) const&
{
if (has_value())
{
Expand All @@ -195,6 +233,20 @@ namespace s3d
}
}

template <class Type>
template <class Fty, class R>
inline Optional<R> Optional<Type>::map(Fty f)&&
{
if (has_value())
{
return f(std::move(*this).value());
}
else
{
return none;
}
}

template <class Type1, class Type2>
inline constexpr bool operator ==(const Optional<Type1>& lhs, const Optional<Type2>& rhs)
{
Expand Down