From 967ba08e7fabcf9328aa7bcc62ecabb78c8f4059 Mon Sep 17 00:00:00 2001 From: Marius Bancila Date: Fri, 8 Dec 2017 10:27:22 +0200 Subject: [PATCH] make_uuid --- src/uuid.cpp | 89 +++++++++++++++++++++++++ src/uuid.h | 3 + test/test_win/test_win.cpp | Bin 586 -> 2074 bytes test/test_win/test_win.vcxproj | 16 ++--- test/test_win/test_win.vcxproj.filters | 7 +- 5 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 src/uuid.cpp diff --git a/src/uuid.cpp b/src/uuid.cpp new file mode 100644 index 0000000..ebb6997 --- /dev/null +++ b/src/uuid.cpp @@ -0,0 +1,89 @@ +#include "uuid.h" + +#ifdef _WIN32 +#include +#elif defined(__linux__) || defined(__unix__) +#include +#elif defined(__APPLE__) +#include +#endif + +namespace uuids +{ + uuid make_uuid() + { +#ifdef _WIN32 + + GUID newId; + ::CoCreateGuid(&newId); + + std::array bytes = + { + (unsigned char)((newId.Data1 >> 24) & 0xFF), + (unsigned char)((newId.Data1 >> 16) & 0xFF), + (unsigned char)((newId.Data1 >> 8) & 0xFF), + (unsigned char)((newId.Data1) & 0xff), + + (unsigned char)((newId.Data2 >> 8) & 0xFF), + (unsigned char)((newId.Data2) & 0xff), + + (unsigned char)((newId.Data3 >> 8) & 0xFF), + (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] + }; + + return uuid{ bytes }; + +#elif defined(__linux__) || defined(__unix__) + + uuid_t id; + uuid_generate(id); + return uuid{ id }; + +#elif defined(__APPLE__) + auto newId = CFUUIDCreate(NULL); + auto bytes = CFUUIDGetUUIDBytes(newId); + CFRelease(newId); + + std::array byteArray = + { + 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{ byteArray }; +#elif + return uuid{}; +#endif + } +} + +namespace std +{ + template <> + void swap(uuids::uuid & lhs, uuids::uuid & rhs) + { + lhs.swap(rhs); + } +} \ No newline at end of file diff --git a/src/uuid.h b/src/uuid.h index 6e1a8e3..247d6ac 100644 --- a/src/uuid.h +++ b/src/uuid.h @@ -92,6 +92,7 @@ namespace uuids public: constexpr uuid() {} + constexpr uuid(std::array const & bytes) :data{bytes} {} variant_type variant() const noexcept { @@ -212,6 +213,8 @@ namespace uuids << std::setw(2) << (int)id.data[14] << std::setw(2) << (int)id.data[15]; } + + uuid make_uuid(); } namespace std diff --git a/test/test_win/test_win.cpp b/test/test_win/test_win.cpp index e71e04abbb4204d0783bf07209821808d409eccf..535447294ebb1f7882bc5e2cab413f0b88d556d9 100644 GIT binary patch literal 2074 zcmdT_$xg#C5PfGP{=qGmRzfLUIF#kYXCOr(U5IGYDo#Ow_;ujTq;8z3OOhM1Vmq0c zH*Xd@zds|4V31=9%Q?h55~K(aW6fbO=JQDDOuvhkXut4_;gfQo;e-1%R;rP6KjS|0 z^kTlJv|M4Kc9CK)s2?LDLZb2j=Qtyqk**ht7Sm#biCXo^D!|irw)7D5ZNrgJBOs^# z0n0mju^dy{$Bp9T&3z$N>As-XB81OQQZ6}D_^h9YQ|2k zdTrAK$2!^Nrh9kDDQ$8#TtAm=su3NluCsM{J#ATfG3aT(@3;MbdF*o1nw-;m9p!&B z(Nkgh*O7mMf74xh_lc`U9ie(l_IP&OjoKv!SHHuyb^pG7l%2R{%j6bTg~>A*Er3{R@&`tR$sgFY E0Ib6ic>n+a diff --git a/test/test_win/test_win.vcxproj b/test/test_win/test_win.vcxproj index 80338d4..f0db909 100644 --- a/test/test_win/test_win.vcxproj +++ b/test/test_win/test_win.vcxproj @@ -84,7 +84,7 @@ - Use + NotUsing Level3 Disabled true @@ -97,7 +97,7 @@ - Use + NotUsing Level3 Disabled true @@ -110,7 +110,7 @@ - Use + NotUsing Level3 MaxSpeed true @@ -127,7 +127,7 @@ - Use + NotUsing Level3 MaxSpeed true @@ -144,16 +144,10 @@ - - - Create - Create - Create - Create - + diff --git a/test/test_win/test_win.vcxproj.filters b/test/test_win/test_win.vcxproj.filters index 2c975bc..7adf8d3 100644 --- a/test/test_win/test_win.vcxproj.filters +++ b/test/test_win/test_win.vcxproj.filters @@ -15,9 +15,6 @@ - - Header Files - Header Files @@ -26,10 +23,10 @@ - + Source Files - + Source Files