]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
21 | #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_ | |
22 | #define BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_ | |
23 | ||
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> | |
31 | ||
32 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
33 | #pragma once | |
34 | #endif | |
35 | ||
36 | namespace boost { | |
37 | ||
38 | BOOST_LOG_OPEN_NAMESPACE | |
39 | ||
40 | namespace aux { | |
41 | ||
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 | |
45 | { | |
46 | typedef T type; | |
47 | }; | |
48 | ||
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 > | |
52 | { | |
53 | typedef U type; | |
54 | }; | |
55 | ||
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 > : | |
59 | public mpl::if_< | |
60 | is_unsigned< T >, | |
61 | T, | |
62 | U | |
63 | > | |
64 | { | |
65 | }; | |
66 | ||
67 | } // namespace aux | |
68 | ||
69 | //! Equality predicate | |
70 | struct equal_to | |
71 | { | |
72 | typedef bool result_type; | |
73 | ||
74 | template< typename T, typename U > | |
75 | bool operator() (T const& left, U const& right) const | |
76 | { | |
77 | return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type()); | |
78 | } | |
79 | ||
80 | private: | |
81 | template< typename T, typename U > | |
82 | static bool op(T const& left, U const& right, mpl::false_ const&) | |
83 | { | |
84 | return (left == right); | |
85 | } | |
86 | template< typename T, typename U > | |
87 | static bool op(T const& left, U const& right, mpl::true_ const&) | |
88 | { | |
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); | |
91 | } | |
92 | }; | |
93 | ||
94 | //! Inequality predicate | |
95 | struct not_equal_to | |
96 | { | |
97 | typedef bool result_type; | |
98 | ||
99 | template< typename T, typename U > | |
100 | bool operator() (T const& left, U const& right) const | |
101 | { | |
102 | return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type()); | |
103 | } | |
104 | ||
105 | private: | |
106 | template< typename T, typename U > | |
107 | static bool op(T const& left, U const& right, mpl::false_ const&) | |
108 | { | |
109 | return (left != right); | |
110 | } | |
111 | template< typename T, typename U > | |
112 | static bool op(T const& left, U const& right, mpl::true_ const&) | |
113 | { | |
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); | |
116 | } | |
117 | }; | |
118 | ||
119 | //! Less predicate | |
120 | struct less | |
121 | { | |
122 | typedef bool result_type; | |
123 | ||
124 | template< typename T, typename U > | |
125 | bool operator() (T const& left, U const& right) const | |
126 | { | |
127 | return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type()); | |
128 | } | |
129 | ||
130 | private: | |
131 | template< typename T, typename U > | |
132 | static bool op(T const& left, U const& right, mpl::false_ const&) | |
133 | { | |
134 | return (left < right); | |
135 | } | |
136 | template< typename T, typename U > | |
137 | static bool op(T const& left, U const& right, mpl::true_ const&) | |
138 | { | |
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); | |
141 | } | |
142 | }; | |
143 | ||
144 | //! Greater predicate | |
145 | struct greater | |
146 | { | |
147 | typedef bool result_type; | |
148 | ||
149 | template< typename T, typename U > | |
150 | bool operator() (T const& left, U const& right) const | |
151 | { | |
152 | return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type()); | |
153 | } | |
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 | ||
169 | //! Less or equal predicate | |
170 | struct less_equal | |
171 | { | |
172 | typedef bool result_type; | |
173 | ||
174 | template< typename T, typename U > | |
175 | bool operator() (T const& left, U const& right) const | |
176 | { | |
177 | return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type()); | |
178 | } | |
179 | ||
180 | private: | |
181 | template< typename T, typename U > | |
182 | static bool op(T const& left, U const& right, mpl::false_ const&) | |
183 | { | |
184 | return (left <= right); | |
185 | } | |
186 | template< typename T, typename U > | |
187 | static bool op(T const& left, U const& right, mpl::true_ const&) | |
188 | { | |
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); | |
191 | } | |
192 | }; | |
193 | ||
194 | //! Greater or equal predicate | |
195 | struct greater_equal | |
196 | { | |
197 | typedef bool result_type; | |
198 | ||
199 | template< typename T, typename U > | |
200 | bool operator() (T const& left, U const& right) const | |
201 | { | |
202 | return op(left, right, typename mpl::and_< is_integral< T >, is_integral< U > >::type()); | |
203 | } | |
204 | ||
205 | private: | |
206 | template< typename T, typename U > | |
207 | static bool op(T const& left, U const& right, mpl::false_ const&) | |
208 | { | |
209 | return (left >= right); | |
210 | } | |
211 | template< typename T, typename U > | |
212 | static bool op(T const& left, U const& right, mpl::true_ const&) | |
213 | { | |
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); | |
216 | } | |
217 | }; | |
218 | ||
219 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
220 | ||
221 | } // namespace boost | |
222 | ||
223 | #include <boost/log/detail/footer.hpp> | |
224 | ||
225 | #endif // BOOST_LOG_UTILITY_FUNCTIONAL_LOGICAL_HPP_INCLUDED_ |