More extensions

This commit is contained in:
Simon Brand
2017-10-21 20:08:43 +01:00
parent ed27d58e95
commit 470595d91a
4 changed files with 84 additions and 18 deletions

View File

@ -14,7 +14,7 @@ set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/in_place.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/in_place.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/relops.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/relops.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/observers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/observers.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/monadic.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/extensions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/constexpr.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/constexpr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/nullopt.cpp) ${CMAKE_CURRENT_SOURCE_DIR}/tests/nullopt.cpp)

View File

@ -177,16 +177,16 @@ public:
U <a href='doc_optional.md#tl::optional-T-::map_or(F&&,U&&)&'>map_or</a>(F&amp;&amp; f, U&amp;&amp; u) const &amp;&amp;; U <a href='doc_optional.md#tl::optional-T-::map_or(F&&,U&&)&'>map_or</a>(F&amp;&amp; f, U&amp;&amp; u) const &amp;&amp;;
template &lt;class F, class U&gt; template &lt;class F, class U&gt;
U <a href='doc_optional.md#tl::optional-T-::map_or_else(F&&,U&&)&'>map_or_else</a>(F&amp;&amp; f, U&amp;&amp; u) &amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) &amp;;
template &lt;class F, class U&gt; template &lt;class F, class U&gt;
U <a href='doc_optional.md#tl::optional-T-::map_or_else(F&&,U&&)&'>map_or_else</a>(F&amp;&amp; f, U&amp;&amp; u) &amp;&amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) &amp;&amp;;
template &lt;class F, class U&gt; template &lt;class F, class U&gt;
U <a href='doc_optional.md#tl::optional-T-::map_or_else(F&&,U&&)&'>map_or_else</a>(F&amp;&amp; f, U&amp;&amp; u) const &amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) const &amp;;
template &lt;class F, class U&gt; template &lt;class F, class U&gt;
U <a href='doc_optional.md#tl::optional-T-::map_or_else(F&&,U&&)&'>map_or_else</a>(F&amp;&amp; f, U&amp;&amp; u) const &amp;&amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) const &amp;&amp;;
template &lt;class U&gt; template &lt;class U&gt;
constexpr <a href='doc_optional.md#tl::optional-T-'>optional&lt;U&gt;</a> <a href='doc_optional.md#tl::optional-T-::conjunction(U&&)const'>conjunction</a>(U&amp;&amp; u) const; constexpr <a href='doc_optional.md#tl::optional-T-'>optional&lt;typename std::decay&lt;U&gt;::type&gt;</a> <a href='doc_optional.md#tl::optional-T-::conjunction(U&&)const'>conjunction</a>(U&amp;&amp; u) const;
constexpr <a href='doc_optional.md#tl::optional-T-'>optional</a> <a href='doc_optional.md#tl::optional-T-::disjunction(constoptional-T-&)&'>disjunction</a>(const <a href='doc_optional.md#tl::optional-T-'>optional</a>&amp; rhs) &amp;; constexpr <a href='doc_optional.md#tl::optional-T-'>optional</a> <a href='doc_optional.md#tl::optional-T-::disjunction(constoptional-T-&)&'>disjunction</a>(const <a href='doc_optional.md#tl::optional-T-'>optional</a>&amp; rhs) &amp;;
constexpr <a href='doc_optional.md#tl::optional-T-'>optional</a> <a href='doc_optional.md#tl::optional-T-::disjunction(constoptional-T-&)&'>disjunction</a>(const <a href='doc_optional.md#tl::optional-T-'>optional</a>&amp; rhs) const &amp;; constexpr <a href='doc_optional.md#tl::optional-T-'>optional</a> <a href='doc_optional.md#tl::optional-T-::disjunction(constoptional-T-&)&'>disjunction</a>(const <a href='doc_optional.md#tl::optional-T-'>optional</a>&amp; rhs) const &amp;;
@ -333,16 +333,16 @@ If there is a value stored, then `f` is called with `**this` and the value is re
### Function template `tl::optional::map_or_else`<a id="tl::optional-T-::map_or_else(F&&,U&&)&"></a> ### Function template `tl::optional::map_or_else`<a id="tl::optional-T-::map_or_else(F&&,U&&)&"></a>
<pre><code class="language-cpp">(1) template &lt;class F, class U&gt; <pre><code class="language-cpp">(1) template &lt;class F, class U&gt;
U map_or_else(F&amp;&amp; f, U&amp;&amp; u) &amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) &amp;;
(2) template &lt;class F, class U&gt; (2) template &lt;class F, class U&gt;
U map_or_else(F&amp;&amp; f, U&amp;&amp; u) &amp;&amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) &amp;&amp;;
(3) template &lt;class F, class U&gt; (3) template &lt;class F, class U&gt;
U map_or_else(F&amp;&amp; f, U&amp;&amp; u) const &amp;; auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) const &amp;;
(4) template &lt;class F, class U&gt; (4) template &lt;class F, class U&gt;
U map_or_else(F&amp;&amp; f, U&amp;&amp; u) const &amp;&amp;;</code></pre> auto map_or_else(F &amp;&amp;f, U &amp;&amp;u) const &amp;&amp;;</code></pre>
Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result. Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result.
@ -351,7 +351,7 @@ If there is a value stored, then `f` is called with `**this` and the value is re
### Function template `tl::optional::conjunction`<a id="tl::optional-T-::conjunction(U&&)const"></a> ### Function template `tl::optional::conjunction`<a id="tl::optional-T-::conjunction(U&&)const"></a>
<pre><code class="language-cpp">template &lt;class U&gt; <pre><code class="language-cpp">template &lt;class U&gt;
constexpr <a href='doc_optional.md#tl::optional-T-'>optional&lt;U&gt;</a> conjunction(U&amp;&amp; u) const;</code></pre> constexpr <a href='doc_optional.md#tl::optional-T-'>optional&lt;typename std::decay&lt;U&gt;::type&gt;</a> conjunction(U&amp;&amp; u) const;</code></pre>
*Returns*: `u` if `*this` has a value, otherwise an empty optional. *Returns*: `u` if `*this` has a value, otherwise an empty optional.

View File

@ -683,32 +683,42 @@ public:
/// \details If there is a value stored, then `f` is called with `**this` and the value is returned. /// \details If there is a value stored, then `f` is called with `**this` and the value is returned.
/// Otherwise `std::forward<U>(u)()` is returned. /// Otherwise `std::forward<U>(u)()` is returned.
/// \group map_or_else /// \group map_or_else
template <class F, class U> U map_or_else(F &&f, U &&u) & { /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) &;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) & {
return has_value() ? detail::invoke(std::forward<F>(f), **this) return has_value() ? detail::invoke(std::forward<F>(f), **this)
: std::forward<U>(u)(); : std::forward<U>(u)();
} }
/// \group map_or_else /// \group map_or_else
template <class F, class U> U map_or_else(F &&f, U &&u) && { /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) &&;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) && {
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
: std::forward<U>(u)(); : std::forward<U>(u)();
} }
/// \group map_or_else /// \group map_or_else
template <class F, class U> U map_or_else(F &&f, U &&u) const & { /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) const &;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) const & {
return has_value() ? detail::invoke(std::forward<F>(f), **this) return has_value() ? detail::invoke(std::forward<F>(f), **this)
: std::forward<U>(u)(); : std::forward<U>(u)();
} }
/// \group map_or_else /// \group map_or_else
template <class F, class U> U map_or_else(F &&f, U &&u) const && { /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) const &&;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) const && {
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
: std::forward<U>(u)(); : std::forward<U>(u)();
} }
/// \returns `u` if `*this` has a value, otherwise an empty optional. /// \returns `u` if `*this` has a value, otherwise an empty optional.
template <class U> constexpr optional<U> conjunction (U &&u) const { template <class U>
return has_value() ? u : nullopt; constexpr optional<typename std::decay<U>::type> conjunction (U &&u) const {
using result = optional<detail::decay_t<U>>;
return has_value() ? result{u} : result{nullopt};
} }
/// \returns `rhs` if `*this` is empty, otherwise the current value. /// \returns `rhs` if `*this` is empty, otherwise the current value.

View File

@ -1,5 +1,6 @@
#include "catch.hpp" #include "catch.hpp"
#include "optional.hpp" #include "optional.hpp"
#include <string>
#define TOKENPASTE(x, y) x##y #define TOKENPASTE(x, y) x##y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y) #define TOKENPASTE2(x, y) TOKENPASTE(x, y)
@ -339,7 +340,62 @@ SECTION("constexpr and_then") {
} }
SECTION("or else") { SECTION("or else") {
//TODO tl::optional<int> o1 = 42;
REQUIRE(*(o1.or_else([] { return tl::make_optional(13); })) == 42);
tl::optional<int> o2;
REQUIRE(*(o2.or_else([] { return tl::make_optional(13); })) == 13);
}
SECTION("disjunction") {
tl::optional<int> o1 = 42;
tl::optional<int> o2 = 12;
tl::optional<int> o3;
REQUIRE(*o1.disjunction(o2) == 42);
REQUIRE(*o1.disjunction(o3) == 42);
REQUIRE(*o2.disjunction(o1) == 12);
REQUIRE(*o2.disjunction(o3) == 12);
REQUIRE(*o3.disjunction(o1) == 42);
REQUIRE(*o3.disjunction(o2) == 12);
}
SECTION("conjunction") {
tl::optional<int> o1 = 42;
REQUIRE(*o1.conjunction(42.0) == 42.0);
REQUIRE(*o1.conjunction(std::string{"hello"}) == std::string{"hello"});
REQUIRE(!o1.conjunction(tl::nullopt));
tl::optional<int> o2;
REQUIRE(!o2.conjunction(42.0));
REQUIRE(!o2.conjunction(std::string{"hello"}));
REQUIRE(!o2.conjunction(tl::nullopt));
}
SECTION("map_or") {
tl::optional<int> o1 = 21;
REQUIRE((o1.map_or([](auto x) { return x * 2; }, 13)) == 42);
tl::optional<int> o2;
REQUIRE((o2.map_or([](auto x) { return x * 2; }, 13)) == 13);
}
SECTION("map_or_else") {
tl::optional<int> o1 = 21;
REQUIRE((o1.map_or_else([](auto x) { return x * 2; }, []{return 13;})) == 42);
tl::optional<int> o2;
REQUIRE((o2.map_or_else([](auto x) { return x * 2; }, []{return 13;})) == 13);
}
SECTION("take") {
tl::optional<int> o1 = 42;
REQUIRE(*o1.take() == 42);
REQUIRE(!o1);
tl::optional<int> o2;
REQUIRE(!o2.take());
REQUIRE(!o2);
} }
} }
; ;