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)
9 * \author Andrey Semashev
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.
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.
21 #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_
22 #define BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_
24 #include <boost/mpl/if.hpp>
25 #include <boost/mpl/bool.hpp>
26 #include <boost/mpl/and.hpp>
27 #include <boost/type_traits/is_integral.hpp>
28 #include <boost/type_traits/is_unsigned.hpp>
29 #include <boost/log/detail/config.hpp>
30 #include <boost/log/detail/header.hpp>
32 #ifdef BOOST_HAS_PRAGMA_ONCE
38 BOOST_LOG_OPEN_NAMESPACE
42 //! The trait creates a common integral type suitable for comparison. This is mostly to silence compiler warnings like 'signed/unsigned mismatch'.
43 template< typename T, typename U, unsigned int TSizeV = sizeof(T), unsigned int USizeV = sizeof(U), bool TSmallerThanU = (sizeof(T) < sizeof(U)) >
44 struct make_common_integral_type
49 //! Specialization for case when \c T is smaller than \c U
50 template< typename T, typename U, unsigned int TSizeV, unsigned int USizeV >
51 struct make_common_integral_type< T, U, TSizeV, USizeV, true >
56 //! Specialization for the case when both types have the same size
57 template< typename T, typename U, unsigned int SizeV >
58 struct make_common_integral_type< T, U, SizeV, SizeV, false > :
69 //! Equality predicate
72 typedef bool result_type;
74 template< typename T, typename U >
75 bool operator() (T const& left, U const& right) const
77 return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
81 template< typename T, typename U >
82 static bool op(T const& left, U const& right, mpl::false_ const&)
84 return (left == right);
86 template< typename T, typename U >
87 static bool op(T const& left, U const& right, mpl::true_ const&)
89 typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
90 return static_cast< common_integral_type >(left) == static_cast< common_integral_type >(right);
94 //! Inequality predicate
97 typedef bool result_type;
99 template< typename T, typename U >
100 bool operator() (T const& left, U const& right) const
102 return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
106 template< typename T, typename U >
107 static bool op(T const& left, U const& right, mpl::false_ const&)
109 return (left != right);
111 template< typename T, typename U >
112 static bool op(T const& left, U const& right, mpl::true_ const&)
114 typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
115 return static_cast< common_integral_type >(left) != static_cast< common_integral_type >(right);
122 typedef bool result_type;
124 template< typename T, typename U >
125 bool operator() (T const& left, U const& right) const
127 return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
131 template< typename T, typename U >
132 static bool op(T const& left, U const& right, mpl::false_ const&)
134 return (left < right);
136 template< typename T, typename U >
137 static bool op(T const& left, U const& right, mpl::true_ const&)
139 typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
140 return static_cast< common_integral_type >(left) < static_cast< common_integral_type >(right);
144 //! Greater predicate
147 typedef bool result_type;
149 template< typename T, typename U >
150 bool operator() (T const& left, U const& right) const
152 return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
156 template< typename T, typename U >
157 static bool op(T const& left, U const& right, mpl::false_ const&)
159 return (left > right);
161 template< typename T, typename U >
162 static bool op(T const& left, U const& right, mpl::true_ const&)
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);
169 //! Less or equal predicate
172 typedef bool result_type;
174 template< typename T, typename U >
175 bool operator() (T const& left, U const& right) const
177 return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
181 template< typename T, typename U >
182 static bool op(T const& left, U const& right, mpl::false_ const&)
184 return (left <= right);
186 template< typename T, typename U >
187 static bool op(T const& left, U const& right, mpl::true_ const&)
189 typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
190 return static_cast< common_integral_type >(left) <= static_cast< common_integral_type >(right);
194 //! Greater or equal predicate
197 typedef bool result_type;
199 template< typename T, typename U >
200 bool operator() (T const& left, U const& right) const
202 return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type());
206 template< typename T, typename U >
207 static bool op(T const& left, U const& right, mpl::false_ const&)
209 return (left >= right);
211 template< typename T, typename U >
212 static bool op(T const& left, U const& right, mpl::true_ const&)
214 typedef typename aux::make_common_integral_type< T, U >::type common_integral_type;
215 return static_cast< common_integral_type >(left) >= static_cast< common_integral_type >(right);
219 BOOST_LOG_CLOSE_NAMESPACE // namespace log
223 #include <boost/log/detail/footer.hpp>
225 #endif // BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_