Add guid type

This commit is contained in:
Justin C. Miller
2018-05-16 00:29:43 -07:00
parent a7e20fd390
commit a5da56d02f
2 changed files with 56 additions and 0 deletions

38
src/modules/kutil/guid.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
/// \file guid.h
/// Definition of the guid type and related functions
#include <stdint.h>
#include "kutil/misc.h"
namespace kutil {
/// A GUID
struct guid
{
uint64_t a, b;
};
/// Make a GUID by writing it naturally-ordered in code:
/// AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE becomes:
/// make_guid(0xAAAAAAAA, 0xBBBB, 0xCCCC, 0xDDDD, 0xEEEEEEEEEEEE);
/// \returns The guid object
inline constexpr guid make_guid(uint32_t a, uint16_t b, uint16_t c, uint16_t d, uint64_t e)
{
const uint64_t h =
static_cast<uint64_t>(c) << 48 |
static_cast<uint64_t>(b) << 32 |
a;
const uint64_t l =
static_cast<uint64_t>(byteswap(e & 0xffffffff)) << 32 |
(byteswap(e >> 32) & 0xffff0000) |
((d << 8) & 0xff00) | ((d >> 8) & 0xff);
return {h, l};
}
} // namespace kutil
inline bool operator==(const kutil::guid &a, const kutil::guid &b) { return a.a == b.a && a.b == b.b; }