make_uuid

This commit is contained in:
Marius Bancila 2017-12-08 10:27:22 +02:00
parent 7e845c917b
commit 967ba08e7f
5 changed files with 99 additions and 16 deletions

89
src/uuid.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "uuid.h"
#ifdef _WIN32
#include <objbase.h>
#elif defined(__linux__) || defined(__unix__)
#include <uuid/uuid.h>
#elif defined(__APPLE__)
#include <CoreFoundation/CFUUID.h>
#endif
namespace uuids
{
uuid make_uuid()
{
#ifdef _WIN32
GUID newId;
::CoCreateGuid(&newId);
std::array<unsigned char, 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),
(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<unsigned char, 16> 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);
}
}

View File

@ -92,6 +92,7 @@ namespace uuids
public:
constexpr uuid() {}
constexpr uuid(std::array<uint8_t, 16> 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

Binary file not shown.

View File

@ -84,7 +84,7 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
@ -97,7 +97,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
@ -110,7 +110,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
@ -127,7 +127,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
@ -144,16 +144,10 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\src\uuid.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\..\src\uuid.cpp" />
<ClCompile Include="test_win.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -15,9 +15,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -26,10 +23,10 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<ClCompile Include="test_win.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test_win.cpp">
<ClCompile Include="..\..\src\uuid.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>