staticlib_support  1.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
is_integer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * File: int_utils.hpp
19  * Author: alex
20  *
21  * Created on June 13, 2016, 10:39 PM
22  */
23 
24 #ifndef STATICLIB_SUPPORT_IS_INTEGER_HPP
25 #define STATICLIB_SUPPORT_IS_INTEGER_HPP
26 
27 #include <cstdint>
28 #include <ios>
29 #include <limits>
30 #include <type_traits>
31 
32 namespace staticlib {
33 namespace support {
34 
41 template <typename T>
42 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_int32(T val) {
43  return val <= static_cast<uint32_t>(std::numeric_limits<int32_t>::max());
44 }
45 
52 template <typename T>
53 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_int32(T val) {
54  return val >= std::numeric_limits<int32_t>::min() && val <= std::numeric_limits<int32_t>::max();
55 }
56 
63 template <typename T>
64 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_uint32(T val) {
65  return val <= std::numeric_limits<uint32_t>::max();
66 }
67 
74 template <typename T>
75 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_uint32(T val) {
76  return val >= 0 && val <= static_cast<int64_t> (std::numeric_limits<uint32_t>::max());
77 }
78 
85 template <typename T>
86 bool is_uint32_positive(T val) {
87  return 0 != val && is_uint32(val);
88 }
89 
96 template <typename T>
97 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_int16(T val) {
98  return val <= static_cast<uint16_t> (std::numeric_limits<int16_t>::max());
99 }
100 
107 template <typename T>
108 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_int16(T val) {
109  return val >= std::numeric_limits<int16_t>::min() && val <= std::numeric_limits<int16_t>::max();
110 }
111 
118 template <typename T>
119 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_uint16(T val) {
120  return val <= std::numeric_limits<uint16_t>::max();
121 }
122 
129 template <typename T>
130 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_uint16(T val) {
131  return val >= 0 && val <= static_cast<int32_t> (std::numeric_limits<uint16_t>::max());
132 }
133 
140 template <typename T>
141 bool is_uint16_positive(T val) {
142  return 0 != val && is_uint16(val);
143 }
144 
151 template <typename T>
152 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_int64(T val) {
153  return val <= static_cast<uint64_t> (std::numeric_limits<int64_t>::max());
154 }
155 
162 template <typename T>
163 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_int64(T val) {
164  return val >= std::numeric_limits<int64_t>::min() && val <= std::numeric_limits<int64_t>::max();
165 }
166 
173 template <typename T>
174 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_uint64(T val) {
175  return val <= std::numeric_limits<uint64_t>::max();
176 }
177 
184 template <typename T>
185 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_uint64(T val) {
186  return val >= 0 && val <= std::numeric_limits<int64_t>::max();
187 }
188 
195 template <typename T>
196 bool is_uint64_positive(T val) {
197  return 0 != val && is_uint64(val);
198 }
199 
206 template <typename T>
207 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_streamsize(T val) {
208  return val <= static_cast<std::size_t> (std::numeric_limits<std::streamsize>::max());
209 }
210 
217 template <typename T>
218 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_streamsize(T val) {
219  return val >= std::numeric_limits<std::streamsize>::min() && val <= std::numeric_limits<std::streamsize>::max();
220 }
221 
228 template <typename T>
229 typename std::enable_if<std::is_unsigned<T>::value, bool>::type is_sizet(T val) {
230  return val <= std::numeric_limits<size_t>::max();
231 }
232 
239 template <typename T>
240 typename std::enable_if<!std::is_unsigned<T>::value, bool>::type is_sizet(T val) {
241  if (val < 0) {
242  return false;
243  }
244  return static_cast<uint64_t>(val) <= std::numeric_limits<size_t>::max();
245 }
246 
253 template <typename T>
254 bool is_sizet_positive(T val) {
255  return 0 != val && is_sizet(val);
256 }
257 
258 } // namespace
259 }
260 
261 #endif /* STATICLIB_SUPPORT_IS_INTEGER_HPP */