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#if __has_include(<version>)
36# include <version>
37#endif
38
39#if __cplusplus < 201402L
40# error kblib requires C++14 or higher
41#endif
42
43#define KBLIB_X(X) X
44
45#define KBLIB_VERS_MAJ 00
46#define KBLIB_VERS_MIN 04
47#define KBLIB_VERS_REV 02
48#define KBLIB_VERS_I(P, MAJ, MIN, REV) KBLIB_VERS_I2(P, MAJ, MIN, REV)
49#define KBLIB_VERS_I2(P, MAJ, MIN, REV) P##MAJ##MIN##REV
50
51// VMMmmrr
52#define KBLIB_VERS_S \
53 KBLIB_VERS_I(KBV, KBLIB_VERS_MAJ, KBLIB_VERS_MIN, KBLIB_VERS_REV)
54// 1MMmmrr
55#define KBLIB_VERS \
56 KBLIB_VERS_I(1, KBLIB_VERS_MAJ, KBLIB_VERS_MIN, KBLIB_VERS_REV)
57
63#if (__cplusplus >= 201703L)
64# define KBLIB_USE_CXX17 1
65#else
66# define KBLIB_USE_CXX17 0
67#endif
68
74#if (__cplusplus >= 202002L)
75# define KBLIB_USE_CXX20 1
76#else
77# define KBLIB_USE_CXX20 0
78#endif
84#ifndef KBLIB_USE_STRING_VIEW
85# if KBLIB_USE_CXX17
86# define KBLIB_USE_STRING_VIEW 1
87# else
88# define KBLIB_USE_STRING_VIEW 0
89# endif
90#endif
91
97#ifndef KBLIB_USE_SPANSTREAM
98# if __cpp_lib_spanstream
99# define KBLIB_USE_SPANSTREAM 1
100# else
101# define KBLIB_USE_SPANSTREAM 0
102# endif
103#endif
104
109#if KBLIB_USE_CXX20
110# define KBLIB_CXX20(args) args
111#else
112# define KBLIB_CXX20(args)
113#endif
114
115// used to prevent cross-linkage between incompatible library versions
116#define KBLIB_VERS_NS_I(VS, CXX17, CXX_SV, CXX_SS, CXX20) \
117 KBLIB_VERS_NS_I2(VS, CXX17, CXX_SV, CXX_SS, CXX20)
118#define KBLIB_VERS_NS_I2(VS, CXX17, CXX_SV, CXX_SS, CXX20) \
119 VS##_##CXX17##CXX_SV##CXX_SS##CXX20
120
121#define KBLIB_VERS_NS \
122 KBLIB_VERS_NS_I(KBLIB_VERS_S, KBLIB_USE_CXX17, KBLIB_USE_STRING_VIEW, \
123 KBLIB_USE_SPANSTREAM, KBLIB_USE_CXX20)
124
125#ifndef _DOXYGEN_
126# define KBLIB_NS KBLIB_VERS_NS
127namespace KBLIB_NS {}
128namespace kblib = KBLIB_NS;
129#else
130# define KBLIB_NS kblib
131#endif
132
133// Note that __has_cpp_attribute(nodiscard) does not work with at least certain
134// versions of Clang
145#if KBLIB_USE_CXX17
146# define KBLIB_NODISCARD [[nodiscard]]
147# define KBLIB_UNUSED [[maybe_unused]]
148#else
149# define KBLIB_NODISCARD [[gnu::warn_unused_result]]
150# define KBLIB_UNUSED [[gnu::unused]]
151#endif
152
153#if KBLIB_USE_CXX17
154# define KBLIB_CONSTANT constexpr inline
155# define KBLIB_CONSTANT_V constexpr inline bool
156# define KBLIB_CONSTANT_M constexpr inline static
157# define KBLIB_CONSTANT_MV constexpr inline static bool
158#else
159# define KBLIB_CONSTANT constexpr
160# define KBLIB_CONSTANT_V constexpr bool
161# define KBLIB_CONSTANT_M constexpr static
162# define KBLIB_CONSTANT_MV constexpr static bool
163#endif
164
165#if defined(_DOXYGEN_) and not defined(KBLIB_DEF_MACROS)
171# define KBLIB_DEF_MACROS
172#endif
173
179namespace KBLIB_NS {
180
187namespace detail {
188
189 template <typename Container, bool, typename...>
191
192 template <typename T>
193 struct tag {
194 using type = T;
195 };
196
197 template <typename T>
198 struct no_dangle {
199 using type = T&;
200 };
201
202 template <typename T>
204 using type = T;
205 };
206
207 template <typename T>
209
210} // namespace detail
211enum class endian { unknown, little, big, weird };
212
213#ifdef __BYTE_ORDER__
214namespace detail {
215 KBLIB_NODISCARD constexpr auto get_system_endian() -> endian {
216 if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
217 return endian::big;
218 } else if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) {
219 return endian::little;
220 } else {
221 return endian::weird;
222 }
223 }
224} // namespace detail
225
226constexpr endian system_endian = detail::get_system_endian();
227#else
228constexpr endian system_endian = endian::unknown;
229#endif
230
231namespace detail {
233 if (system_endian == endian::little or system_endian == endian::big) {
234 return system_endian;
235 } else {
236 return endian::little;
237 }
238 }
239} // namespace detail
240
241#ifdef KBLIB_CONSISTENT_HASHES
242constexpr endian hash_order = little;
243#else
245#endif
246
247#if KBLIB_USE_CXX17
248using std::byte;
249#else
250using byte = unsigned char;
251#endif
252
253} // namespace KBLIB_NS
254
255#endif // KBLIB_TDECL_H
constexpr auto get_hash_order() -> endian
Definition: tdecl.h:232
typename no_dangle< T >::type no_dangle_t
Definition: tdecl.h:208
The main namespace in which all entities from kblib are defined.
Definition: algorithm.h:44
constexpr endian system_endian
Definition: tdecl.h:228
endian
Definition: tdecl.h:211
constexpr endian hash_order
Definition: tdecl.h:244
#define KBLIB_NS
Definition: tdecl.h:130
#define KBLIB_NODISCARD
This internal macro is used to provide a fallback for [[nodiscard]] in C++14.
Definition: tdecl.h:146