-
Notifications
You must be signed in to change notification settings - Fork 50
Support C++23 monadic operators in OptionalRef #210
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #include "fixed_containers/source_location.hpp" | ||
|
|
||
| #include <compare> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <type_traits> | ||
|
|
@@ -27,6 +28,9 @@ class OptionalReference | |
| using pointer = T*; | ||
| using const_pointer = T*; | ||
| using BackingType = pointer; | ||
| template <class F, class Ref> | ||
| using TransformResultType = | ||
| OptionalReference<std::remove_reference_t<std::invoke_result_t<F, Ref>>>; | ||
|
|
||
| public: | ||
| // Needed for structural type | ||
|
|
@@ -144,6 +148,124 @@ class OptionalReference | |
| return val; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr std::invoke_result_t<F, reference> and_then(F&& func) & | ||
| { | ||
| if (has_value()) | ||
| { | ||
| return std::invoke(std::forward<F>(func), *val()); | ||
| } | ||
| return std::invoke_result_t<F, reference>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr std::invoke_result_t<F, const_reference> and_then(F&& func) const& | ||
| { | ||
| if (has_value()) | ||
| { | ||
| return std::invoke(std::forward<F>(func), *val()); | ||
| } | ||
| return std::invoke_result_t<F, const_reference>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr std::invoke_result_t<F, reference> and_then(F&& func) && | ||
| { | ||
| if (has_value()) | ||
| { | ||
| return std::invoke(std::forward<F>(func), *val()); | ||
| } | ||
| return std::invoke_result_t<F, reference>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr std::invoke_result_t<F, const_reference> and_then(F&& func) const&& | ||
| { | ||
| if (has_value()) | ||
| { | ||
| return std::invoke(std::forward<F>(func), *val()); | ||
| } | ||
| return std::invoke_result_t<F, const_reference>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr TransformResultType<F, reference> transform(F&& func) & | ||
| { | ||
| using InvokeResult = std::invoke_result_t<F, reference>; | ||
| static_assert(std::is_reference_v<InvokeResult>, | ||
| "transform() function must return a reference for OptionalReference"); | ||
|
Comment on lines
+194
to
+196
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think having this requirement makes |
||
| using ResultValueType = std::remove_reference_t<InvokeResult>; | ||
| if (has_value()) | ||
| { | ||
| return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val())); | ||
| } | ||
| return OptionalReference<ResultValueType>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr TransformResultType<F, const_reference> transform(F&& func) const& | ||
| { | ||
| using InvokeResult = std::invoke_result_t<F, const_reference>; | ||
| static_assert(std::is_reference_v<InvokeResult>, | ||
| "transform() function must return a reference for OptionalReference"); | ||
| using ResultValueType = std::remove_reference_t<InvokeResult>; | ||
| if (has_value()) | ||
| { | ||
| return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val())); | ||
| } | ||
| return OptionalReference<ResultValueType>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr TransformResultType<F, reference> transform(F&& func) && | ||
| { | ||
| using InvokeResult = std::invoke_result_t<F, reference>; | ||
| static_assert(std::is_reference_v<InvokeResult>, | ||
| "transform() function must return a reference for OptionalReference"); | ||
| using ResultValueType = std::remove_reference_t<InvokeResult>; | ||
| if (has_value()) | ||
| { | ||
| return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val())); | ||
| } | ||
| return OptionalReference<ResultValueType>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr TransformResultType<F, const_reference> transform(F&& func) const&& | ||
| { | ||
| using InvokeResult = std::invoke_result_t<F, const_reference>; | ||
| static_assert(std::is_reference_v<InvokeResult>, | ||
| "transform() function must return a reference for OptionalReference"); | ||
| using ResultValueType = std::remove_reference_t<InvokeResult>; | ||
| if (has_value()) | ||
| { | ||
| return OptionalReference<ResultValueType>(std::invoke(std::forward<F>(func), *val())); | ||
| } | ||
| return OptionalReference<ResultValueType>{}; | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr Self or_else(F&& func) const& | ||
| requires std::is_same_v<std::invoke_result_t<F>, Self> | ||
| { | ||
| if (has_value()) | ||
| { | ||
| return *this; | ||
| } | ||
| return std::invoke(std::forward<F>(func)); | ||
| } | ||
|
|
||
| template <class F> | ||
| constexpr Self or_else(F&& func) && | ||
| requires std::is_same_v<std::invoke_result_t<F>, Self> | ||
| { | ||
| if (has_value()) | ||
| { | ||
| return std::move(*this); | ||
| } | ||
| return std::invoke(std::forward<F>(func)); | ||
| } | ||
|
|
||
| private: | ||
| [[nodiscard]] constexpr const BackingType& val() const | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this
std::moveval? Or will*val()already be an rvalue when we're inside this special kind of method