Debian Codegraph @

src/mpt/base/size.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_SIZE_HPP
4 #define MPT_BASE_SIZE_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9
10 #if MPT_CXX_AT_LEAST(20)
11 #include "mpt/base/constexpr_throw.hpp"
12 #endif // C++20
13 #include "mpt/base/namespace.hpp"
14 #if MPT_CXX_AT_LEAST(20)
15 #include "mpt/base/utility.hpp"
16 #endif // C++20
17
18 #if MPT_CXX_AT_LEAST(20)
19 #include <stdexcept>
20 #endif // C++20
21
22 #include <cstddef>
23
24
25
26 namespace mpt {
27 inline namespace MPT_INLINE_NS {
28
29
30
31 using usize = std::size_t;
32 using ssize = std::ptrdiff_t;
33
34
35 namespace size_literals {
36 #if MPT_CXX_AT_LEAST(20)
37 consteval usize operator""_uz(unsigned long long val) noexcept {
38 if (!mpt::in_range<usize>(val)) {
39 mpt::constexpr_throw(std::domain_error(""));
40 }
41 return static_cast<usize>(val);
42 }
43 #else // !C++20
44 constexpr usize operator""_uz(unsigned long long val) noexcept {
45 return static_cast<usize>(val);
46 }
47 #endif // C++20
48 } // namespace size_literals
49
50 namespace size_literals {
51 #if MPT_CXX_AT_LEAST(20)
52 consteval ssize operator""_z(unsigned long long val) noexcept {
53 if (!mpt::in_range<ssize>(val)) {
54 mpt::constexpr_throw(std::domain_error(""));
55 }
56 return static_cast<ssize>(val);
57 }
58 #else // !C++20
59 constexpr ssize operator""_z(unsigned long long val) noexcept {
60 return static_cast<ssize>(val);
61 }
62 #endif // C++20
63 } // namespace size_literals
64
65
66
67 } // namespace MPT_INLINE_NS
68 } // namespace mpt
69
70
71
72 #endif // MPT_BASE_SIZE_HPP