From 8353b03ebcd719f70330f4aa629c089eb73605c0 Mon Sep 17 00:00:00 2001 From: Marius Bancila Date: Mon, 15 Jan 2018 10:29:32 +0200 Subject: [PATCH] std::byte replaced with uint_8 --- include/uuid.cpp | 108 +++++++++++++++++++++++------------------------ include/uuid.h | 61 ++++++++++++++++++-------- test/test.cpp | 53 ++++++++++++++--------- 3 files changed, 131 insertions(+), 91 deletions(-) diff --git a/include/uuid.cpp b/include/uuid.cpp index 25e086c..ce32323 100644 --- a/include/uuid.cpp +++ b/include/uuid.cpp @@ -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 bytes = + std::array 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 bytes = + std::array 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 bytes = + std::array 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 diff --git a/include/uuid.h b/include/uuid.h index 6e3c8d0..bb7a84a 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -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 data{ { std::byte{0}} }; + std::array 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 + 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 + uuid make_uuid(Generator & g) + { + return g(); + } } namespace std diff --git a/test/test.cpp b/test/test.cpp index c2e7ff5..3aba201 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -3,6 +3,7 @@ #include #include #include +#include int main() { @@ -50,26 +51,24 @@ int main() using namespace std::string_literals; { - std::array 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 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 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 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 rgen(mtgen); + auto id4 = make_uuid(rgen); + auto id5 = make_uuid(rgen); + } + std::cout << "ALL PASSED" << std::endl; return 0;