Skip to content

Commit f6913c1

Browse files
committed
Reduced macro-madness in boolean.hpp
1 parent 80e6491 commit f6913c1

File tree

2 files changed

+79
-112
lines changed

2 files changed

+79
-112
lines changed

include/nonstd/boolean.hpp

Lines changed: 28 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -53,89 +53,16 @@
5353
#define boolean_CPP17_OR_GREATER ( boolean_CPLUSPLUS >= 201703L )
5454
#define boolean_CPP20_OR_GREATER ( boolean_CPLUSPLUS >= 202000L )
5555

56-
// C++ language version (represent 98 as 3):
57-
58-
#define boolean_CPLUSPLUS_V ( boolean_CPLUSPLUS / 100 - (boolean_CPLUSPLUS > 200000 ? 2000 : 1994) )
59-
60-
// half-open range [lo..hi):
61-
#define boolean_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
62-
63-
// Compiler versions:
64-
65-
// MSVC++ 6.0 _MSC_VER == 1200 boolean_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
66-
// MSVC++ 7.0 _MSC_VER == 1300 boolean_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
67-
// MSVC++ 7.1 _MSC_VER == 1310 boolean_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
68-
// MSVC++ 8.0 _MSC_VER == 1400 boolean_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
69-
// MSVC++ 9.0 _MSC_VER == 1500 boolean_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
70-
// MSVC++ 10.0 _MSC_VER == 1600 boolean_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
71-
// MSVC++ 11.0 _MSC_VER == 1700 boolean_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
72-
// MSVC++ 12.0 _MSC_VER == 1800 boolean_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
73-
// MSVC++ 14.0 _MSC_VER == 1900 boolean_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
74-
// MSVC++ 14.1 _MSC_VER >= 1910 boolean_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
75-
// MSVC++ 14.2 _MSC_VER >= 1920 boolean_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
56+
// MSVC version:
7657

7758
#if defined(_MSC_VER ) && !defined(__clang__)
78-
# define boolean_COMPILER_MSVC_VER (_MSC_VER )
79-
# define boolean_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
80-
# define boolean_COMPILER_MSVC_VERSION_FULL (_MSC_VER - 100 * ( 5 + (_MSC_VER < 1900 ) ) )
81-
#else
82-
# define boolean_COMPILER_MSVC_VER 0
83-
# define boolean_COMPILER_MSVC_VERSION 0
84-
# define boolean_COMPILER_MSVC_VERSION_FULL 0
85-
#endif
86-
87-
#define boolean_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
88-
89-
// AppleClang 7.0.0 __apple_build_version__ == 7000172 boolean_COMPILER_APPLECLANG_VERSION == 700 (Xcode 7.0, 7.0.1) (LLVM 3.7.0)
90-
// AppleClang 7.0.0 __apple_build_version__ == 7000176 boolean_COMPILER_APPLECLANG_VERSION == 700 (Xcode 7.1) (LLVM 3.7.0)
91-
// AppleClang 7.0.2 __apple_build_version__ == 7000181 boolean_COMPILER_APPLECLANG_VERSION == 702 (Xcode 7.2, 7.2.1) (LLVM 3.7.0)
92-
// AppleClang 7.3.0 __apple_build_version__ == 7030029 boolean_COMPILER_APPLECLANG_VERSION == 730 (Xcode 7.3) (LLVM 3.8.0)
93-
// AppleClang 7.3.0 __apple_build_version__ == 7030031 boolean_COMPILER_APPLECLANG_VERSION == 730 (Xcode 7.3.1) (LLVM 3.8.0)
94-
// AppleClang 8.0.0 __apple_build_version__ == 8000038 boolean_COMPILER_APPLECLANG_VERSION == 800 (Xcode 8.0) (LLVM 3.9.0)
95-
// AppleClang 8.0.0 __apple_build_version__ == 8000042 boolean_COMPILER_APPLECLANG_VERSION == 800 (Xcode 8.1, 8.2, 8.2.1) (LLVM 3.9.0)
96-
// AppleClang 8.1.0 __apple_build_version__ == 8020038 boolean_COMPILER_APPLECLANG_VERSION == 810 (Xcode 8.3) (LLVM 3.9.0)
97-
// AppleClang 8.1.0 __apple_build_version__ == 8020041 boolean_COMPILER_APPLECLANG_VERSION == 810 (Xcode 8.3.1) (LLVM 3.9.0)
98-
// AppleClang 8.1.0 __apple_build_version__ == 8020042 boolean_COMPILER_APPLECLANG_VERSION == 810 (Xcode 8.3.2, 8.3.3) (LLVM 3.9.0)
99-
// AppleClang 9.0.0 __apple_build_version__ == 9000037 boolean_COMPILER_APPLECLANG_VERSION == 900 (Xcode 9.0) (LLVM 4.0.0?)
100-
// AppleClang 9.0.0 __apple_build_version__ == 9000038 boolean_COMPILER_APPLECLANG_VERSION == 900 (Xcode 9.1) (LLVM 4.0.0?)
101-
// AppleClang 9.0.0 __apple_build_version__ == 9000039 boolean_COMPILER_APPLECLANG_VERSION == 900 (Xcode 9.2) (LLVM 4.0.0?)
102-
// AppleClang 9.1.0 __apple_build_version__ == 9020039 boolean_COMPILER_APPLECLANG_VERSION == 910 (Xcode 9.3, 9.3.1) (LLVM 5.0.2?)
103-
// AppleClang 9.1.0 __apple_build_version__ == 9020039 boolean_COMPILER_APPLECLANG_VERSION == 910 (Xcode 9.4, 9.4.1) (LLVM 5.0.2?)
104-
// AppleClang 10.0.0 __apple_build_version__ == 10001145 boolean_COMPILER_APPLECLANG_VERSION == 1000 (Xcode 10.0, 10.1) (LLVM 6.0.1?)
105-
// AppleClang 10.0.1 __apple_build_version__ == 10010046 boolean_COMPILER_APPLECLANG_VERSION == 1001 (Xcode 10.2, 10.2.1, 10.3) (LLVM 7.0.0?)
106-
// AppleClang 11.0.0 __apple_build_version__ == 11000033 boolean_COMPILER_APPLECLANG_VERSION == 1100 (Xcode 11.1, 11.2, 11.3, 11.3.1) (LLVM 8.0.0?)
107-
// AppleClang 11.0.3 __apple_build_version__ == 11030032 boolean_COMPILER_APPLECLANG_VERSION == 1103 (Xcode 11.4, 11.4.1, 11.5, 11.6) (LLVM 9.0.0?)
108-
109-
#if defined( __apple_build_version__ )
110-
# define boolean_COMPILER_APPLECLANG_VERSION boolean_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
111-
# define boolean_COMPILER_CLANG_VERSION 0
112-
#elif defined( __clang__ )
113-
# define boolean_COMPILER_APPLECLANG_VERSION 0
114-
# define boolean_COMPILER_CLANG_VERSION boolean_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
115-
#else
116-
# define boolean_COMPILER_APPLECLANG_VERSION 0
117-
# define boolean_COMPILER_CLANG_VERSION 0
118-
#endif
119-
120-
#if defined(__GNUC__) && !defined(__clang__)
121-
# define boolean_COMPILER_GNUC_VERSION boolean_COMPILER_VERSION( __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ )
59+
# define boolean_COMPILER_MSVC_VER (_MSC_VER )
12260
#else
123-
# define boolean_COMPILER_GNUC_VERSION 0
61+
# define boolean_COMPILER_MSVC_VER 0
12462
#endif
12563

12664
// Presence of language and library features:
12765

128-
#define boolean_HAVE( feature ) ( boolean_HAVE_##feature )
129-
130-
#ifdef _HAS_CPP0X
131-
# define boolean_HAS_CPP0X _HAS_CPP0X
132-
#else
133-
# define boolean_HAS_CPP0X 0
134-
#endif
135-
136-
#define boolean_CPP11_90 (boolean_CPP11_OR_GREATER || boolean_COMPILER_MSVC_VER >= 1500)
137-
#define boolean_CPP11_100 (boolean_CPP11_OR_GREATER || boolean_COMPILER_MSVC_VER >= 1600)
138-
#define boolean_CPP11_110 (boolean_CPP11_OR_GREATER || boolean_COMPILER_MSVC_VER >= 1700)
13966
#define boolean_CPP11_120 (boolean_CPP11_OR_GREATER || boolean_COMPILER_MSVC_VER >= 1800)
14067
#define boolean_CPP11_140 (boolean_CPP11_OR_GREATER || boolean_COMPILER_MSVC_VER >= 1900)
14168

@@ -145,17 +72,8 @@
14572

14673
#define boolean_HAVE_CONSTEXPR_11 boolean_CPP11_140
14774
#define boolean_HAVE_NOEXCEPT boolean_CPP11_140
75+
#define boolean_HAVE_DEFAULT_FN_TPL_ARGS boolean_CPP11_120
14876
#define boolean_HAVE_EXPLICIT_CONVERSION boolean_CPP11_120
149-
#define boolean_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG boolean_CPP11_120
150-
#define boolean_HAVE_STATIC_ASSERT boolean_CPP11_90
151-
152-
// Presence of C++11 library features:
153-
154-
#define boolean_HAVE_TYPE_TRAITS boolean_CPP11_90
155-
#define boolean_HAVE_TR1_TYPE_TRAITS (!! boolean_COMPILER_GNUC_VERSION )
156-
157-
#define boolean_HAVE_IS_SAME boolean_HAVE_TYPE_TRAITS
158-
#define boolean_HAVE_IS_SAME_TR1 boolean_HAVE_TR1_TYPE_TRAITS
15977

16078
// Presence of C++17 language features:
16179

@@ -187,34 +105,21 @@
187105
# define boolean_explicit_cv /*explicit*/
188106
#endif
189107

190-
// Additional includes:
108+
#define boolean_CPP98_FALLBACK ( !boolean_HAVE_DEFAULT_FN_TPL_ARGS )
191109

192-
#if boolean_HAVE( TYPE_TRAITS )
193-
# include <type_traits>
194-
#elif boolean_HAVE( TR1_TYPE_TRAITS )
195-
# include <tr1/type_traits>
196-
#endif
197-
198-
#define boolean_CPP98_FALLBACK ( !boolean_CPP11_120 )
199-
200-
namespace nonstd {
201-
namespace boolean_lite {
110+
// Select between C++98 and C++11/later version of boolean_:
202111

203112
#if ! boolean_CPP98_FALLBACK
204113

205-
namespace std11 {
114+
// Additional includes:
206115

207-
#if boolean_HAVE( TYPE_TRAITS )
208-
using std::enable_if;
209-
using std::is_same;
210-
#elif boolean_HAVE( TR1_TYPE_TRAITS )
211-
// using std::tr1::enable_if;
212-
using std::tr1::is_same;
213-
#endif
214-
}
116+
#include <type_traits>
117+
118+
namespace nonstd {
119+
namespace boolean_lite {
215120

216121
//
217-
// class boolean_, C++11:
122+
// class boolean_, C++11 and later:
218123
//
219124

220125
class boolean_
@@ -226,26 +131,26 @@ class boolean_
226131
// [1] Converting constructor
227132

228133
template< typename T
229-
, typename std11::enable_if< std11::is_same<T,bool>::value, int >::type = 0 >
134+
, typename std::enable_if< std::is_same<T,bool>::value, int >::type = 0 >
230135
/*explicit*/ boolean_constexpr boolean_( T v ) boolean_noexcept
231136
: value( v ) {}
232137

233138
template< typename T
234-
, typename std11::enable_if< !std11::is_same<T,bool>::value, int >::type = 0 >
139+
, typename std::enable_if< !std::is_same<T,bool>::value, int >::type = 0 >
235140
explicit boolean_constexpr boolean_( T v ) boolean_noexcept
236141
: value( v ) {}
237142

238143
// [2] Conversion operator
239144

240145
template< typename T
241-
, typename std11::enable_if< std11::is_same<T,bool>::value, int >::type = 0 >
146+
, typename std::enable_if< std::is_same<T,bool>::value, int >::type = 0 >
242147
boolean_nodiscard /*explicit*/ boolean_constexpr operator T() const boolean_noexcept
243148
{
244149
return T( value );
245150
}
246151

247152
template< typename T
248-
, typename std11::enable_if< !std11::is_same<T,bool>::value, int >::type = 0 >
153+
, typename std::enable_if< !std::is_same<T,bool>::value, int >::type = 0 >
249154
boolean_nodiscard boolean_explicit_cv boolean_constexpr operator T() const boolean_noexcept
250155
{
251156
return T( value );
@@ -293,8 +198,13 @@ class boolean_
293198
}
294199
};
295200

201+
}} // namespace nonstd::boolean_lite
202+
296203
#else // boolean_CPP98_FALLBACK
297204

205+
namespace nonstd {
206+
namespace boolean_lite {
207+
298208
//
299209
// class boolean_, C++98:
300210
//
@@ -369,9 +279,15 @@ class boolean_
369279
}
370280
};
371281

282+
}} // namespace nonstd::boolean_lite
283+
372284
#endif // boolean_CPP98_FALLBACK
373285

374-
} // namespace boolean_lite
286+
//
287+
// Provide type boolean in namespace nonstd:
288+
//
289+
290+
namespace nonstd {
375291

376292
using boolean_lite::boolean_;
377293

test/boolean-main.t.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,53 @@
77

88
#include "boolean-main.t.hpp"
99

10+
// C++ language version (represent 98 as 3):
11+
12+
#define boolean_CPLUSPLUS_V ( boolean_CPLUSPLUS / 100 - (boolean_CPLUSPLUS > 200000 ? 2000 : 1994) )
13+
14+
// Compiler versions:
15+
16+
// MSVC++ 6.0 _MSC_VER == 1200 boolean_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
17+
// MSVC++ 7.0 _MSC_VER == 1300 boolean_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
18+
// MSVC++ 7.1 _MSC_VER == 1310 boolean_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
19+
// MSVC++ 8.0 _MSC_VER == 1400 boolean_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
20+
// MSVC++ 9.0 _MSC_VER == 1500 boolean_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
21+
// MSVC++ 10.0 _MSC_VER == 1600 boolean_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
22+
// MSVC++ 11.0 _MSC_VER == 1700 boolean_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
23+
// MSVC++ 12.0 _MSC_VER == 1800 boolean_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
24+
// MSVC++ 14.0 _MSC_VER == 1900 boolean_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
25+
// MSVC++ 14.1 _MSC_VER >= 1910 boolean_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
26+
// MSVC++ 14.2 _MSC_VER >= 1920 boolean_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
27+
28+
#if defined(_MSC_VER ) && !defined(__clang__)
29+
# define boolean_COMPILER_MSVC_VER (_MSC_VER )
30+
# define boolean_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
31+
# define boolean_COMPILER_MSVC_VERSION_FULL (_MSC_VER - 100 * ( 5 + (_MSC_VER < 1900 ) ) )
32+
#else
33+
# define boolean_COMPILER_MSVC_VER 0
34+
# define boolean_COMPILER_MSVC_VERSION 0
35+
# define boolean_COMPILER_MSVC_VERSION_FULL 0
36+
#endif
37+
38+
#define boolean_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
39+
40+
#if defined( __apple_build_version__ )
41+
# define boolean_COMPILER_APPLECLANG_VERSION boolean_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
42+
# define boolean_COMPILER_CLANG_VERSION 0
43+
#elif defined( __clang__ )
44+
# define boolean_COMPILER_APPLECLANG_VERSION 0
45+
# define boolean_COMPILER_CLANG_VERSION boolean_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
46+
#else
47+
# define boolean_COMPILER_APPLECLANG_VERSION 0
48+
# define boolean_COMPILER_CLANG_VERSION 0
49+
#endif
50+
51+
#if defined(__GNUC__) && !defined(__clang__)
52+
# define boolean_COMPILER_GNUC_VERSION boolean_COMPILER_VERSION( __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ )
53+
#else
54+
# define boolean_COMPILER_GNUC_VERSION 0
55+
#endif
56+
1057
#define boolean_PRESENT( x ) \
1158
std::cout << #x << ": " << x << "\n"
1259

@@ -32,6 +79,7 @@ CASE( "boolean-lite version" "[.boolean][.version]" )
3279
CASE( "boolean-lite configuration" "[.boolean][.config]" )
3380
{
3481
boolean_PRESENT( boolean_CPLUSPLUS );
82+
boolean_PRESENT( boolean_CPLUSPLUS_V );
3583
}
3684

3785
CASE( "__cplusplus" "[.stdc++]" )
@@ -47,14 +95,17 @@ CASE( "__cplusplus" "[.stdc++]" )
4795

4896
CASE( "Compiler version" "[.compiler]" )
4997
{
98+
boolean_PRESENT( boolean_COMPILER_APPLECLANG_VERSION );
5099
boolean_PRESENT( boolean_COMPILER_CLANG_VERSION );
51100
boolean_PRESENT( boolean_COMPILER_GNUC_VERSION );
52101
boolean_PRESENT( boolean_COMPILER_MSVC_VERSION );
102+
boolean_PRESENT( boolean_COMPILER_MSVC_VERSION_FULL );
53103
}
54104

55105
CASE( "presence of C++ language features" "[.stdlanguage]" )
56106
{
57107
boolean_PRESENT( boolean_HAVE_CONSTEXPR_11 );
108+
boolean_PRESENT( boolean_HAVE_DEFAULT_FN_TPL_ARGS );
58109
boolean_PRESENT( boolean_HAVE_EXPLICIT_CONVERSION );
59110
boolean_PRESENT( boolean_HAVE_NODISCARD );
60111
boolean_PRESENT( boolean_HAVE_NOEXCEPT );

0 commit comments

Comments
 (0)