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