in_range.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/type_traits/is_integral.hpp>
  18. #include <boost/type_traits/integral_constant.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/utility/functional/logical.hpp> // make_common_integral_type
  21. #include <boost/log/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. BOOST_LOG_OPEN_NAMESPACE
  27. //! The in_range functor
  28. struct in_range_fun
  29. {
  30. typedef bool result_type;
  31. template< typename T, typename U >
  32. bool operator() (T const& value, std::pair< U, U > const& rng) const
  33. {
  34. return op(value, rng, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  35. }
  36. private:
  37. template< typename T, typename U >
  38. static bool op(T const& value, std::pair< U, U > const& rng, false_type)
  39. {
  40. return (value >= rng.first && value < rng.second);
  41. }
  42. template< typename T, typename U >
  43. static bool op(T const& value, std::pair< U, U > const& rng, true_type)
  44. {
  45. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  46. return (static_cast< common_integral_type >(value) >= static_cast< common_integral_type >(rng.first))
  47. && (static_cast< common_integral_type >(value) < static_cast< common_integral_type >(rng.second));
  48. }
  49. };
  50. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  51. } // namespace boost
  52. #include <boost/log/detail/footer.hpp>
  53. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_IN_RANGE_HPP_INCLUDED_