Sprout/sprout/functional/not1.hpp

73 lines
2.2 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2019 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
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)
=============================================================================*/
2012-04-09 16:38:55 +00:00
#ifndef SPROUT_FUNCTIONAL_NOT1_HPP
#define SPROUT_FUNCTIONAL_NOT1_HPP
#include <functional>
#include <utility>
2012-04-09 16:38:55 +00:00
#include <sprout/config.hpp>
#include <sprout/functional/type_traits/is_strict_function.hpp>
#include <sprout/utility/forward.hpp>
2012-04-09 16:38:55 +00:00
namespace sprout {
namespace detail {
template<typename Predicate, bool IsStrict = sprout::is_strict_unary_function<Predicate>::value>
class unary_negate_impl;
template<typename Predicate>
class unary_negate_impl<Predicate, true> {
public:
typedef typename Predicate::argument_type argument_type;
typedef bool result_type;
protected:
Predicate fn;
public:
explicit SPROUT_CONSTEXPR unary_negate_impl(Predicate const& pred)
: fn(pred)
{}
SPROUT_CONSTEXPR bool operator()(typename Predicate::argument_type const& x) const {
return !fn(x);
}
};
template<typename Predicate>
class unary_negate_impl<Predicate, false> {
protected:
Predicate fn;
public:
explicit SPROUT_CONSTEXPR unary_negate_impl(Predicate const& pred)
: fn(pred)
{}
template<typename T>
SPROUT_CONSTEXPR decltype(!std::declval<Predicate const&>()(std::declval<T>()))
operator()(T&& x) const {
return !fn(SPROUT_FORWARD(T, x));
}
};
} // namespace detail
2012-04-09 16:38:55 +00:00
// 20.8.8 negators
template<typename Predicate>
class unary_negate
: public sprout::detail::unary_negate_impl<Predicate>
{
2012-04-09 16:38:55 +00:00
public:
explicit SPROUT_CONSTEXPR unary_negate(Predicate const& pred)
: sprout::detail::unary_negate_impl<Predicate>(pred)
2012-04-09 16:38:55 +00:00
{}
};
template<typename Predicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR sprout::unary_negate<Predicate>
not1(Predicate const& pred) {
2012-04-09 16:38:55 +00:00
return sprout::unary_negate<Predicate>(pred);
}
} // namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_NOT1_HPP