std::byte instead of uint_8, constructors

This commit is contained in:
Marius Bancila 2017-12-19 11:53:42 +02:00
parent c3017097ae
commit e7186f0092
4 changed files with 120 additions and 89 deletions

View File

@ -1,5 +1,12 @@
Create a project from this folder by running the command:
cmake ..
Windows
cmake -G "Visual Studio 15 2017" [arch] ..
where arch is an optional parameter and can be Win64 or ARM. Without one specified, x86 target
Mac
cmake -G Xcode ..
If you don't have CMake installed you can get it from https://cmake.org/

View File

@ -40,14 +40,14 @@ namespace
namespace uuids
{
uuid::uuid(std::string const & str)
uuid::uuid(std::string_view str)
{
create(str.c_str(), str.size());
create(str.data(), str.size());
}
uuid::uuid(std::wstring const & str)
uuid::uuid(std::wstring_view str)
{
create(str.c_str(), str.size());
create(str.data(), str.size());
}
template <typename TChar>
@ -63,7 +63,7 @@ namespace uuids
if (index >= 16 || !is_hex(str[i]))
{
std::fill(std::begin(data), std::end(data), 0);
std::fill(std::begin(data), std::end(data), std::byte{ 0 });
return;
}
@ -74,14 +74,14 @@ namespace uuids
}
else
{
data[index++] = hexpair2char(digit, str[i]);
data[index++] = std::byte{ hexpair2char(digit, str[i]) };
firstdigit = true;
}
}
if (index < 16)
{
std::fill(std::begin(data), std::end(data), 0);
std::fill(std::begin(data), std::end(data), std::byte{ 0 });
}
}
@ -92,62 +92,83 @@ namespace uuids
GUID newId;
::CoCreateGuid(&newId);
std::array<unsigned char, 16> bytes =
std::array<std::byte, 16> bytes =
{{
(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.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.Data2 >> 8) & 0xFF),
(unsigned char)((newId.Data2) & 0xff),
std::byte{ (unsigned char)((newId.Data2 >> 8) & 0xFF) },
std::byte{ (unsigned char)((newId.Data2) & 0xFF) },
(unsigned char)((newId.Data3 >> 8) & 0xFF),
(unsigned char)((newId.Data3) & 0xFF),
std::byte{ (unsigned char)((newId.Data3 >> 8) & 0xFF) },
std::byte{ (unsigned char)((newId.Data3) & 0xFF) },
(unsigned char)newId.Data4[0],
(unsigned char)newId.Data4[1],
(unsigned char)newId.Data4[2],
(unsigned char)newId.Data4[3],
(unsigned char)newId.Data4[4],
(unsigned char)newId.Data4[5],
(unsigned char)newId.Data4[6],
(unsigned char)newId.Data4[7]
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] }
}};
return uuid{ bytes };
return uuid{ std::begin(bytes), std::end(bytes) };
#elif defined(__linux__) || defined(__unix__)
uuid_t id;
uuid_generate(id);
return uuid{ id };
std::array<std::byte, 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] }
}};
return uuid { std::begin(bytes), std::end(bytes) };
#elif defined(__APPLE__)
auto newId = CFUUIDCreate(NULL);
auto bytes = CFUUIDGetUUIDBytes(newId);
CFRelease(newId);
std::array<unsigned char, 16> byteArray =
std::array<std::byte, 16> bytes =
{{
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
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 }
}};
return uuid{ byteArray };
return uuid{ std::begin(bytes), std::end(bytes) };
#elif
return uuid{};
#endif

View File

@ -5,6 +5,7 @@
#include <iomanip>
#include <array>
#include <string_view>
#include <iterator>
namespace uuids
{
@ -71,32 +72,34 @@ namespace uuids
struct uuid
{
public:
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;
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;
public:
constexpr explicit uuid() {}
constexpr explicit uuid(std::array<uint8_t, 16> const & bytes) :data{bytes} {}
explicit uuid(uint8_t const * const bytes)
constexpr uuid() noexcept {}
template<typename ForwardIterator>
explicit uuid(ForwardIterator first, ForwardIterator last)
{
std::copy(bytes, bytes + 16, std::begin(data));
if (std::distance(first, last) == 16)
std::copy(first, last, std::begin(data));
}
explicit uuid(std::string const & str);
explicit uuid(std::wstring const & str);
explicit uuid(std::string_view str);
explicit uuid(std::wstring_view str);
constexpr uuid_variant variant() const noexcept
{
if ((data[8] & 0x80) == 0x00)
if ((data[8] & std::byte{ 0x80 }) == std::byte{ 0x00 })
return uuid_variant::ncs;
else if ((data[8] & 0xC0) == 0x80)
else if ((data[8] & std::byte{ 0xC0 }) == std::byte{ 0x80 })
return uuid_variant::rfc;
else if ((data[8] & 0xE0) == 0xC0)
else if ((data[8] & std::byte{ 0xE0 }) == std::byte{ 0xC0 })
return uuid_variant::microsoft;
else
return uuid_variant::future;
@ -104,15 +107,15 @@ namespace uuids
constexpr uuid_version version() const noexcept
{
if ((data[6] & 0xF0) == 0x10)
if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x10 })
return uuid_version::time_based;
else if ((data[6] & 0xF0) == 0x20)
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x20 })
return uuid_version::dce_security;
else if ((data[6] & 0xF0) == 0x30)
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x30 })
return uuid_version::name_based_md5;
else if ((data[6] & 0xF0) == 0x40)
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x40 })
return uuid_version::random_number_based;
else if ((data[6] & 0xF0) == 0x50)
else if ((data[6] & std::byte{ 0xF0 }) == std::byte{ 0x50 })
return uuid_version::name_based_sha1;
else
return uuid_version::none;
@ -122,7 +125,7 @@ namespace uuids
constexpr bool nil() const noexcept
{
for (size_t i = 0; i < data.size(); ++i) if (data[i] != 0) return false;
for (size_t i = 0; i < data.size(); ++i) if (data[i] != std::byte{ 0 }) return false;
return true;
}
@ -166,7 +169,7 @@ namespace uuids
}
private:
std::array<uint8_t, 16> data{ 0 };
std::array<std::byte, 16> data{ { std::byte{0}} };
friend bool operator==(uuid const & lhs, uuid const & rhs) noexcept;
friend bool operator<(uuid const & lhs, uuid const & rhs) noexcept;

View File

@ -1,4 +1,4 @@
#include "..\include\uuid.h"
#include "../include/uuid.h"
#include <assert.h>
#include <iostream>
#include <set>
@ -47,17 +47,17 @@ int main()
{
std::cout << "Test std::array constructor" << std::endl;
std::array<uint8_t, 16> arr{{
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
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 }
}};
using namespace std::string_literals;
uuid guid(arr);
uuid guid(std::begin(arr), std::end(arr));
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43"s);
}
@ -66,14 +66,14 @@ int main()
using namespace std::string_literals;
uint8_t arr[16] = {
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
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 }
};
uuid guid(arr);
uuid guid(std::begin(arr), std::end(arr));
assert(guid.string() == "47183823-2574-4bfd-b411-99ed177d3e43"s);
}
@ -172,12 +172,12 @@ int main()
{
std::cout << "Test iterators" << std::endl;
std::array<uint8_t, 16> arr{{
0x47, 0x18, 0x38, 0x23,
0x25, 0x74,
0x4b, 0xfd,
0xb4, 0x11,
0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43
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 }
}};
uuid guid;