-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:pfultz2/Fit
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<class IntegralConstant> | ||
/// constexpr auto if_(IntegralConstant); | ||
/// | ||
/// Requirements | ||
/// ------------ | ||
/// | ||
/// IntegralConstant must be: | ||
/// | ||
/// IntegralConstant | ||
/// | ||
/// Example | ||
/// ------- | ||
/// | ||
/// struct sum_f | ||
/// { | ||
/// template<class T> | ||
/// int operator()(T x, T y) const | ||
/// { | ||
/// return fit::conditional( | ||
/// fit::if_(std::is_integral<T>())(fit::_ + fit::_), | ||
/// fit::always(0) | ||
/// )(x, y); | ||
/// } | ||
/// }; | ||
/// assert(sum_f()(1, 2) == 3); | ||
/// assert(sum_f()("", "") == 0); | ||
/// | ||
|
||
#include <fit/always.h> | ||
#include <fit/detail/result_of.h> | ||
#include <fit/detail/forward.h> | ||
#include <fit/detail/delegate.h> | ||
#include <fit/detail/move.h> | ||
#include <fit/detail/static_const_var.h> | ||
|
||
namespace fit { | ||
|
||
namespace detail { | ||
|
||
template<class C, class...> | ||
struct if_depend | ||
: C | ||
{}; | ||
|
||
template<bool Cond, class F> | ||
struct if_adaptor : F | ||
{ | ||
FIT_INHERIT_CONSTRUCTOR(if_adaptor, F) | ||
}; | ||
|
||
template<class F> | ||
struct if_adaptor<false, F> | ||
{ | ||
template<class... Ts> | ||
constexpr if_adaptor(Ts&&...) | ||
{} | ||
}; | ||
|
||
template<bool Cond> | ||
struct make_if_f | ||
{ | ||
constexpr make_if_f() | ||
{} | ||
template<class F> | ||
constexpr if_adaptor<Cond, F> operator()(F f) const | ||
{ | ||
return if_adaptor<Cond, F>(fit::move(f)); | ||
} | ||
}; | ||
|
||
struct if_f | ||
{ | ||
constexpr if_f() | ||
{} | ||
template<class Cond, bool B=Cond::type::value> | ||
constexpr make_if_f<B> operator()(Cond) const | ||
{ | ||
return {}; | ||
} | ||
}; | ||
|
||
} | ||
|
||
FIT_DECLARE_STATIC_VAR(if_, detail::if_f); | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <fit/if.h> | ||
#include "test.h" | ||
|
||
#include <fit/conditional.h> | ||
#include <fit/placeholders.h> | ||
|
||
|
||
struct is_5 | ||
{ | ||
template<class T> | ||
constexpr bool operator()(T i) const | ||
{ | ||
return i == 5; | ||
} | ||
}; | ||
|
||
struct is_not_5 | ||
{ | ||
template<class T> | ||
constexpr bool operator()(T i) const | ||
{ | ||
return i != 5; | ||
} | ||
}; | ||
|
||
template<class F> | ||
struct test_int | ||
{ | ||
template<class T> | ||
constexpr bool operator()(T x) const | ||
{ | ||
return fit::conditional( | ||
fit::if_(std::is_integral<T>())(F()), | ||
fit::always(true) | ||
)(x); | ||
} | ||
}; | ||
|
||
FIT_TEST_CASE() | ||
{ | ||
FIT_TEST_CHECK(test_int<is_5>()(5)); | ||
FIT_TEST_CHECK(test_int<is_5>()(5L)); | ||
FIT_TEST_CHECK(test_int<is_5>()(5.0)); | ||
FIT_TEST_CHECK(test_int<is_5>()(6.0)); | ||
|
||
FIT_TEST_CHECK(test_int<is_not_5>()(6)); | ||
FIT_TEST_CHECK(test_int<is_not_5>()(6L)); | ||
FIT_TEST_CHECK(test_int<is_not_5>()(5.0)); | ||
FIT_TEST_CHECK(test_int<is_not_5>()(6.0)); | ||
|
||
FIT_STATIC_TEST_CHECK(test_int<is_5>()(5)); | ||
FIT_STATIC_TEST_CHECK(test_int<is_5>()(5L)); | ||
FIT_STATIC_TEST_CHECK(test_int<is_5>()(5.0)); | ||
FIT_STATIC_TEST_CHECK(test_int<is_5>()(6.0)); | ||
|
||
FIT_STATIC_TEST_CHECK(test_int<is_not_5>()(6)); | ||
FIT_STATIC_TEST_CHECK(test_int<is_not_5>()(6L)); | ||
FIT_STATIC_TEST_CHECK(test_int<is_not_5>()(5.0)); | ||
FIT_STATIC_TEST_CHECK(test_int<is_not_5>()(6.0)); | ||
} | ||
|
||
struct sum_f | ||
{ | ||
template<class T> | ||
constexpr int operator()(T x, T y) const | ||
{ | ||
return fit::conditional( | ||
fit::if_(std::is_integral<T>())(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); | ||
} | ||
|
||
|