Skip to content

Commit

Permalink
Use limit to limit the number of paramters for lazy or any function
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 committed Mar 23, 2016
1 parent 439148f commit 40f68b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
20 changes: 19 additions & 1 deletion include/fit/limit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,35 @@
#include <fit/detail/delegate.hpp>
#include <fit/detail/move.hpp>
#include <fit/detail/static_const_var.hpp>
#include <fit/always.hpp>
#include <cstdint>

namespace fit {

namespace detail {

// TODO: Make this work with fit_rewritable1_tag
template<std::size_t N, class F>
struct limit_adaptor : detail::callable_base<F>
{
typedef std::integral_constant<std::size_t, N> fit_function_param_limit;
FIT_INHERIT_CONSTRUCTOR(limit_adaptor, detail::callable_base<F>)

template<class... Ts>
constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
{
return always_ref(*this)(xs...);
}

FIT_RETURNS_CLASS(limit_adaptor);

template<class... Ts, class=typename std::enable_if<(sizeof...(Ts) <= N)>::type>
constexpr FIT_SFINAE_RESULT(const detail::callable_base<F>&, id_<Ts>...)
operator()(Ts&&... xs) const FIT_SFINAE_RETURNS
(
(FIT_MANGLE_CAST(const detail::callable_base<F>&)(FIT_CONST_THIS->base_function(xs...)))
(FIT_FORWARD(Ts)(xs)...)
);

};

template<std::size_t N>
Expand Down
10 changes: 10 additions & 0 deletions test/match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <fit/static.hpp>
#include <fit/lambda.hpp>
#include <fit/placeholders.hpp>
#include <fit/limit.hpp>
#include "test.hpp"

#include <memory>
Expand Down Expand Up @@ -169,3 +170,12 @@ FIT_TEST_CASE()
// FIT_TEST_CHECK(fit::match(negate, sub)(0, 1) == sub(0, 1));
}

FIT_TEST_CASE()
{
const auto negate = fit::limit_c<1>(!fit::_1);
const auto sub = fit::limit_c<2>(fit::_1 - fit::_2);
FIT_TEST_CHECK(fit::match(negate, sub)(0) == negate(0));
// This test will pass because we have limited the number of parameters.
FIT_TEST_CHECK(fit::match(negate, sub)(0, 1) == sub(0, 1));
}

0 comments on commit 40f68b7

Please sign in to comment.