logical.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/type_traits/is_integral.hpp>
  23. #include <boost/type_traits/is_unsigned.hpp>
  24. #include <boost/type_traits/conditional.hpp>
  25. #include <boost/type_traits/integral_constant.hpp>
  26. #include <boost/log/detail/config.hpp>
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. namespace aux {
  34. //! The trait creates a common integral type suitable for comparison. This is mostly to silence compiler warnings like 'signed/unsigned mismatch'.
  35. template< typename T, typename U, unsigned int TSizeV = sizeof(T), unsigned int USizeV = sizeof(U), bool TSmallerThanU = (sizeof(T) < sizeof(U)) >
  36. struct make_common_integral_type
  37. {
  38. typedef T type;
  39. };
  40. //! Specialization for case when \c T is smaller than \c U
  41. template< typename T, typename U, unsigned int TSizeV, unsigned int USizeV >
  42. struct make_common_integral_type< T, U, TSizeV, USizeV, true >
  43. {
  44. typedef U type;
  45. };
  46. //! Specialization for the case when both types have the same size
  47. template< typename T, typename U, unsigned int SizeV >
  48. struct make_common_integral_type< T, U, SizeV, SizeV, false > :
  49. public boost::conditional<
  50. is_unsigned< T >::value,
  51. T,
  52. U
  53. >
  54. {
  55. };
  56. } // namespace aux
  57. //! Equality predicate
  58. struct equal_to
  59. {
  60. typedef bool result_type;
  61. template< typename T, typename U >
  62. bool operator() (T const& left, U const& right) const
  63. {
  64. return op(left, right, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  65. }
  66. private:
  67. template< typename T, typename U >
  68. static bool op(T const& left, U const& right, false_type)
  69. {
  70. return (left == right);
  71. }
  72. template< typename T, typename U >
  73. static bool op(T const& left, U const& right, true_type)
  74. {
  75. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  76. return static_cast< common_integral_type >(left) == static_cast< common_integral_type >(right);
  77. }
  78. };
  79. //! Inequality predicate
  80. struct not_equal_to
  81. {
  82. typedef bool result_type;
  83. template< typename T, typename U >
  84. bool operator() (T const& left, U const& right) const
  85. {
  86. return op(left, right, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  87. }
  88. private:
  89. template< typename T, typename U >
  90. static bool op(T const& left, U const& right, false_type)
  91. {
  92. return (left != right);
  93. }
  94. template< typename T, typename U >
  95. static bool op(T const& left, U const& right, true_type)
  96. {
  97. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  98. return static_cast< common_integral_type >(left) != static_cast< common_integral_type >(right);
  99. }
  100. };
  101. //! Less predicate
  102. struct less
  103. {
  104. typedef bool result_type;
  105. template< typename T, typename U >
  106. bool operator() (T const& left, U const& right) const
  107. {
  108. return op(left, right, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  109. }
  110. private:
  111. template< typename T, typename U >
  112. static bool op(T const& left, U const& right, false_type)
  113. {
  114. return (left < right);
  115. }
  116. template< typename T, typename U >
  117. static bool op(T const& left, U const& right, true_type)
  118. {
  119. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  120. return static_cast< common_integral_type >(left) < static_cast< common_integral_type >(right);
  121. }
  122. };
  123. //! Greater predicate
  124. struct greater
  125. {
  126. typedef bool result_type;
  127. template< typename T, typename U >
  128. bool operator() (T const& left, U const& right) const
  129. {
  130. return op(left, right, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  131. }
  132. private:
  133. template< typename T, typename U >
  134. static bool op(T const& left, U const& right, false_type)
  135. {
  136. return (left > right);
  137. }
  138. template< typename T, typename U >
  139. static bool op(T const& left, U const& right, true_type)
  140. {
  141. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  142. return static_cast< common_integral_type >(left) > static_cast< common_integral_type >(right);
  143. }
  144. };
  145. //! Less or equal predicate
  146. struct less_equal
  147. {
  148. typedef bool result_type;
  149. template< typename T, typename U >
  150. bool operator() (T const& left, U const& right) const
  151. {
  152. return op(left, right, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  153. }
  154. private:
  155. template< typename T, typename U >
  156. static bool op(T const& left, U const& right, false_type)
  157. {
  158. return (left <= right);
  159. }
  160. template< typename T, typename U >
  161. static bool op(T const& left, U const& right, true_type)
  162. {
  163. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  164. return static_cast< common_integral_type >(left) <= static_cast< common_integral_type >(right);
  165. }
  166. };
  167. //! Greater or equal predicate
  168. struct greater_equal
  169. {
  170. typedef bool result_type;
  171. template< typename T, typename U >
  172. bool operator() (T const& left, U const& right) const
  173. {
  174. return op(left, right, integral_constant< bool, is_integral< T >::value && is_integral< U >::value >());
  175. }
  176. private:
  177. template< typename T, typename U >
  178. static bool op(T const& left, U const& right, false_type)
  179. {
  180. return (left >= right);
  181. }
  182. template< typename T, typename U >
  183. static bool op(T const& left, U const& right, true_type)
  184. {
  185. typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
  186. return static_cast< common_integral_type >(left) >= static_cast< common_integral_type >(right);
  187. }
  188. };
  189. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  190. } // namespace boost
  191. #include <boost/log/detail/footer.hpp>
  192. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_