diff --git a/CMakeLists.txt b/CMakeLists.txt index 4402651..8dbffc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ add_test_executable(flip) add_test_executable(flow) add_test_executable(function) add_test_executable(identity) +add_test_executable(if) add_test_executable(implicit) add_test_executable(indirect) add_test_executable(infix) diff --git a/fit/if.h b/fit/if.h new file mode 100644 index 0000000..4852c75 --- /dev/null +++ b/fit/if.h @@ -0,0 +1,109 @@ +/*============================================================================= + Copyright (c) 2015 Paul Fultz II + if_.h + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ + +#ifndef FIT_GUARD_IF_H +#define FIT_GUARD_IF_H + +/// if +/// == +/// +/// Description +/// ----------- +/// +/// The `if_` function applies a boolen condition to the function. +/// +/// Synopsis +/// -------- +/// +/// template +/// constexpr auto if_(IntegralConstant); +/// +/// Requirements +/// ------------ +/// +/// IntegralConstant must be: +/// +/// IntegralConstant +/// +/// Example +/// ------- +/// +/// struct sum_f +/// { +/// template +/// int operator()(T x, T y) const +/// { +/// return fit::conditional( +/// fit::if_(std::is_integral())(fit::_ + fit::_), +/// fit::always(0) +/// )(x, y); +/// } +/// }; +/// assert(sum_f()(1, 2) == 3); +/// assert(sum_f()("", "") == 0); +/// + +#include +#include +#include +#include +#include +#include + +namespace fit { + +namespace detail { + +template +struct if_depend +: C +{}; + +template +struct if_adaptor : F +{ + FIT_INHERIT_CONSTRUCTOR(if_adaptor, F) +}; + +template +struct if_adaptor +{ + template + constexpr if_adaptor(Ts&&...) + {} +}; + +template +struct make_if_f +{ + constexpr make_if_f() + {} + template + constexpr if_adaptor operator()(F f) const + { + return if_adaptor(fit::move(f)); + } +}; + +struct if_f +{ + constexpr if_f() + {} + template + constexpr make_if_f operator()(Cond) const + { + return {}; + } +}; + +} + +FIT_DECLARE_STATIC_VAR(if_, detail::if_f); + +} + +#endif diff --git a/mkdocs.yml b/mkdocs.yml index 6494359..63b4e94 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -49,6 +49,7 @@ pages: - 'eval': 'eval.md' - 'FIT_STATIC_FUNCTION': 'function.md' - 'FIT_STATIC_LAMBDA': 'lambda.md' + - 'if': 'if.md' - 'lift': 'lift.md' - 'is_callable': 'is_callable.md' - 'pack': 'pack.md' diff --git a/test/if.cpp b/test/if.cpp new file mode 100644 index 0000000..9788d25 --- /dev/null +++ b/test/if.cpp @@ -0,0 +1,84 @@ +#include +#include "test.h" + +#include +#include + + +struct is_5 +{ + template + constexpr bool operator()(T i) const + { + return i == 5; + } +}; + +struct is_not_5 +{ + template + constexpr bool operator()(T i) const + { + return i != 5; + } +}; + +template +struct test_int +{ + template + constexpr bool operator()(T x) const + { + return fit::conditional( + fit::if_(std::is_integral())(F()), + fit::always(true) + )(x); + } +}; + +FIT_TEST_CASE() +{ + FIT_TEST_CHECK(test_int()(5)); + FIT_TEST_CHECK(test_int()(5L)); + FIT_TEST_CHECK(test_int()(5.0)); + FIT_TEST_CHECK(test_int()(6.0)); + + FIT_TEST_CHECK(test_int()(6)); + FIT_TEST_CHECK(test_int()(6L)); + FIT_TEST_CHECK(test_int()(5.0)); + FIT_TEST_CHECK(test_int()(6.0)); + + FIT_STATIC_TEST_CHECK(test_int()(5)); + FIT_STATIC_TEST_CHECK(test_int()(5L)); + FIT_STATIC_TEST_CHECK(test_int()(5.0)); + FIT_STATIC_TEST_CHECK(test_int()(6.0)); + + FIT_STATIC_TEST_CHECK(test_int()(6)); + FIT_STATIC_TEST_CHECK(test_int()(6L)); + FIT_STATIC_TEST_CHECK(test_int()(5.0)); + FIT_STATIC_TEST_CHECK(test_int()(6.0)); +} + +struct sum_f +{ + template + constexpr int operator()(T x, T y) const + { + return fit::conditional( + fit::if_(std::is_integral())(fit::_ + fit::_), + fit::always(0) + )(x, y); + } +}; + +FIT_TEST_CASE() +{ + FIT_TEST_CHECK(sum_f()(1, 2) == 3); + FIT_TEST_CHECK(sum_f()(1.0, 2.0) == 0); + FIT_TEST_CHECK(sum_f()("", "") == 0); + + FIT_STATIC_TEST_CHECK(sum_f()(1, 2) == 3); + FIT_STATIC_TEST_CHECK(sum_f()("", "") == 0); +} + +