Skip to content

Commit

Permalink
Merge pull request #40 from tusharpm/generators_test_windows
Browse files Browse the repository at this point in the history
Enable generator tests on Windows CI
  • Loading branch information
mariusbancila authored Jun 25, 2021
2 parents 297cd66 + 1562616 commit 4959d46
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

option(UUID_BUILD_TESTS "Build the unit tests" ON)
option(UUID_SYSTEM_GENERATOR "Enable operating system uuid generator" OFF)
option(UUID_TIME_GENERATOR "Enable experimental time-based uuid generator" OFF)
option(UUID_USING_CXX20_SPAN "Using span from std instead of gsl" OFF)

# Library target
Expand All @@ -29,6 +30,11 @@ if (UUID_SYSTEM_GENERATOR)
endif ()
endif ()

# Using time-based generator
if (UUID_TIME_GENERATOR)
target_compile_definitions(${PROJECT_NAME} INTERFACE UUID_TIME_GENERATOR)
endif()

# Using span from std
if (NOT UUID_USING_CXX20_SPAN)
target_include_directories(${PROJECT_NAME} INTERFACE
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ environment:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
CMAKE_GENERATOR: Visual Studio 15 2017
CMAKE_GENERATOR_PLATFORM: x64
CMAKE_CLI_FLAGS: -DUUID_SYSTEM_GENERATOR=ON -DUUID_TIME_GENERATOR=ON

init:
- cmake --version
- msbuild /version

before_build:
- cmake -S . -B build
- cmake %CMAKE_CLI_FLAGS% -S . -B build
- cd build

build_script:
Expand Down
33 changes: 11 additions & 22 deletions include/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@
#include <atomic>
#include <span>

#if defined(UUID_TIME_GENERATOR) || defined(UUID_SYSTEM_GENERATOR)
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif

#ifdef UUID_SYSTEM_GENERATOR
#include <objbase.h>
#endif

#include <windows.h>
#include <intrin.h>
#ifdef UUID_TIME_GENERATOR
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
#endif

#elif defined(__linux__) || defined(__unix__)

Expand All @@ -47,7 +40,6 @@
#include <CoreFoundation/CFUUID.h>
#endif

#endif
#endif

namespace uuids
Expand Down Expand Up @@ -278,10 +270,6 @@ namespace uuids
size_t m_blockByteIndex;
size_t m_byteCount;
};

static std::mt19937 clock_gen(std::random_device{}());
static std::uniform_int_distribution<short> clock_dis{ -32768, 32767 };
static std::atomic_short clock_sequence = clock_dis(clock_gen);
}

// --------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -847,11 +835,15 @@ namespace uuids
return ns / 100;
}

public:
uuid_time_generator()
static unsigned short get_clock_sequence()
{
static std::mt19937 clock_gen(std::random_device{}());
static std::uniform_int_distribution<unsigned short> clock_dis;
static std::atomic_ushort clock_sequence = clock_dis(clock_gen);
return clock_sequence++;
}

public:
uuid operator()()
{
if (get_mac_address())
Expand All @@ -860,25 +852,22 @@ namespace uuids

auto tm = get_time_intervals();

short clock_seq = detail::clock_sequence++;

clock_seq &= 0x3FFF;
auto clock_seq = get_clock_sequence();

auto ptm = reinterpret_cast<uuids::uuid::value_type*>(&tm);
ptm[0] &= 0x0F;

memcpy(&data[0], ptm + 4, 4);
memcpy(&data[4], ptm + 2, 2);
memcpy(&data[6], ptm, 2);

memcpy(&data[8], reinterpret_cast<uuids::uuid::value_type*>(&clock_seq), 2);
memcpy(&data[8], &clock_seq, 2);

// variant must be 0b10xxxxxx
data[8] &= 0xBF;
data[8] |= 0x80;

// version must be 0b0001xxxx
data[6] &= 0x5F;
data[6] &= 0x1F;
data[6] |= 0x10;

memcpy(&data[10], &device_address.value()[0], 6);
Expand Down

0 comments on commit 4959d46

Please sign in to comment.