Skip to content

Commit

Permalink
Merge pull request #17 from pfultz2/if_c
Browse files Browse the repository at this point in the history
Add if_c
  • Loading branch information
pfultz2 committed Aug 20, 2015
2 parents 1d4aa72 + d95064a commit cd68d74
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
18 changes: 17 additions & 1 deletion fit/if.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@
/// Description
/// -----------
///
/// The `if_` function applies a boolen condition to the function.
/// The `if_` function applies a boolen condition to the function. The `if_c`
/// version can be used to give a boolean condtion directly(intead of relying
/// on dependent typing).
///
/// Synopsis
/// --------
///
/// template<class IntegralConstant>
/// constexpr auto if_(IntegralConstant);
///
/// template<bool B, class F>
/// constexpr auto if_c(F);
///
/// Requirements
/// ------------
///
/// IntegralConstant must be:
///
/// IntegralConstant
///
/// F must be:
///
/// FunctionObject
/// MoveConstructible
///
/// Example
/// -------
///
Expand Down Expand Up @@ -102,6 +112,12 @@ struct if_f

}

template<bool B, class F>
constexpr detail::if_adaptor<B, F> if_c(F f)
{
return detail::if_adaptor<B, F>(fit::move(f));
}

FIT_DECLARE_STATIC_VAR(if_, detail::if_f);

}
Expand Down
59 changes: 59 additions & 0 deletions test/if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ FIT_TEST_CASE()
FIT_STATIC_TEST_CHECK(test_int<is_not_5>()(6.0));
}

template<class F>
struct test_int_c
{
template<class T>
constexpr bool operator()(T x) const
{
return fit::conditional(
fit::if_c<std::is_integral<T>::value>(F()),
fit::always(true)
)(x);
}
};

FIT_TEST_CASE()
{
FIT_TEST_CHECK(test_int_c<is_5>()(5));
FIT_TEST_CHECK(test_int_c<is_5>()(5L));
FIT_TEST_CHECK(test_int_c<is_5>()(5.0));
FIT_TEST_CHECK(test_int_c<is_5>()(6.0));

FIT_TEST_CHECK(test_int_c<is_not_5>()(6));
FIT_TEST_CHECK(test_int_c<is_not_5>()(6L));
FIT_TEST_CHECK(test_int_c<is_not_5>()(5.0));
FIT_TEST_CHECK(test_int_c<is_not_5>()(6.0));

FIT_STATIC_TEST_CHECK(test_int_c<is_5>()(5));
FIT_STATIC_TEST_CHECK(test_int_c<is_5>()(5L));
FIT_STATIC_TEST_CHECK(test_int_c<is_5>()(5.0));
FIT_STATIC_TEST_CHECK(test_int_c<is_5>()(6.0));

FIT_STATIC_TEST_CHECK(test_int_c<is_not_5>()(6));
FIT_STATIC_TEST_CHECK(test_int_c<is_not_5>()(6L));
FIT_STATIC_TEST_CHECK(test_int_c<is_not_5>()(5.0));
FIT_STATIC_TEST_CHECK(test_int_c<is_not_5>()(6.0));
}

struct sum_f
{
template<class T>
Expand All @@ -82,3 +118,26 @@ FIT_TEST_CASE()
}


struct sum_f_c
{
template<class T>
constexpr int operator()(T x, T y) const
{
return fit::conditional(
fit::if_c<std::is_integral<T>::value>(fit::_ + fit::_),
fit::always(0)
)(x, y);
}
};

FIT_TEST_CASE()
{
FIT_TEST_CHECK(sum_f_c()(1, 2) == 3);
FIT_TEST_CHECK(sum_f_c()(1.0, 2.0) == 0);
FIT_TEST_CHECK(sum_f_c()("", "") == 0);

FIT_STATIC_TEST_CHECK(sum_f_c()(1, 2) == 3);
FIT_STATIC_TEST_CHECK(sum_f_c()("", "") == 0);
}


0 comments on commit cd68d74

Please sign in to comment.