Skip to content

Instantly share code, notes, and snippets.

@kennykerr

kennykerr/make_guid.cpp

Last active Nov 19, 2020
Embed
What would you like to do?
#include <string_view>
#include <exception>
#include <assert.h>
using namespace std::literals;
struct guid
{
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
};
constexpr bool operator==(guid const& left, guid const& right) noexcept
{
return left.Data1 == right.Data1 &&
left.Data2 == right.Data2 &&
left.Data3 == right.Data3 &&
left.Data4[0] == right.Data4[0] &&
left.Data4[1] == right.Data4[1] &&
left.Data4[2] == right.Data4[2] &&
left.Data4[3] == right.Data4[3] &&
left.Data4[4] == right.Data4[4] &&
left.Data4[5] == right.Data4[5] &&
left.Data4[6] == right.Data4[6] &&
left.Data4[7] == right.Data4[7];
}
constexpr uint32_t to_uint(char const value) noexcept
{
if (value >= '0' && value <= '9')
{
return value - '0';
}
if (value >= 'A' && value <= 'F')
{
return 10 + value - 'A';
}
if (value >= 'a' && value <= 'f')
{
return 10 + value - 'a';
}
std::terminate();
}
constexpr guid make_guid(std::string_view const& value) noexcept
{
if (value.size() != 36 || value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-')
{
std::terminate();
}
return
{
((to_uint(value[0]) * 16 + to_uint(value[1])) << 24) +
((to_uint(value[2]) * 16 + to_uint(value[3])) << 16) +
((to_uint(value[4]) * 16 + to_uint(value[5])) << 8) +
(to_uint(value[6]) * 16 + to_uint(value[7])),
static_cast<uint16_t>(((to_uint(value[9]) * 16 + to_uint(value[10])) << 8) +
(to_uint(value[11]) * 16 + to_uint(value[12]))),
static_cast<uint16_t>(((to_uint(value[14]) * 16 + to_uint(value[15])) << 8) +
(to_uint(value[16]) * 16 + to_uint(value[17]))),
{
static_cast<uint8_t>(to_uint(value[19]) * 16 + to_uint(value[20])),
static_cast<uint8_t>(to_uint(value[21]) * 16 + to_uint(value[22])),
static_cast<uint8_t>(to_uint(value[24]) * 16 + to_uint(value[25])),
static_cast<uint8_t>(to_uint(value[26]) * 16 + to_uint(value[27])),
static_cast<uint8_t>(to_uint(value[28]) * 16 + to_uint(value[29])),
static_cast<uint8_t>(to_uint(value[30]) * 16 + to_uint(value[31])),
static_cast<uint8_t>(to_uint(value[32]) * 16 + to_uint(value[33])),
static_cast<uint8_t>(to_uint(value[34]) * 16 + to_uint(value[35])),
}
};
}
static constexpr guid a
{ 0x8aa90cad, 0xfed1, 0x4c54, { 0x89, 0xdb, 0x9b, 0x75, 0x22, 0xd8, 0xaa, 0x92 } };
static constexpr guid b
{ make_guid("8AA90CAD-fed1-4c54-89db-9B7522D8AA92"sv) };
static_assert(a == b);
int main()
{
assert(a == b);
}
@DrusTheAxe

This comment has been minimized.

Copy link

@DrusTheAxe DrusTheAxe commented Nov 19, 2020

Any plans to add this to C++/WinRT?
And add wchar_t/wstring flavors?

@kennykerr

This comment has been minimized.

Copy link
Owner Author

@kennykerr kennykerr commented Nov 19, 2020

Hi Howard, if you have a need for this please create an issue and I'll try to knock it off when I get a moment. https://github.com/microsoft/cppwinrt/issues

@DrusTheAxe

This comment has been minimized.

Copy link

@DrusTheAxe DrusTheAxe commented Nov 19, 2020

Ask and ye shall receive :-)
microsoft/cppwinrt#797

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.