Skip to content

Commit 84e7375

Browse files
committed
types.hpp -> type_traits.hpp
1 parent ab3192f commit 84e7375

File tree

6 files changed

+251
-218
lines changed

6 files changed

+251
-218
lines changed

src/utki/base64.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SOFTWARE.
3232
#include <stdexcept>
3333

3434
#include "debug.hpp"
35-
#include "types.hpp"
35+
#include "type_traits.hpp"
3636

3737
using namespace utki;
3838

src/utki/linq.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ SOFTWARE.
3939
# include <algorithm>
4040

4141
# include "debug.hpp"
42-
# include "types.hpp"
42+
# include "type_traits.hpp"
4343
# include "config.hpp"
4444

4545
namespace utki {

src/utki/type_traits.hpp

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
utki - Utility Kit for C++.
5+
6+
Copyright (c) 2015-2025 Ivan Gagis <[email protected]>
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
/* ================ LICENSE END ================ */
28+
29+
#pragma once
30+
31+
#include <cstddef>
32+
#include <cstdint>
33+
#include <limits>
34+
#include <memory>
35+
#include <variant>
36+
37+
#include "config.hpp"
38+
39+
namespace utki {
40+
41+
[[deprecated("use utki::byte_bits")]] constexpr auto num_bits_in_byte = 8;
42+
constexpr auto byte_bits = 8;
43+
static_assert(std::numeric_limits<uint8_t>::digits == byte_bits, "system with non-8-bit bytes is not supported");
44+
[[deprecated("use utki::nibble_bits")]] constexpr auto num_bits_in_nibble = byte_bits / 2;
45+
constexpr auto nibble_bits = byte_bits / 2;
46+
47+
constexpr auto lower_nibble_mask = 0xf;
48+
constexpr auto upper_nibble_mask = 0xf0;
49+
constexpr auto byte_mask = 0xff;
50+
constexpr auto bit_0_mask = 0x01;
51+
constexpr auto bit_1_mask = 0x02;
52+
constexpr auto bit_2_mask = 0x04;
53+
constexpr auto bit_3_mask = 0x08;
54+
constexpr auto bit_4_mask = 0x10;
55+
constexpr auto bit_5_mask = 0x20;
56+
constexpr auto bit_6_mask = 0x40;
57+
constexpr auto bit_7_mask = 0x80;
58+
59+
/**
60+
* @brief Smallest unsigned integer type to hold specified number of bytes.
61+
* @tparam type_size - number of bytes to store.
62+
*/
63+
template <size_t type_size>
64+
struct uint_size;
65+
66+
template <>
67+
struct uint_size<1> {
68+
using type = uint8_t;
69+
};
70+
71+
template <>
72+
struct uint_size<2> {
73+
using type = uint16_t;
74+
};
75+
76+
template <>
77+
struct uint_size<3> {
78+
using type = uint32_t;
79+
};
80+
81+
template <>
82+
struct uint_size<4> {
83+
using type = uint32_t;
84+
};
85+
86+
template <>
87+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
88+
struct uint_size<5> {
89+
using type = uint64_t;
90+
};
91+
92+
template <>
93+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
94+
struct uint_size<6> {
95+
using type = uint64_t;
96+
};
97+
98+
template <>
99+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
100+
struct uint_size<7> {
101+
using type = uint64_t;
102+
};
103+
104+
template <>
105+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
106+
struct uint_size<8> {
107+
using type = uint64_t;
108+
};
109+
110+
/**
111+
* @brief Smallest signed integer type to hold specified number of bytes.
112+
* @tparam type_size - number of bytes to store.
113+
*/
114+
template <size_t type_size>
115+
struct int_size;
116+
117+
template <>
118+
struct int_size<1> {
119+
using type = int8_t;
120+
};
121+
122+
template <>
123+
struct int_size<2> {
124+
using type = int16_t;
125+
};
126+
127+
template <>
128+
struct int_size<3> {
129+
using type = int32_t;
130+
};
131+
132+
template <>
133+
struct int_size<4> {
134+
using type = int32_t;
135+
};
136+
137+
template <>
138+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
139+
struct int_size<5> {
140+
using type = int64_t;
141+
};
142+
143+
template <>
144+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
145+
struct int_size<6> {
146+
using type = int64_t;
147+
};
148+
149+
template <>
150+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
151+
struct int_size<7> {
152+
using type = int64_t;
153+
};
154+
155+
template <>
156+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
157+
struct int_size<8> {
158+
using type = int64_t;
159+
};
160+
161+
template <typename in_type>
162+
struct remove_const_pointer {
163+
using type = typename std::remove_const_t<typename std::remove_pointer_t<in_type>>;
164+
};
165+
166+
template <typename in_type>
167+
using remove_const_pointer_t = typename remove_const_pointer<in_type>::type;
168+
169+
// test remove_const_pointer_t
170+
static_assert(std::is_same_v<remove_const_pointer_t<const char*>, char>, "remove_const_pointer_t test failed");
171+
172+
template <typename in_type>
173+
struct remove_const_reference {
174+
using type = typename std::remove_const_t<typename std::remove_reference_t<in_type>>;
175+
};
176+
177+
template <typename in_type>
178+
using remove_const_reference_t = typename remove_const_reference<in_type>::type;
179+
180+
// test remove_const_reference_t
181+
static_assert(std::is_same_v<remove_const_reference_t<const char&>, char>, "remove_const_reference_t test failed");
182+
183+
/**
184+
* @brief Dummy class.
185+
* This class is supposed to be used as template parameter when there
186+
* is a desire to avoid function overload matching.
187+
* For example, see utki::span class implementation.
188+
*/
189+
class dummy_class
190+
{};
191+
192+
/**
193+
* @brief Check if owner_type::type is defined.
194+
* Defines bool 'value' which is true if owner_type::type is defined and false otherwise.
195+
* @tparam owner_type - type to check.
196+
*/
197+
template <class owner_type, class = void>
198+
struct is_type_defined : std::false_type {};
199+
200+
// clang-format off
201+
202+
template <class owner_type>
203+
struct is_type_defined<owner_type, std::void_t<typename owner_type::type>> : std::true_type{};
204+
205+
// clang-format on
206+
207+
/**
208+
* @brief Get type or void.
209+
* @tparam owner_type - type to get owner_type::type from.
210+
* Defines 'type' member which is same as owner_type::type in case owner_type::type is defined, or void type otherwise.
211+
*/
212+
template <class owner_type, class = void>
213+
struct type_or_void {
214+
using type = void;
215+
};
216+
217+
template <class owner_type>
218+
struct type_or_void<owner_type, std::void_t<typename owner_type::type>> {
219+
using type = typename owner_type::type;
220+
};
221+
222+
/**
223+
* @brief Type alias for type_or_void.
224+
*/
225+
template <typename owner_type>
226+
using type_or_void_t = typename type_or_void<owner_type>::type;
227+
228+
/**
229+
* @brief Check if given type is a specialization of given template.
230+
* @tparam template_templ - template to check for specialization of.
231+
* @tparam checked_type - type to check for specialization of given template.
232+
*/
233+
template <template <typename...> class template_templ, typename checked_type>
234+
struct is_specialization_of : std::false_type {};
235+
236+
// clang-format off
237+
template <template <typename...> class template_templ, typename... args_type>
238+
struct is_specialization_of<template_templ, template_templ<args_type...>> : std::true_type{};
239+
240+
// clang-format on
241+
242+
template <template <typename...> class template_templ, typename checked_type>
243+
constexpr static bool is_specialization_of_v = is_specialization_of<template_templ, checked_type>::value;
244+
245+
} // namespace utki

0 commit comments

Comments
 (0)