mirror of
https://github.com/boostorg/hof.git
synced 2026-01-31 08:12:19 +00:00
@@ -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)
|
||||
|
||||
109
fit/if.h
Normal file
109
fit/if.h
Normal file
@@ -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
|
||||
@@ -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'
|
||||
|
||||
84
test/if.cpp
Normal file
84
test/if.cpp
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user