in_range.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file in_range.hpp
  9. * \author Andrey Semashev
  10. * \date 30.03.2008
  11. *
  12. * This header contains a predicate for checking if the provided value is within a half-open range.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_IN_RANGE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_FUNCTIONAL_IN_RANGE_HPP_INCLUDED_
  16. #include <utility>
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/utility/functional/logical.hpp> // make_common_integral_type
  19. #include <boost/log/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. BOOST_LOG_OPEN_NAMESPACE
  25. //! The in_range functor
  26. struct in_range_fun
  27. {
  28. typedef bool result_type;
  29. template< typename T, typename U >
  30. bool operator() (T const& value, std::pair< U, U > const& rng) const
  31. {
  32. return op(value, rng, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  33. }
  34. private:
  35. template< typename T, typename U >
  36. static bool op(T const& value, std::pair< U, U > const& rng, mpl::false_ const&)
  37. {
  38. return (value >= rng.first && value < rng.second);
  39. }
  40. template< typename T, typename U >
  41. static bool op(T const& value, std::pair< U, U > const& rng, mpl::true_ const&)
  42. {
  43. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  44. return (static_cast< common_integral_type >(value) >= static_cast< common_integral_type >(rng.first))
  45. && (static_cast< common_integral_type >(value) < static_cast< common_integral_type >(rng.second));
  46. }
  47. };
  48. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  49. } // namespace boost
  50. #include <boost/log/detail/footer.hpp>
  51. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_IN_RANGE_HPP_INCLUDED_