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// 1MMmmrr
41#define KBLIB_VERS 1000203
42
43#define KBLIB_VERS_MAJ 0
44#define KBLIB_VERS_MIN 2
45#define KBLIB_VERS_REV 3
46// MM_mm_rr
47#define KBLIB_VERS_S KBLIB_VERS_MAJ##_##KBLIB_VERS_MIN##_##KBLIB_VERS_REV
48// api_vMM_mm_rr
49#define KBLIB_VERS_NS api_v##KBLIB_VERS_S
50
56#define KBLIB_USE_CXX17 __cplusplus >= 201703L
57
63#define KBLIB_USE_CXX20 __cplusplus >= 202002L
69#ifndef KBLIB_USE_STRING_VIEW
70# define KBLIB_USE_STRING_VIEW __cpp_lib_string_view
71#endif
72
73// Note that __has_cpp_attribute(nodiscard) does not work with at least certain
74// versions of Clang
80#if KBLIB_USE_CXX17
81# define KBLIB_NODISCARD [[nodiscard]]
82#else
83# define KBLIB_NODISCARD [[gnu::warn_unused_result]]
84#endif
85
91#if KBLIB_USE_CXX17
92# define KBLIB_UNUSED [[maybe_unused]]
93#else
94# define KBLIB_UNUSED [[gnu::unused]]
95#endif
96
97#if KBLIB_USE_CXX17
98# define KBLIB_CONSTANT constexpr
99# define KBLIB_CONSTANT_V constexpr bool
100# define KBLIB_CONSTANT_M constexpr inline static
101# define KBLIB_CONSTANT_MV constexpr inline static bool
102#else
103# define KBLIB_CONSTANT constexpr static
104# define KBLIB_CONSTANT_V constexpr static bool
105# define KBLIB_CONSTANT_M constexpr static
106# define KBLIB_CONSTANT_MV constexpr static bool
107#endif
108
113#if KBLIB_USE_CXX20
114# define KBLIB_CXX20(args) args
115#else
116# define KBLIB_CXX20(args)
117#endif
118
119#if defined(_DOXYGEN_) and not defined(KBLIB_DEF_MACROS)
125# define KBLIB_DEF_MACROS
126#endif
127
132namespace kblib {
133
140namespace detail {
141
142 template <typename Container, bool, typename...>
144
145 template <typename T>
146 struct tag {
147 using type = T;
148 };
149
150 template <typename T>
151 struct no_dangle {
152 using type = T&;
153 };
154
155 template <typename T>
157 using type = T;
158 };
159
160 template <typename T>
162
163} // namespace detail
164enum class endian { unknown, little, big, weird };
165
166#ifdef __BYTE_ORDER__
167namespace detail {
168 KBLIB_NODISCARD constexpr auto get_system_endian() -> endian {
169 if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
170 return endian::big;
171 } else if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) {
172 return endian::little;
173 } else {
174 return endian::weird;
175 }
176 }
177} // namespace detail
178
179constexpr endian system_endian = detail::get_system_endian();
180#else
182#endif
183
184namespace detail {
187 return system_endian;
188 } else {
189 return endian::little;
190 }
191 }
192} // namespace detail
193
194#ifdef KBLIB_CONSISTENT_HASHES
195constexpr endian hash_order = little;
196#else
198#endif
199
200#if KBLIB_USE_CXX17
201using std::byte;
202#else
203using byte = unsigned char;
204#endif
205
206} // namespace kblib
207
208#endif // KBLIB_TDECL_H
constexpr auto get_hash_order() -> endian
Definition: tdecl.h:185
typename no_dangle< T >::type no_dangle_t
Definition: tdecl.h:161
The main namespace in which all entities from kblib are defined.
Definition: algorithm.h:44
constexpr endian system_endian
Definition: tdecl.h:181
endian
Definition: tdecl.h:164
constexpr endian hash_order
Definition: tdecl.h:197
#define KBLIB_NODISCARD
This internal macro is used to provide a fallback for [[nodiscard]] in C++14.
Definition: tdecl.h:81