std::byte replaced with uint_8

This commit is contained in:
Marius Bancila 2018-01-15 10:29:32 +02:00
parent ba56824f4c
commit 8353b03ebc
3 changed files with 131 additions and 91 deletions

View File

@ -63,7 +63,7 @@ namespace uuids
if (index >= 16 || !is_hex(str[i]))
{
std::fill(std::begin(data), std::end(data), std::byte{ 0 });
std::fill(std::begin(data), std::end(data), 0);
return;
}
@ -74,14 +74,14 @@ namespace uuids
}
else
{
data[index++] = std::byte{ hexpair2char(digit, str[i]) };
data[index++] = hexpair2char(digit, str[i]);
firstdigit = true;
}
}
if (index < 16)
{
std::fill(std::begin(data), std::end(data), std::byte{ 0 });
std::fill(std::begin(data), std::end(data), 0);
}
}
@ -92,27 +92,27 @@ namespace uuids
GUID newId;
::CoCreateGuid(&newId);
std::array<std::byte, 16> bytes =
std::array<uint8_t, 16> bytes =
{{
std::byte{ (unsigned char)((newId.Data1 >> 24) & 0xFF) },
std::byte{ (unsigned char)((newId.Data1 >> 16) & 0xFF) },
std::byte{ (unsigned char)((newId.Data1 >> 8) & 0xFF) },
std::byte{ (unsigned char)((newId.Data1) & 0xFF) },
(unsigned char)((newId.Data1 >> 24) & 0xFF),
(unsigned char)((newId.Data1 >> 16) & 0xFF),
(unsigned char)((newId.Data1 >> 8) & 0xFF),
(unsigned char)((newId.Data1) & 0xFF),
std::byte{ (unsigned char)((newId.Data2 >> 8) & 0xFF) },
std::byte{ (unsigned char)((newId.Data2) & 0xFF) },
(unsigned char)((newId.Data2 >> 8) & 0xFF),
(unsigned char)((newId.Data2) & 0xFF),
std::byte{ (unsigned char)((newId.Data3 >> 8) & 0xFF) },
std::byte{ (unsigned char)((newId.Data3) & 0xFF) },
(unsigned char)((newId.Data3 >> 8) & 0xFF),
(unsigned char)((newId.Data3) & 0xFF),
std::byte{ newId.Data4[0] },
std::byte{ newId.Data4[1] },
std::byte{ newId.Data4[2] },
std::byte{ newId.Data4[3] },
std::byte{ newId.Data4[4] },
std::byte{ newId.Data4[5] },
std::byte{ newId.Data4[6] },
std::byte{ newId.Data4[7] }
newId.Data4[0],
newId.Data4[1],
newId.Data4[2],
newId.Data4[3],
newId.Data4[4],
newId.Data4[5],
newId.Data4[6],
newId.Data4[7]
}};
return uuid{ std::begin(bytes), std::end(bytes) };
@ -122,24 +122,24 @@ namespace uuids
uuid_t id;
uuid_generate(id);
std::array<std::byte, 16> bytes =
std::array<uint8_t, 16> bytes =
{ {
std::byte{ id[0] },
std::byte{ id[1] },
std::byte{ id[2] },
std::byte{ id[3] },
std::byte{ id[4] },
std::byte{ id[5] },
std::byte{ id[6] },
std::byte{ id[7] },
std::byte{ id[8] },
std::byte{ id[9] },
std::byte{ id[10] },
std::byte{ id[11] },
std::byte{ id[12] },
std::byte{ id[13] },
std::byte{ id[14] },
std::byte{ id[15] }
id[0],
id[1],
id[2],
id[3],
id[4],
id[5],
id[6],
id[7],
id[8],
id[9],
id[10],
id[11],
id[12],
id[13],
id[14],
id[15]
}};
return uuid { std::begin(bytes), std::end(bytes) };
@ -149,24 +149,24 @@ namespace uuids
auto bytes = CFUUIDGetUUIDBytes(newId);
CFRelease(newId);
std::array<std::byte, 16> bytes =
std::array<uint8_t, 16> bytes =
{{
std::byte{ bytes.byte0 },
std::byte{ bytes.byte1 },
std::byte{ bytes.byte2 },
std::byte{ bytes.byte3 },
std::byte{ bytes.byte4 },
std::byte{ bytes.byte5 },
std::byte{ bytes.byte6 },
std::byte{ bytes.byte7 },
std::byte{ bytes.byte8 },
std::byte{ bytes.byte9 },
std::byte{ bytes.byte10 },
std::byte{ bytes.byte11 },
std::byte{ bytes.byte12 },
std::byte{ bytes.byte13 },
std::byte{ bytes.byte14 },
std::byte{ bytes.byte15 }
bytes.byte0,
bytes.byte1,
bytes.byte2,
bytes.byte3,
bytes.byte4,
bytes.byte5,
bytes.byte6,
bytes.byte7,
bytes.byte8,
bytes.byte9,
bytes.byte10,
bytes.byte11,
bytes.byte12,
bytes.byte13,
bytes.byte14,
bytes.byte15
}};
return uuid{ std::begin(bytes), std::end(bytes) };
#elif

View File

@ -72,13 +72,13 @@ namespace uuids
struct uuid
{
public:
typedef std::byte value_type;
typedef std::byte& reference;
typedef std::byte const& const_reference;
typedef std::byte* iterator;
typedef std::byte const* const_iterator;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef uint8_t value_type;
typedef uint8_t& reference;
typedef uint8_t const& const_reference;
typedef uint8_t* iterator;
typedef uint8_t const* const_iterator;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public:
constexpr uuid() noexcept {}
@ -95,11 +95,11 @@ namespace uuids
constexpr uuid_variant variant() const noexcept
{
if ((data[8] & std::byte{ 0x80 }) == std::byte{ 0x00 })
if ((data[8] & 0x80) == 0x00)
return uuid_variant::ncs;
else if ((data[8] & std::byte{ 0xC0 }) == std::byte{ 0x80 })
else if ((data[8] & 0xC0) == 0x80)
return uuid_variant::rfc;
else if ((data[8] & std::byte{ 0xE0 }) == std::byte{ 0xC0 })
else if ((data[8] & 0xE0) == 0xC0)
return uuid_variant::microsoft;
else
return uuid_variant::reserved;
@ -107,15 +107,15 @@ namespace uuids
constexpr uuid_version version() const noexcept
{
if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x10 })
if ((data[6] & 0xF0) == 0x10)
return uuid_version::time_based;
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x20 })
else if ((data[6] & 0xF0) == 0x20)
return uuid_version::dce_security;
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x30 })
else if ((data[6] & 0xF0) == 0x30)
return uuid_version::name_based_md5;
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x40 })
else if ((data[6] & 0xF0) == 0x40)
return uuid_version::random_number_based;
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x50 })
else if ((data[6] & 0xF0) == 0x50)
return uuid_version::name_based_sha1;
else
return uuid_version::none;
@ -125,7 +125,7 @@ namespace uuids
constexpr bool nil() const noexcept
{
for (size_t i = 0; i < data.size(); ++i) if (data[i] != std::byte{ 0 }) return false;
for (size_t i = 0; i < data.size(); ++i) if (data[i] != 0) return false;
return true;
}
@ -169,7 +169,7 @@ namespace uuids
}
private:
std::array<std::byte, 16> data{ { std::byte{0}} };
std::array<uint8_t, 16> data{ { 0 } };
friend bool operator==(uuid const & lhs, uuid const & rhs) noexcept;
friend bool operator<(uuid const & lhs, uuid const & rhs) noexcept;
@ -222,7 +222,34 @@ namespace uuids
<< std::setw(2) << (int)id.data[15];
}
class uuid_default_generator
{
public:
typedef uuid result_type;
uuid operator()() { return uuid{}; }
};
template <typename UniformRandomNumberGenerator>
class uuid_random_generator
{
public:
typedef uuid result_type;
uuid_random_generator() {}
explicit uuid_random_generator(UniformRandomNumberGenerator& gen) {}
explicit uuid_random_generator(UniformRandomNumberGenerator* pGen) {}
uuid operator()() { return uuid{}; }
};
uuid make_uuid();
template <typename Generator>
uuid make_uuid(Generator & g)
{
return g();
}
}
namespace std

View File

@ -3,6 +3,7 @@
#include <iostream>
#include <set>
#include <unordered_set>
#include <random>
int main()
{
@ -50,26 +51,24 @@ int main()
using namespace std::string_literals;
{
std::array<std::byte, 16> arr{ {
std::byte{ 0x47 }, std::byte{ 0x18 }, std::byte{ 0x38 }, std::byte{ 0x23 },
std::byte{ 0x25 }, std::byte{ 0x74 },
std::byte{ 0x4b }, std::byte{ 0xfd },
std::byte{ 0xb4 }, std::byte{ 0x11 },
std::byte{ 0x99 }, std::byte{ 0xed }, std::byte{ 0x17 }, std::byte{ 0x7d }, std::byte{ 0x3e }, std::byte{ 0x43 }
} };
std::array<uint8_t, 16> arr{ {
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43} };
uuid guid(std::begin(arr), std::end(arr));
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43"s);
}
{
std::byte arr[16] = {
std::byte{ 0x47 }, std::byte{ 0x18 }, std::byte{ 0x38 }, std::byte{ 0x23 },
std::byte{ 0x25 }, std::byte{ 0x74 },
std::byte{ 0x4b }, std::byte{ 0xfd },
std::byte{ 0xb4 }, std::byte{ 0x11 },
std::byte{ 0x99 }, std::byte{ 0xed }, std::byte{ 0x17 }, std::byte{ 0x7d }, std::byte{ 0x3e }, std::byte{ 0x43 }
};
uint8_t arr[16] = {
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 };
uuid guid(std::begin(arr), std::end(arr));
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43"s);
@ -171,12 +170,12 @@ int main()
{
std::cout << "Test iterators" << std::endl;
std::array<std::byte, 16> arr{{
std::byte{ 0x47 }, std::byte{ 0x18 }, std::byte{ 0x38 }, std::byte{ 0x23 },
std::byte{ 0x25 }, std::byte{ 0x74 },
std::byte{ 0x4b }, std::byte{ 0xfd },
std::byte{ 0xb4 }, std::byte{ 0x11 },
std::byte{ 0x99 }, std::byte{ 0xed }, std::byte{ 0x17 }, std::byte{ 0x7d }, std::byte{ 0x3e }, std::byte{ 0x43 }
std::array<uint8_t, 16> arr{{
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
}};
uuid guid;
@ -203,6 +202,20 @@ int main()
constexpr uuid_version version = empty.version();
}
{
auto id1 = make_uuid();
uuid_default_generator dgen;
auto id2 = make_uuid(dgen);
auto id3 = make_uuid(dgen);
std::random_device rd;
std::mt19937 mtgen(rd());
uuid_random_generator<std::mt19937> rgen(mtgen);
auto id4 = make_uuid(rgen);
auto id5 = make_uuid(rgen);
}
std::cout << "ALL PASSED" << std::endl;
return 0;