Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pfultz2/Fit
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 committed Jun 22, 2015
2 parents 586917f + 08ea9bf commit 674fee3
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions fit/if.h
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
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
84 changes: 84 additions & 0 deletions test/if.cpp
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);
}


0 comments on commit 674fee3

Please sign in to comment.