Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_datatype01.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 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#ifndef TEST_DATATYPE01_HPP_
25#define TEST_DATATYPE01_HPP_
26
27#include <cassert>
28#include <cinttypes>
29#include <cstdint>
30#include <cstring>
31#include <random>
32
33#include <jau/cpp_lang_util.hpp>
36#include <jau/basic_types.hpp>
37
38using namespace jau;
39
40__pack ( struct Addr48Bit {
41 uint8_t b[6]; // == sizeof(Addr48Bit)
42
43 constexpr Addr48Bit() noexcept : b{0} { }
44 constexpr Addr48Bit(const uint64_t encoded) noexcept
45 : b{static_cast<uint8_t>(encoded & 0xff),
46 static_cast<uint8_t>( ( encoded >> 8 ) & 0xff),
47 static_cast<uint8_t>( ( encoded >> 16 ) & 0xff),
48 static_cast<uint8_t>( ( encoded >> 24 ) & 0xff),
49 static_cast<uint8_t>( ( encoded >> 32 ) & 0xff),
50 static_cast<uint8_t>( ( encoded >> 40 ) & 0xff)} { }
51
52 Addr48Bit(const uint8_t * b_) noexcept {
53 memcpy(b, b_, sizeof(b));
54 }
55 // Trivially-copyable
56 constexpr Addr48Bit(const Addr48Bit &o) noexcept = default;
57 Addr48Bit(Addr48Bit &&o) noexcept = default;
58 constexpr Addr48Bit& operator=(const Addr48Bit &o) noexcept = default;
59 Addr48Bit& operator=(Addr48Bit &&o) noexcept = default;
60
61 bool next() noexcept {
62 for(int i=0; i<6; ++i) {
63 if(b[i] < 0xff ) {
64 ++b[i];
65 for(int j=i-1; j>=0; --j) {
66 b[j]=0;
67 }
68 return true;
69 }
70 }
71 return false;
72 }
73 void random(std::default_random_engine& e) {
74 std::uniform_int_distribution<uint8_t> d(0, 255);
75 for(uint8_t & i : b) {
76 i = static_cast<uint8_t>( d(e) );
77 }
78 }
79
80 constexpr std::size_t hash_code() const noexcept {
81 // 31 * x == (x << 5) - x
82 std::size_t h = b[0];
83 h = ( ( h << 5 ) - h ) + b[1];
84 h = ( ( h << 5 ) - h ) + b[2];
85 h = ( ( h << 5 ) - h ) + b[3];
86 h = ( ( h << 5 ) - h ) + b[4];
87 h = ( ( h << 5 ) - h ) + b[5];
88 // printf("hash.Addr48Bit %zu\n", h);
89 return h;
90 }
91
92 std::string toString() const noexcept {
93 std::string str;
94 str.reserve(17);
95
96 for(int i=6-1; 0 <= i; --i) {
97 jau::byteHexString(str, b[i], false /* lowerCase */);
98 if( 0 < i ) {
99 str.push_back(':');
100 }
101 }
102 return str;
103 }
104
105#if 0
106 constexpr_cxx20 operator std::string() const noexcept {
107 return toString();
108 }
109#endif
110
111} );
112
114
115std::ostream & operator << (std::ostream &out, const Addr48Bit &a) {
116 out << a.toString();
117 return out;
118}
119
120inline bool operator==(const Addr48Bit& lhs, const Addr48Bit& rhs) noexcept {
121 if( &lhs == &rhs ) {
122 return true;
123 }
124 //return !memcmp(&lhs, &rhs, sizeof(Addr48Bit));
125 const uint8_t * a = lhs.b;
126 const uint8_t * b = rhs.b;
127 return a[0] == b[0] &&
128 a[1] == b[1] &&
129 a[2] == b[2] &&
130 a[3] == b[3] &&
131 a[4] == b[4] &&
132 a[5] == b[5];
133}
134
135inline bool operator!=(const Addr48Bit& lhs, const Addr48Bit& rhs) noexcept
136{ return !(lhs == rhs); }
137
138
140 public:
142 uint8_t type;
143
144 private:
145 jau::relaxed_atomic_size_t hash = 0; // default 0, cache
146
147 public:
148 DataType01(const Addr48Bit & address_, uint8_t type_)
149 : address(address_), type(type_) {}
150
151 DataType01(const uint64_t encoded) noexcept
152 : address(encoded), type(0) { }
153
154 constexpr DataType01() noexcept : address(), type{0} { }
155 DataType01(const DataType01 &o) noexcept : address(o.address), type(o.type) { }
156 DataType01(DataType01 &&o) noexcept {
157 address = std::move(o.address);
158 type = std::move(o.type);
159 }
160 constexpr DataType01& operator=(const DataType01 &o) noexcept {
161 address = o.address;
162 type = o.type;
163 return *this;
164 }
166 address = std::move(o.address);
167 type = std::move(o.type);
168 return *this;
169 }
170
171 int nop() const noexcept { return address.b[0]+1; }
172
173 std::size_t hash_code() const noexcept {
174 std::size_t h = hash;
175 if( 0 == h ) {
176 // 31 * x == (x << 5) - x
177 h = 31 + address.hash_code();
178 h = ((h << 5) - h) + type;
179 const_cast<DataType01 *>(this)->hash = h;
180 // printf("hash.dataSet01 new %zu\n", h);
181 } else {
182 // printf("hash.dataSet01 *cache* %zu\n", h);
183 }
184 return h;
185 }
186
187 void clearHash() { hash = 0; }
188
189 std::string toString() const noexcept {
190 std::string res;
191 return res.append("[").append(address.toString()).append(", ").append(std::to_string(type)).append("]");
192 }
193#if 0
194 constexpr_cxx20 operator std::string() const noexcept {
195 return toString();
196 }
197#endif
198};
200
201std::ostream & operator << (std::ostream &out, const DataType01 &a) {
202 out << a.toString();
203 return out;
204}
205
206inline bool operator==(const DataType01& lhs, const DataType01& rhs) noexcept {
207 if( &lhs == &rhs ) {
208 return true;
209 }
210 return lhs.address == rhs.address &&
211 lhs.type == rhs.type;
212}
213inline bool operator!=(const DataType01& lhs, const DataType01& rhs) noexcept
214{ return !(lhs == rhs); }
215
217 public:
218 typedef std::true_type container_memmove_compliant;
219 typedef std::true_type enforce_secmem;
220
222 uint8_t type;
223
224 private:
225 jau::relaxed_atomic_size_t hash = 0; // default 0, cache
226
227 public:
228 DataType02_Memmove_Secmem(const Addr48Bit & address_, uint8_t type_)
229 : address(address_), type(type_) {}
230
231 DataType02_Memmove_Secmem(const uint64_t encoded) noexcept
232 : address(encoded), type(0) { }
233
234 constexpr DataType02_Memmove_Secmem() noexcept : address(), type{0} { }
235 DataType02_Memmove_Secmem(const DataType02_Memmove_Secmem &o) noexcept : address(o.address), type(o.type) { }
237 address = std::move(o.address);
238 type = std::move(o.type);
239 }
241 address = o.address;
242 type = o.type;
243 return *this;
244 }
246 address = std::move(o.address);
247 type = std::move(o.type);
248 return *this;
249 }
250
251 int nop() const noexcept { return address.b[0]+1; }
252
253 std::size_t hash_code() const noexcept {
254 std::size_t h = hash;
255 if( 0 == h ) {
256 // 31 * x == (x << 5) - x
257 h = 31 + address.hash_code();
258 h = ((h << 5) - h) + type;
259 const_cast<DataType02_Memmove_Secmem *>(this)->hash = h;
260 // printf("hash.dataSet01 new %zu\n", h);
261 } else {
262 // printf("hash.dataSet01 *cache* %zu\n", h);
263 }
264 return h;
265 }
266
267 void clearHash() { hash = 0; }
268
269 std::string toString() const noexcept {
270 std::string res;
271 return res.append("[").append(address.toString()).append(", ").append(std::to_string(type)).append("]");
272 }
273#if 0
274 constexpr_cxx20 operator std::string() const noexcept {
275 return toString();
276 }
277#endif
278};
280
281std::ostream & operator << (std::ostream &out, const DataType02_Memmove_Secmem &a) {
282 out << a.toString();
283 return out;
284}
285
286inline bool operator==(const DataType02_Memmove_Secmem& lhs, const DataType02_Memmove_Secmem& rhs) noexcept {
287 if( &lhs == &rhs ) {
288 return true;
289 }
290 return lhs.address == rhs.address &&
291 lhs.type == rhs.type;
292}
293inline bool operator!=(const DataType02_Memmove_Secmem& lhs, const DataType02_Memmove_Secmem& rhs) noexcept
294{ return !(lhs == rhs); }
295
296// injecting specialization of std::hash to namespace std of our types above
297namespace std
298{
299 template<> struct hash<Addr48Bit> {
300 std::size_t operator()(Addr48Bit const& a) const noexcept {
301 return a.hash_code();
302 }
303 };
304
305 template<> struct hash<DataType01> {
306 std::size_t operator()(DataType01 const& a) const noexcept {
307 return a.hash_code();
308 }
309 };
310}
311
312#endif /* TEST_DATATYPE01_HPP_ */
DataType01(DataType01 &&o) noexcept
std::string toString() const noexcept
constexpr DataType01 & operator=(const DataType01 &o) noexcept
DataType01 & operator=(DataType01 &&o) noexcept
DataType01(const DataType01 &o) noexcept
int nop() const noexcept
std::size_t hash_code() const noexcept
Addr48Bit address
DataType01(const Addr48Bit &address_, uint8_t type_)
constexpr DataType01() noexcept
DataType01(const uint64_t encoded) noexcept
DataType02_Memmove_Secmem(const Addr48Bit &address_, uint8_t type_)
std::string toString() const noexcept
DataType02_Memmove_Secmem(const uint64_t encoded) noexcept
constexpr DataType02_Memmove_Secmem() noexcept
constexpr DataType02_Memmove_Secmem & operator=(const DataType02_Memmove_Secmem &o) noexcept
std::true_type container_memmove_compliant
DataType02_Memmove_Secmem & operator=(DataType02_Memmove_Secmem &&o) noexcept
DataType02_Memmove_Secmem(DataType02_Memmove_Secmem &&o) noexcept
std::size_t hash_code() const noexcept
int nop() const noexcept
DataType02_Memmove_Secmem(const DataType02_Memmove_Secmem &o) noexcept
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
#define JAU_TYPENAME_CUE_ALL(A)
std::string & byteHexString(std::string &dest, const uint8_t value, const bool lowerCase) noexcept
Produce a hexadecimal string representation of the given byte value.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
STL namespace.
std::string toString() const noexcept
uint8_t b[6]
constexpr std::size_t hash_code() const noexcept
std::size_t operator()(Addr48Bit const &a) const noexcept
std::size_t operator()(DataType01 const &a) const noexcept
bool operator!=(const Addr48Bit &lhs, const Addr48Bit &rhs) noexcept
bool operator==(const Addr48Bit &lhs, const Addr48Bit &rhs) noexcept