kblib 0.2.3
General utilities library for modern C++
tdecl.h
Go to the documentation of this file.
1/* *****************************************************************************
2 * kblib is a general utility library for C++14 and C++17, intended to provide
3 * performant high-level abstractions and more expressive ways to do simple
4 * things.
5 *
6 * Copyright (c) 2021 killerbee
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * ****************************************************************************/
21
31#ifndef KBLIB_TDECL_H
32#define KBLIB_TDECL_H
33
34#include <cstddef>
35
36#if __cplusplus < 201402L
37# error kblib requires C++14 or higher
38#endif
39
40#define KBLIB_X(X) X
41
42#define KBLIB_VERS_MAJ 00
43#define KBLIB_VERS_MIN 04
44#define KBLIB_VERS_REV 00
45#define KBLIB_VERS_I(P, MAJ, MIN, REV) KBLIB_VERS_I2(P, MAJ, MIN, REV)
46#define KBLIB_VERS_I2(P, MAJ, MIN, REV) P##MAJ##MIN##REV
47
48// VMMmmrr
49#define KBLIB_VERS_S \
50 KBLIB_VERS_I(KBV, KBLIB_VERS_MAJ, KBLIB_VERS_MIN, KBLIB_VERS_REV)
51// 1MMmmrr
52#define KBLIB_VERS \
53 KBLIB_VERS_I(1, KBLIB_VERS_MAJ, KBLIB_VERS_MIN, KBLIB_VERS_REV)
54
60#if (__cplusplus >= 201703L)
61# define KBLIB_USE_CXX17 1
62#else
63# define KBLIB_USE_CXX17 0
64#endif
65
71#if (__cplusplus >= 202002L)
72# define KBLIB_USE_CXX20 1
73#else
74# define KBLIB_USE_CXX20 0
75#endif
81#ifndef KBLIB_USE_STRING_VIEW
82# if KBLIB_USE_CXX17
83# define KBLIB_USE_STRING_VIEW 1
84# else
85# define KBLIB_USE_STRING_VIEW 0
86# endif
87#endif
88
93#if KBLIB_USE_CXX20
94# define KBLIB_CXX20(args) args
95#else
96# define KBLIB_CXX20(args)
97#endif
98
99// used to prevent cross-linkage between incompatible library versions
100#define KBLIB_VERS_NS_I(VS, CXX17, CXX_SV, CXX20) \
101 KBLIB_VERS_NS_I2(VS, CXX17, CXX_SV, CXX20)
102#define KBLIB_VERS_NS_I2(VS, CXX17, CXX_SV, CXX20) VS##_##CXX17##CXX_SV##CXX20
103
104#define KBLIB_VERS_NS \
105 KBLIB_VERS_NS_I(KBLIB_VERS_S, KBLIB_USE_CXX17, KBLIB_USE_STRING_VIEW, \
106 KBLIB_USE_CXX20)
107
108#ifndef _DOXYGEN_
109# define KBLIB_NS KBLIB_VERS_NS
110namespace KBLIB_NS {}
111namespace kblib = KBLIB_NS;
112#else
113# define KBLIB_NS kblib
114#endif
115
116// Note that __has_cpp_attribute(nodiscard) does not work with at least certain
117// versions of Clang
128#if KBLIB_USE_CXX17
129# define KBLIB_NODISCARD [[nodiscard]]
130# define KBLIB_UNUSED [[maybe_unused]]
131#else
132# define KBLIB_NODISCARD [[gnu::warn_unused_result]]
133# define KBLIB_UNUSED [[gnu::unused]]
134#endif
135
136#if KBLIB_USE_CXX17
137# define KBLIB_CONSTANT constexpr inline
138# define KBLIB_CONSTANT_V constexpr inline bool
139# define KBLIB_CONSTANT_M constexpr inline static
140# define KBLIB_CONSTANT_MV constexpr inline static bool
141#else
142# define KBLIB_CONSTANT constexpr
143# define KBLIB_CONSTANT_V constexpr bool
144# define KBLIB_CONSTANT_M constexpr static
145# define KBLIB_CONSTANT_MV constexpr static bool
146#endif
147
148#if defined(_DOXYGEN_) and not defined(KBLIB_DEF_MACROS)
154# define KBLIB_DEF_MACROS
155#endif
156
162namespace KBLIB_NS {
163
170namespace detail {
171
172 template <typename Container, bool, typename...>
174
175 template <typename T>
176 struct tag {
177 using type = T;
178 };
179
180 template <typename T>
181 struct no_dangle {
182 using type = T&;
183 };
184
185 template <typename T>
187 using type = T;
188 };
189
190 template <typename T>
192
193} // namespace detail
194enum class endian { unknown, little, big, weird };
195
196#ifdef __BYTE_ORDER__
197namespace detail {
198 KBLIB_NODISCARD constexpr auto get_system_endian() -> endian {
199 if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
200 return endian::big;
201 } else if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) {
202 return endian::little;
203 } else {
204 return endian::weird;
205 }
206 }
207} // namespace detail
208
209constexpr endian system_endian = detail::get_system_endian();
210#else
211constexpr endian system_endian = endian::unknown;
212#endif
213
214namespace detail {
216 if (system_endian == endian::little or system_endian == endian::big) {
217 return system_endian;
218 } else {
219 return endian::little;
220 }
221 }
222} // namespace detail
223
224#ifdef KBLIB_CONSISTENT_HASHES
225constexpr endian hash_order = little;
226#else
228#endif
229
230#if KBLIB_USE_CXX17
231using std::byte;
232#else
233using byte = unsigned char;
234#endif
235
236} // namespace KBLIB_NS
237
238#endif // KBLIB_TDECL_H
constexpr auto get_hash_order() -> endian
Definition: tdecl.h:215
typename no_dangle< T >::type no_dangle_t
Definition: tdecl.h:191
The main namespace in which all entities from kblib are defined.
Definition: algorithm.h:44
constexpr endian system_endian
Definition: tdecl.h:211
endian
Definition: tdecl.h:194
constexpr endian hash_order
Definition: tdecl.h:227
#define KBLIB_NS
Definition: tdecl.h:113
#define KBLIB_NODISCARD
This internal macro is used to provide a fallback for [[nodiscard]] in C++14.
Definition: tdecl.h:129