Skip to content

Commit 06032eb

Browse files
committed
Add transform
1 parent 2d42ed8 commit 06032eb

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

tl/optional.hpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,85 @@ class optional : private detail::optional_move_assign_base<T>,
891891
return optional_map_impl(std::move(*this), std::forward<F>(f));
892892
}
893893
#endif
894+
#endif
895+
896+
#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
897+
!defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
898+
/// \brief Carries out some operation on the stored object if there is one.
899+
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
900+
/// value())`. Returns a `std::optional<U>`. The return value is empty if
901+
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
902+
/// return value of `std::invoke(std::forward<F>(f), value())` and is
903+
/// returned.
904+
///
905+
/// \group map
906+
/// \synopsis template <class F> constexpr auto transform(F &&f) &;
907+
template <class F> TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) & {
908+
return optional_map_impl(*this, std::forward<F>(f));
909+
}
910+
911+
/// \group map
912+
/// \synopsis template <class F> constexpr auto transform(F &&f) &&;
913+
template <class F> TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) && {
914+
return optional_map_impl(std::move(*this), std::forward<F>(f));
915+
}
916+
917+
/// \group map
918+
/// \synopsis template <class F> constexpr auto transform(F &&f) const&;
919+
template <class F> constexpr auto transform(F&& f) const & {
920+
return optional_map_impl(*this, std::forward<F>(f));
921+
}
922+
923+
/// \group map
924+
/// \synopsis template <class F> constexpr auto transform(F &&f) const&&;
925+
template <class F> constexpr auto transform(F&& f) const && {
926+
return optional_map_impl(std::move(*this), std::forward<F>(f));
927+
}
928+
#else
929+
/// \brief Carries out some operation on the stored object if there is one.
930+
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
931+
/// value())`. Returns a `std::optional<U>`. The return value is empty if
932+
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
933+
/// return value of `std::invoke(std::forward<F>(f), value())` and is
934+
/// returned.
935+
///
936+
/// \group map
937+
/// \synopsis template <class F> auto transform(F &&f) &;
938+
template <class F>
939+
TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&>(),
940+
std::declval<F&&>()))
941+
transform(F&& f) & {
942+
return optional_map_impl(*this, std::forward<F>(f));
943+
}
944+
945+
/// \group map
946+
/// \synopsis template <class F> auto transform(F &&f) &&;
947+
template <class F>
948+
TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&&>(),
949+
std::declval<F&&>()))
950+
transform(F&& f) && {
951+
return optional_map_impl(std::move(*this), std::forward<F>(f));
952+
}
953+
954+
/// \group map
955+
/// \synopsis template <class F> auto transform(F &&f) const&;
956+
template <class F>
957+
constexpr decltype(optional_map_impl(std::declval<const optional&>(),
958+
std::declval<F&&>()))
959+
transform(F&& f) const & {
960+
return optional_map_impl(*this, std::forward<F>(f));
961+
}
962+
963+
#ifndef TL_OPTIONAL_NO_CONSTRR
964+
/// \group map
965+
/// \synopsis template <class F> auto transform(F &&f) const&&;
966+
template <class F>
967+
constexpr decltype(optional_map_impl(std::declval<const optional&&>(),
968+
std::declval<F&&>()))
969+
transform(F&& f) const && {
970+
return optional_map_impl(std::move(*this), std::forward<F>(f));
971+
}
972+
#endif
894973
#endif
895974

896975
/// \brief Calls `f` if the optional is empty
@@ -1941,6 +2020,85 @@ template <class T> class optional<T &> {
19412020
return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
19422021
}
19432022
#endif
2023+
#endif
2024+
2025+
#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
2026+
!defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
2027+
/// \brief Carries out some operation on the stored object if there is one.
2028+
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
2029+
/// value())`. Returns a `std::optional<U>`. The return value is empty if
2030+
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
2031+
/// return value of `std::invoke(std::forward<F>(f), value())` and is
2032+
/// returned.
2033+
///
2034+
/// \group map
2035+
/// \synopsis template <class F> constexpr auto transform(F &&f) &;
2036+
template <class F> TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) & {
2037+
return detail::optional_map_impl(*this, std::forward<F>(f));
2038+
}
2039+
2040+
/// \group map
2041+
/// \synopsis template <class F> constexpr auto transform(F &&f) &&;
2042+
template <class F> TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) && {
2043+
return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2044+
}
2045+
2046+
/// \group map
2047+
/// \synopsis template <class F> constexpr auto transform(F &&f) const&;
2048+
template <class F> constexpr auto transform(F&& f) const & {
2049+
return detail::optional_map_impl(*this, std::forward<F>(f));
2050+
}
2051+
2052+
/// \group map
2053+
/// \synopsis template <class F> constexpr auto transform(F &&f) const&&;
2054+
template <class F> constexpr auto transform(F&& f) const && {
2055+
return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2056+
}
2057+
#else
2058+
/// \brief Carries out some operation on the stored object if there is one.
2059+
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
2060+
/// value())`. Returns a `std::optional<U>`. The return value is empty if
2061+
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
2062+
/// return value of `std::invoke(std::forward<F>(f), value())` and is
2063+
/// returned.
2064+
///
2065+
/// \group map
2066+
/// \synopsis template <class F> auto transform(F &&f) &;
2067+
template <class F>
2068+
TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval<optional&>(),
2069+
std::declval<F&&>()))
2070+
transform(F&& f) & {
2071+
return detail::optional_map_impl(*this, std::forward<F>(f));
2072+
}
2073+
2074+
/// \group map
2075+
/// \synopsis template <class F> auto transform(F &&f) &&;
2076+
template <class F>
2077+
TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval<optional&&>(),
2078+
std::declval<F&&>()))
2079+
transform(F&& f) && {
2080+
return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2081+
}
2082+
2083+
/// \group map
2084+
/// \synopsis template <class F> auto transform(F &&f) const&;
2085+
template <class F>
2086+
constexpr decltype(detail::optional_map_impl(std::declval<const optional&>(),
2087+
std::declval<F&&>()))
2088+
transform(F&& f) const & {
2089+
return detail::optional_map_impl(*this, std::forward<F>(f));
2090+
}
2091+
2092+
#ifndef TL_OPTIONAL_NO_CONSTRR
2093+
/// \group map
2094+
/// \synopsis template <class F> auto transform(F &&f) const&&;
2095+
template <class F>
2096+
constexpr decltype(detail::optional_map_impl(std::declval<const optional&&>(),
2097+
std::declval<F&&>()))
2098+
transform(F&& f) const && {
2099+
return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2100+
}
2101+
#endif
19442102
#endif
19452103

19462104
/// \brief Calls `f` if the optional is empty

0 commit comments

Comments
 (0)