logical.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 logical.hpp
  9. * \author Andrey Semashev
  10. * \date 30.03.2008
  11. *
  12. * This header contains logical predicates for value comparison, analogous to \c std::less, \c std::greater
  13. * and others. The main difference from the standard equivalents is that the predicates defined in this
  14. * header are not templates and therefore do not require a fixed argument type. Furthermore, both arguments
  15. * may have different types, in which case the comparison is performed without type conversion.
  16. *
  17. * \note In case if arguments are integral, the conversion is performed according to the standard C++ rules
  18. * in order to avoid warnings from the compiler.
  19. */
  20. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_
  21. #define BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_
  22. #include <boost/mpl/if.hpp>
  23. #include <boost/mpl/bool.hpp>
  24. #include <boost/mpl/and.hpp>
  25. #include <boost/type_traits/is_integral.hpp>
  26. #include <boost/type_traits/is_unsigned.hpp>
  27. #include <boost/log/detail/config.hpp>
  28. #include <boost/log/detail/header.hpp>
  29. #ifdef BOOST_HAS_PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32. namespace boost {
  33. BOOST_LOG_OPEN_NAMESPACE
  34. namespace aux {
  35. //! The trait creates a common integral type suitable for comparison. This is mostly to silence compiler warnings like 'signed/unsigned mismatch'.
  36. template< typename T, typename U, unsigned int TSizeV = sizeof(T), unsigned int USizeV = sizeof(U), bool TSmallerThanU = (sizeof(T) < sizeof(U)) >
  37. struct make_common_integral_type
  38. {
  39. typedef T type;
  40. };
  41. //! Specialization for case when \c T is smaller than \c U
  42. template< typename T, typename U, unsigned int TSizeV, unsigned int USizeV >
  43. struct make_common_integral_type< T, U, TSizeV, USizeV, true >
  44. {
  45. typedef U type;
  46. };
  47. //! Specialization for the case when both types have the same size
  48. template< typename T, typename U, unsigned int SizeV >
  49. struct make_common_integral_type< T, U, SizeV, SizeV, false > :
  50. public mpl::if_<
  51. is_unsigned< T >,
  52. T,
  53. U
  54. >
  55. {
  56. };
  57. } // namespace aux
  58. //! Equality predicate
  59. struct equal_to
  60. {
  61. typedef bool result_type;
  62. template< typename T, typename U >
  63. bool operator() (T const& left, U const& right) const
  64. {
  65. return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  66. }
  67. private:
  68. template< typename T, typename U >
  69. static bool op(T const& left, U const& right, mpl::false_ const&)
  70. {
  71. return (left == right);
  72. }
  73. template< typename T, typename U >
  74. static bool op(T const& left, U const& right, mpl::true_ const&)
  75. {
  76. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  77. return static_cast< common_integral_type >(left) == static_cast< common_integral_type >(right);
  78. }
  79. };
  80. //! Inequality predicate
  81. struct not_equal_to
  82. {
  83. typedef bool result_type;
  84. template< typename T, typename U >
  85. bool operator() (T const& left, U const& right) const
  86. {
  87. return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  88. }
  89. private:
  90. template< typename T, typename U >
  91. static bool op(T const& left, U const& right, mpl::false_ const&)
  92. {
  93. return (left != right);
  94. }
  95. template< typename T, typename U >
  96. static bool op(T const& left, U const& right, mpl::true_ const&)
  97. {
  98. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  99. return static_cast< common_integral_type >(left) != static_cast< common_integral_type >(right);
  100. }
  101. };
  102. //! Less predicate
  103. struct less
  104. {
  105. typedef bool result_type;
  106. template< typename T, typename U >
  107. bool operator() (T const& left, U const& right) const
  108. {
  109. return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  110. }
  111. private:
  112. template< typename T, typename U >
  113. static bool op(T const& left, U const& right, mpl::false_ const&)
  114. {
  115. return (left < right);
  116. }
  117. template< typename T, typename U >
  118. static bool op(T const& left, U const& right, mpl::true_ const&)
  119. {
  120. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  121. return static_cast< common_integral_type >(left) < static_cast< common_integral_type >(right);
  122. }
  123. };
  124. //! Greater predicate
  125. struct greater
  126. {
  127. typedef bool result_type;
  128. template< typename T, typename U >
  129. bool operator() (T const& left, U const& right) const
  130. {
  131. return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  132. }
  133. private:
  134. template< typename T, typename U >
  135. static bool op(T const& left, U const& right, mpl::false_ const&)
  136. {
  137. return (left > right);
  138. }
  139. template< typename T, typename U >
  140. static bool op(T const& left, U const& right, mpl::true_ const&)
  141. {
  142. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  143. return static_cast< common_integral_type >(left) > static_cast< common_integral_type >(right);
  144. }
  145. };
  146. //! Less or equal predicate
  147. struct less_equal
  148. {
  149. typedef bool result_type;
  150. template< typename T, typename U >
  151. bool operator() (T const& left, U const& right) const
  152. {
  153. return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  154. }
  155. private:
  156. template< typename T, typename U >
  157. static bool op(T const& left, U const& right, mpl::false_ const&)
  158. {
  159. return (left <= right);
  160. }
  161. template< typename T, typename U >
  162. static bool op(T const& left, U const& right, mpl::true_ const&)
  163. {
  164. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  165. return static_cast< common_integral_type >(left) <= static_cast< common_integral_type >(right);
  166. }
  167. };
  168. //! Greater or equal predicate
  169. struct greater_equal
  170. {
  171. typedef bool result_type;
  172. template< typename T, typename U >
  173. bool operator() (T const& left, U const& right) const
  174. {
  175. return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
  176. }
  177. private:
  178. template< typename T, typename U >
  179. static bool op(T const& left, U const& right, mpl::false_ const&)
  180. {
  181. return (left >= right);
  182. }
  183. template< typename T, typename U >
  184. static bool op(T const& left, U const& right, mpl::true_ const&)
  185. {
  186. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  187. return static_cast< common_integral_type >(left) >= static_cast< common_integral_type >(right);
  188. }
  189. };
  190. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  191. } // namespace boost
  192. #include <boost/log/detail/footer.hpp>
  193. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_