Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
int_types.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef JAU_INT_TYPES_HPP_
26#define JAU_INT_TYPES_HPP_
27
28#include <cstdint>
29#include <cstring>
30
31#include <jau/cpp_lang_util.hpp>
33
34namespace jau {
35 /** @defgroup Integer Integer types and arithmetic
36 * Integral integer types and arithmetic.
37 *
38 * Further support is coming from \ref ByteUtils and meta-group \ref Math
39 *
40 * @{
41 */
42
43 /**
44 * Natural 'size_t' alternative using `uint_fast32_t` as its natural sized type.
45 * <p>
46 * The leading 'n' stands for natural.
47 * </p>
48 * <p>
49 * This is a compromise to indicate intend,
50 * but to avoid handling a multiple sized `size_t` footprint where not desired.
51 * </p>
52 */
53 typedef uint_fast32_t nsize_t;
54
55 /**
56 * Natural 'ssize_t' alternative using `int_fast32_t` as its natural sized type.
57 * <p>
58 * The leading 'n' stands for natural.
59 * </p>
60 * <p>
61 * This is a compromise to indicate intend,
62 * but to avoid handling a multiple sized `ssize_t` footprint where not desired.
63 * </p>
64 */
65 typedef int_fast32_t snsize_t;
66
67 // Remember: constexpr specifier used in a function or static data member (since C++17) declaration implies inline.
68
69 template <int bytesize> struct uint_bytes;
70 template <> struct uint_bytes<4>{ using type = uint32_t; };
71 template <> struct uint_bytes<8>{ using type = uint64_t; };
72 #if defined(__SIZEOF_INT128__)
73 template <> struct uint_bytes<16>{ using type = uint128_t; };
74 #endif
75
76 template <int bytesize> struct sint_bytes;
77 template <> struct sint_bytes<4>{ using type = int32_t; };
78 template <> struct sint_bytes<8>{ using type = int64_t; };
79 #if defined(__SIZEOF_INT128__)
80 template <> struct sint_bytes<16>{ using type = int128_t; };
81 #endif
82
83 template <int bytesize> struct float_bytes;
84 template <> struct float_bytes<sizeof(float)>{ using type = float; };
85 template <> struct float_bytes<sizeof(double)>{ using type = double; };
86 template <> struct float_bytes<sizeof(long double)>{ using type = long double; };
87
88 /**
89 // *************************************************
90 // *************************************************
91 // *************************************************
92 */
93
94 __pack( /** A 128-bit packed uint8_t data array */ struct uint128dp_t {
95 uint8_t data[16];
96
97 constexpr uint128dp_t() noexcept : data{0} {}
98 constexpr uint128dp_t(const uint8_t v[16]) noexcept : data{0} { *this = pointer_cast<packed_t<uint128dp_t>*>( v )->store; }
99 constexpr uint128dp_t(const uint128dp_t &o) noexcept = default;
100 uint128dp_t(uint128dp_t &&o) noexcept = default;
101 constexpr uint128dp_t& operator=(const uint128dp_t &o) noexcept = default;
102 uint128dp_t& operator=(uint128dp_t &&o) noexcept = default;
103
104 void clear() noexcept { ::bzero(data, sizeof(data)); }
105
106 constexpr bool operator==(uint128dp_t const &o) const noexcept {
107 if( this == &o ) {
108 return true;
109 }
110 return !std::memcmp(data, o.data, sizeof(data));
111 }
112 constexpr bool operator!=(uint128dp_t const &o) const noexcept
113 { return !(*this == o); }
114 } ) ;
115
116 __pack( /** A 196-bit packed uint8_t data array */ struct uint192dp_t {
117 uint8_t data[24];
118
119 constexpr uint192dp_t() noexcept : data{0} {}
120 constexpr uint192dp_t(const uint8_t v[24]) noexcept : data{0} { *this = pointer_cast<packed_t<uint192dp_t>*>( v )->store; }
121 constexpr uint192dp_t(const uint192dp_t &o) noexcept = default;
122 uint192dp_t(uint192dp_t &&o) noexcept = default;
123 constexpr uint192dp_t& operator=(const uint192dp_t &o) noexcept = default;
124 uint192dp_t& operator=(uint192dp_t &&o) noexcept = default;
125
126 void clear() noexcept { ::bzero(data, sizeof(data)); }
127
128 constexpr bool operator==(uint192dp_t const &o) const noexcept {
129 if( this == &o ) {
130 return true;
131 }
132 return !std::memcmp(data, o.data, sizeof(data));
133 }
134 constexpr bool operator!=(uint192dp_t const &o) const noexcept
135 { return !(*this == o); }
136 } );
137
138 __pack( /** A 256-bit packed uint8_t data array */ struct uint256dp_t {
139 uint8_t data[32];
140
141 constexpr uint256dp_t() noexcept : data{0} {}
142 constexpr uint256dp_t(const uint8_t v[32]) noexcept : data{0} { *this = pointer_cast<packed_t<uint256dp_t>*>( v )->store; }
143 constexpr uint256dp_t(const uint256dp_t &o) noexcept = default;
144 uint256dp_t(uint256dp_t &&o) noexcept = default;
145 constexpr uint256dp_t& operator=(const uint256dp_t &o) noexcept = default;
146 uint256dp_t& operator=(uint256dp_t &&o) noexcept = default;
147
148 void clear() noexcept { ::bzero(data, sizeof(data)); }
149
150 constexpr bool operator==(uint256dp_t const &o) const noexcept {
151 if( this == &o ) {
152 return true;
153 }
154 return !std::memcmp(data, o.data, sizeof(data));
155 }
156 constexpr bool operator!=(uint256dp_t const &o) const noexcept
157 { return !(*this == o); }
158 } );
159
160 namespace int_literals {
161 /** Literal for signed int8_t */
162 constexpr int8_t operator ""_i8(unsigned long long int __v) { return (int8_t)__v; }
163
164 /** Literal for unsigned uint8_t */
165 constexpr uint8_t operator ""_u8(unsigned long long int __v) { return (uint8_t)__v; }
166
167 /** Literal for signed int16_t */
168 constexpr int16_t operator ""_i16(unsigned long long int __v) { return (int16_t)__v; }
169
170 /** Literal for unsigned uint16_t */
171 constexpr uint16_t operator ""_u16(unsigned long long int __v) { return (uint16_t)__v; }
172
173 /** Literal for signed int32_t */
174 constexpr int32_t operator ""_i32(unsigned long long int __v) { return (int32_t)__v; }
175
176 /** Literal for unsigned uint32_t */
177 constexpr uint32_t operator ""_u32(unsigned long long int __v) { return (uint32_t)__v; }
178
179 /** Literal for signed int64_t */
180 constexpr int64_t operator ""_i64(unsigned long long int __v) { return (int64_t)__v; }
181
182 /** Literal for unsigned uint64_t */
183 constexpr uint64_t operator ""_u64(unsigned long long int __v) { return (uint64_t)__v; }
184
185 /** Literal for signed ssize_t */
186 constexpr ssize_t operator ""_iz(unsigned long long int __v) { return (ssize_t)__v; }
187
188 /** Literal for unsigned size_t */
189 constexpr size_t operator ""_uz(unsigned long long int __v) { return (size_t)__v; }
190
191 /** Literal for signed jau::snsize_t */
192 constexpr jau::snsize_t operator ""_inz(unsigned long long int __v) { return (jau::snsize_t)__v; }
193
194 /** Literal for unsigned jau::nsize_t */
195 constexpr jau::nsize_t operator ""_unz(unsigned long long int __v) { return (jau::nsize_t)__v; }
196 }
197
198 /**@}*/
199
200} // namespace jau
201
202/** \example test_basictypeconv.cpp
203 * This C++ unit test validates the jau::bswap and get/set value implementation
204 */
205
206#endif /* JAU_INT_TYPES_HPP_ */
void clear() noexcept
Clears internal list.
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:53
int_fast32_t snsize_t
Natural 'ssize_t' alternative using int_fast32_t as its natural sized type.
Definition: int_types.hpp:65
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
bool operator==(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
Definition: callocator.hpp:150
bool operator!=(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
Definition: callocator.hpp:163
A 128-bit packed uint8_t data array.
Definition: int_types.hpp:114
A 196-bit packed uint8_t data array.
Definition: int_types.hpp:136
A 256-bit packed uint8_t data array.
Definition: int_types.hpp:158