1 //=================================================================================================
2 /*!
3 //  \file blaze/util/IntegralConstant.h
4 //  \brief Header file for the IntegralConstant class template
5 //
6 //  Copyright (C) 2012-2020 Klaus Iglberger - All Rights Reserved
7 //
8 //  This file is part of the Blaze library. You can redistribute it and/or modify it under
9 //  the terms of the New (Revised) BSD License. Redistribution and use in source and binary
10 //  forms, with or without modification, are permitted provided that the following conditions
11 //  are met:
12 //
13 //  1. Redistributions of source code must retain the above copyright notice, this list of
14 //     conditions and the following disclaimer.
15 //  2. Redistributions in binary form must reproduce the above copyright notice, this list
16 //     of conditions and the following disclaimer in the documentation and/or other materials
17 //     provided with the distribution.
18 //  3. Neither the names of the Blaze development group nor the names of its contributors
19 //     may be used to endorse or promote products derived from this software without specific
20 //     prior written permission.
21 //
22 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 //  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 //  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 //  SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 //  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 //  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 //  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 //  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 //  DAMAGE.
32 */
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_INTEGRALCONSTANT_H_
36 #define _BLAZE_UTIL_INTEGRALCONSTANT_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <type_traits>
44 #include <blaze/util/Types.h>
45 
46 
47 namespace blaze {
48 
49 //=================================================================================================
50 //
51 //  CLASS DEFINITION
52 //
53 //=================================================================================================
54 
55 //*************************************************************************************************
56 /*!\brief Generic wrapper for a compile time constant integral value.
57 // \ingroup util
58 //
59 // The IntegralConstant class template represents a generic wrapper for a compile time constant
60 // integral value. The value of an IntegralConstant can be accessed via the nested \a value (which
61 // is guaranteed to be of type \a T), the type can be accessed via the nested type definition
62 // \a ValueType.
63 
64    \code
65    using namespace blaze;
66 
67    IntegralConstant<int,3>::value        // Evaluates to 3
68    IntegralConstant<long,5L>::ValueType  // Results in long
69    \endcode
70 */
71 template< typename T, T N >
72 struct IntegralConstant
73    : public std::integral_constant<T,N>
74 {
75    //**********************************************************************************************
76    /*! \cond BLAZE_INTERNAL */
77    using ValueType = T;
78    using Type = IntegralConstant<T,N>;
79    /*! \endcond */
80    //**********************************************************************************************
81 };
82 //*************************************************************************************************
83 
84 
85 
86 
87 //=================================================================================================
88 //
89 //  ALIAS DECLARATIONS
90 //
91 //=================================================================================================
92 
93 //*************************************************************************************************
94 /*!\brief Generic wrapper for a compile time constant boolean value.
95 // \ingroup util
96 //
97 // The BoolConstant alias template represents a generic wrapper for a compile time constant
98 // boolean value. The value of a BoolConstant can be accessed via the nested \a value (which
99 // is guaranteed to be of type \c bool), the type can be accessed via the nested type definition
100 // \a ValueType.
101 
102    \code
103    using namespace blaze;
104 
105    BoolConstant<true>::value       // Evaluates to true
106    BoolConstant<false>::ValueType  // Results in bool
107    \endcode
108 */
109 template< bool B >
110 using BoolConstant = IntegralConstant<bool,B>;
111 //*************************************************************************************************
112 
113 
114 //*************************************************************************************************
115 /*!\brief Type/value traits base class.
116 // \ingroup util
117 //
118 // The FalseType class is used as base class for type traits and value traits that evaluate to
119 // \a false.
120 */
121 using FalseType = BoolConstant<false>;
122 //*************************************************************************************************
123 
124 
125 //*************************************************************************************************
126 /*!\brief Type traits base class.
127 // \ingroup util
128 //
129 // The TrueType class is used as base class for type traits and value traits that evaluate to
130 // \a true.
131 */
132 using TrueType = BoolConstant<true>;
133 //*************************************************************************************************
134 
135 
136 //*************************************************************************************************
137 /*!\brief Compile time integral constant wrapper for \a bool.
138 // \ingroup util
139 //
140 // The Bool_t alias template represents an integral wrapper for a compile time constant
141 // expression of type \a bool. The value of a Bool_t can be accessed via the nested \a value
142 // (which is guaranteed to be of type \a bool), the type can be accessed via the nested type
143 // definition \a ValueType.
144 
145    \code
146    using namespace blaze;
147 
148    Bool_t<true>::value       // Evaluates to true
149    Bool_t<false>::ValueType  // Results in bool
150    \endcode
151 */
152 template< bool B >
153 using Bool_t = IntegralConstant<bool,B>;
154 //*************************************************************************************************
155 
156 
157 //*************************************************************************************************
158 /*!\brief Compile time integral constant wrapper for \a char.
159 // \ingroup util
160 //
161 // The Char_t alias template represents an integral wrapper for a compile time constant
162 // expression of type \a char. The value of an Char_t can be accessed via the nested \a value
163 // (which is guaranteed to be of type \a char), the type can be accessed via the nested type
164 // definition \a ValueType.
165 
166    \code
167    using namespace blaze;
168 
169    Char_t<3>::value      // Evaluates to 3
170    Char_t<5>::ValueType  // Results in char
171    \endcode
172 */
173 template< char N >
174 using Char_t = IntegralConstant<char,N>;
175 //*************************************************************************************************
176 
177 
178 //*************************************************************************************************
179 /*!\brief Compile time integral constant wrapper for \a int.
180 // \ingroup util
181 //
182 // The Int_t alias template represents an integral wrapper for a compile time constant
183 // expression of type \a int. The value of an Int_t can be accessed via the nested \a value
184 // (which is guaranteed to be of type \a int), the type can be accessed via the nested type
185 // definition \a ValueType.
186 
187    \code
188    using namespace blaze;
189 
190    Int_t<3>::value      // Evaluates to 3
191    Int_t<5>::ValueType  // Results in int
192    \endcode
193 */
194 template< int N >
195 using Int_t = IntegralConstant<int,N>;
196 //*************************************************************************************************
197 
198 
199 //*************************************************************************************************
200 /*!\brief Compile time integral constant wrapper for \a long.
201 // \ingroup util
202 //
203 // The Long_t alias template represents an integral wrapper for a compile time constant
204 // expression of type \a long. The value of an Long_t can be accessed via the nested \a value
205 // (which is guaranteed to be of type \a long), the type can be accessed via the nested type
206 // definition \a ValueType.
207 
208    \code
209    using namespace blaze;
210 
211    Long_t<3>::value      // Evaluates to 3
212    Long_t<5>::ValueType  // Results in long
213    \endcode
214 */
215 template< long N >
216 using Long_t = IntegralConstant<long,N>;
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
221 /*!\brief Compile time integral constant wrapper for \a ptrdiff_t.
222 // \ingroup util
223 //
224 // The Ptrdiff_t alias template represents an integral wrapper for a compile time constant
225 // expression of type \a ptrdiff_t. The value of an Ptrdiff_t can be accessed via the nested
226 // \a value (which is guaranteed to be of type \a ptrdiff_t), the type can be accessed via
227 // the nested type definition \a ValueType.
228 
229    \code
230    using namespace blaze;
231 
232    Ptrdiff_t<3>::value      // Evaluates to 3
233    Ptrdiff_t<5>::ValueType  // Results in ptrdiff_t
234    \endcode
235 */
236 template< ptrdiff_t N >
237 using Ptrdiff_t = IntegralConstant<ptrdiff_t,N>;
238 //*************************************************************************************************
239 
240 
241 //*************************************************************************************************
242 /*!\brief Compile time integral constant wrapper for \a size_t.
243 // \ingroup util
244 //
245 // The Size_t alias template represents an integral wrapper for a compile time constant expression
246 // of type \a size_t. The value of an Size_t can be accessed via the nested \a value (which is
247 // guaranteed to be of type \a size_t), the type can be accessed via the nested type definition
248 // \a ValueType.
249 
250    \code
251    using namespace blaze;
252 
253    Size_t<3>::value      // Evaluates to 3
254    Size_t<5>::ValueType  // Results in size_t
255    \endcode
256 */
257 template< size_t N >
258 using Size_t = IntegralConstant<size_t,N>;
259 //*************************************************************************************************
260 
261 
262 
263 
264 //=================================================================================================
265 //
266 //  GLOBAL FUNCTIONS
267 //
268 //=================================================================================================
269 
270 //*************************************************************************************************
271 /*!\brief Logical NOT of a boolean constant.
272 // \ingroup util
273 //
274 // \return \a TrueType if the given argument is \a FalseType, \a FalseType otherwise.
275 //
276 // This function performs a logical NOT on the given boolean constant. In case the argument is
277 // \a FalseType, the function returns \a TrueType, else it returns \a FalseType.
278 */
279 template< bool B >
280 constexpr BoolConstant<!B> operator!( BoolConstant<B> ) noexcept
281 {
282    return BoolConstant<!B>();
283 }
284 //*************************************************************************************************
285 
286 
287 //*************************************************************************************************
288 /*!\brief Logical AND of two boolean constants.
289 // \ingroup util
290 //
291 // \return \a TrueType if both arguments are \a TrueType, \a FalseType otherwise.
292 //
293 // This function performs a logical AND between the two given boolean constants. In case both
294 // arguments are \a TrueType, the function returns \a TrueType, else it returns \a FalseType.
295 */
296 template< bool B1, bool B2 >
297 constexpr BoolConstant< B1 && B2 > operator&&( BoolConstant<B1>, BoolConstant<B2> ) noexcept
298 {
299    return BoolConstant< B1 && B2 >();
300 }
301 //*************************************************************************************************
302 
303 
304 //*************************************************************************************************
305 /*!\brief Logical OR of two boolean constants.
306 // \ingroup util
307 //
308 // \return \a TrueType if any of the arguments is \a TrueType, \a FalseType otherwise.
309 //
310 // This function performs a logical OR between the two given boolean constants. In case any of the
311 // two arguments is \a TrueType, the function returns \a TrueType, else it returns \a FalseType.
312 */
313 template< bool B1, bool B2 >
314 constexpr BoolConstant< B1 || B2 > operator||( BoolConstant<B1>, BoolConstant<B2> ) noexcept
315 {
316    return BoolConstant< B1 || B2 >();
317 }
318 //*************************************************************************************************
319 
320 } // namespace blaze
321 
322 #endif
323