Skip to content

Commit 4959d46

Browse files
Merge pull request #40 from tusharpm/generators_test_windows
Enable generator tests on Windows CI
2 parents 297cd66 + 1562616 commit 4959d46

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
44

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

910
# Library target
@@ -29,6 +30,11 @@ if (UUID_SYSTEM_GENERATOR)
2930
endif ()
3031
endif ()
3132

33+
# Using time-based generator
34+
if (UUID_TIME_GENERATOR)
35+
target_compile_definitions(${PROJECT_NAME} INTERFACE UUID_TIME_GENERATOR)
36+
endif()
37+
3238
# Using span from std
3339
if (NOT UUID_USING_CXX20_SPAN)
3440
target_include_directories(${PROJECT_NAME} INTERFACE

appveyor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ environment:
99
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
1010
CMAKE_GENERATOR: Visual Studio 15 2017
1111
CMAKE_GENERATOR_PLATFORM: x64
12+
CMAKE_CLI_FLAGS: -DUUID_SYSTEM_GENERATOR=ON -DUUID_TIME_GENERATOR=ON
1213

1314
init:
1415
- cmake --version
1516
- msbuild /version
1617

1718
before_build:
18-
- cmake -S . -B build
19+
- cmake %CMAKE_CLI_FLAGS% -S . -B build
1920
- cd build
2021

2122
build_script:

include/uuid.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,16 @@
1717
#include <atomic>
1818
#include <span>
1919

20-
#if defined(UUID_TIME_GENERATOR) || defined(UUID_SYSTEM_GENERATOR)
2120
#ifdef _WIN32
22-
#ifndef WIN32_LEAN_AND_MEAN
23-
#define WIN32_LEAN_AND_MEAN
24-
#endif
25-
#ifndef NOMINMAX
26-
#define NOMINMAX
27-
#endif
2821

2922
#ifdef UUID_SYSTEM_GENERATOR
3023
#include <objbase.h>
3124
#endif
3225

33-
#include <windows.h>
34-
#include <intrin.h>
26+
#ifdef UUID_TIME_GENERATOR
3527
#include <iphlpapi.h>
3628
#pragma comment(lib, "IPHLPAPI.lib")
29+
#endif
3730

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

@@ -47,7 +40,6 @@
4740
#include <CoreFoundation/CFUUID.h>
4841
#endif
4942

50-
#endif
5143
#endif
5244

5345
namespace uuids
@@ -278,10 +270,6 @@ namespace uuids
278270
size_t m_blockByteIndex;
279271
size_t m_byteCount;
280272
};
281-
282-
static std::mt19937 clock_gen(std::random_device{}());
283-
static std::uniform_int_distribution<short> clock_dis{ -32768, 32767 };
284-
static std::atomic_short clock_sequence = clock_dis(clock_gen);
285273
}
286274

287275
// --------------------------------------------------------------------------------------------------------------------------
@@ -847,11 +835,15 @@ namespace uuids
847835
return ns / 100;
848836
}
849837

850-
public:
851-
uuid_time_generator()
838+
static unsigned short get_clock_sequence()
852839
{
840+
static std::mt19937 clock_gen(std::random_device{}());
841+
static std::uniform_int_distribution<unsigned short> clock_dis;
842+
static std::atomic_ushort clock_sequence = clock_dis(clock_gen);
843+
return clock_sequence++;
853844
}
854845

846+
public:
855847
uuid operator()()
856848
{
857849
if (get_mac_address())
@@ -860,25 +852,22 @@ namespace uuids
860852

861853
auto tm = get_time_intervals();
862854

863-
short clock_seq = detail::clock_sequence++;
864-
865-
clock_seq &= 0x3FFF;
855+
auto clock_seq = get_clock_sequence();
866856

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

870859
memcpy(&data[0], ptm + 4, 4);
871860
memcpy(&data[4], ptm + 2, 2);
872861
memcpy(&data[6], ptm, 2);
873862

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

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

880869
// version must be 0b0001xxxx
881-
data[6] &= 0x5F;
870+
data[6] &= 0x1F;
882871
data[6] |= 0x10;
883872

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

0 commit comments

Comments
 (0)