### Hash UUIDs Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Example of hashing a UUID string. ```cpp using namespace std::string_literals; auto str = "47183823-2574-4bfd-b411-99ed177d3e43"s; ``` -------------------------------- ### Generating New UUIDs Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Provides examples for generating various versions of UUIDs using different generator classes. ```APIDOC ## Generating new uuids Several function objects, called generators, are provided in order to create different versions of UUIDs. Examples for generating new UUIDs with the `basic_uuid_random_generator` class: ```cpp { std::random_device rd; auto seed_data = std::array {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 engine(seq); basic_uuid_random_generator dgen{engine}; auto id1 = dgen(); assert(!id1.is_nil()); assert(id1.size() == 16); assert(id1.version() == uuid_version::random_number_based); assert(id1.variant() == uuid_variant::rfc); } { std::random_device rd; auto seed_data = std::array {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::ranlux48_base engine(seq); basic_uuid_random_generator dgen{engine}; auto id1 = dgen(); assert(!id1.is_nil()); assert(id1.size() == 16); assert(id1.version() == uuid_version::random_number_based); assert(id1.variant() == uuid_variant::rfc); } { std::random_device rd; auto seed_data = std::array {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); auto engine = std::make_unique(seq); basic_uuid_random_generator dgen(engine.get()); auto id1 = dgen(); assert(!id1.is_nil()); assert(id1.size() == 16); assert(id1.version() == uuid_version::random_number_based); assert(id1.variant() == uuid_variant::rfc); } ``` Examples for generating new UUIDs with the `uuid_random_generator` type alias: ```cpp std::random_device rd; auto seed_data = std::array {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 engine(seq); uuid const guid = uuid_random_generator{engine}(); auto id1 = dgen(); assert(!id1.is_nil()); assert(id1.size() == 16); assert(id1.version() == uuid_version::random_number_based); assert(id1.variant() == uuid_variant::rfc); ``` Examples for genearting new UUIDs with the `uuid_name_generator` class: ```cpp uuid_name_generator dgen(uuid::from_string("415ccc2b-f5cf-4ec1-b544-45132a518cc8").value()); auto id1 = dgen("john"); assert(!id1.is_nil()); assert(id1.size() == 16); assert(id1.version() == uuid_version::name_based_sha1); assert(id1.variant() == uuid_variant::rfc); auto id2 = dgen("jane"); assert(!id2.is_nil()); assert(id2.size() == 16); assert(id2.version() == uuid_version::name_based_sha1); assert(id2.variant() == uuid_variant::rfc); auto id3 = dgen("jane"); assert(!id3.is_nil()); assert(id3.size() == 16); assert(id3.version() == uuid_version::name_based_sha1); assert(id3.variant() == uuid_variant::rfc); auto id4 = dgen(L"jane"); assert(!id4.is_nil()); assert(id4.size() == 16); assert(id4.version() == uuid_version::name_based_sha1); assert(id4.variant() == uuid_variant::rfc); assert(id1 != id2); assert(id2 == id3); assert(id3 != id4); ``` ``` -------------------------------- ### Generate UUIDs with Alias Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Example usage of the uuid_random_generator type alias for generating random UUIDs. ```cpp std::random_device rd; auto seed_data = std::array {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 engine(seq); uuid const guid = uuid_random_generator{engine}(); auto id1 = dgen(); assert(!id1.is_nil()); assert(id1.size() == 16); assert(id1.version() == uuid_version::random_number_based); assert(id1.variant() == uuid_variant::rfc); ``` -------------------------------- ### Get UUID Variant and Version Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Determine the variant and version of a UUID using `variant()` and `version()` member functions. These return strongly typed enums. ```cpp uuid id = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(); assert(id.version() == uuid_version::random_number_based); assert(id.variant() == uuid_variant::rfc); ``` -------------------------------- ### Create Build Directory Source: https://github.com/mariusbancila/stduuid/blob/master/how_to_build.md Creates and navigates into a build directory. This is a common first step for CMake projects. ```bash mkdir build cd build ``` -------------------------------- ### Initialize and Inspect UUIDs in C++ Source: https://context7.com/mariusbancila/stduuid/llms.txt Demonstrates creating UUIDs from various sources like byte arrays, std::array, and iterators, as well as accessing their properties. ```cpp #include "uuid.h" #include #include using namespace uuids; int main() { // Create a nil (empty) UUID - default constructor uuid empty; assert(empty.is_nil()); assert(empty.variant() == uuid_variant::ncs); assert(empty.version() == uuid_version::none); // Create UUID from byte array using initializer list uuid id{{ 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 }}; assert(!id.is_nil()); // Create UUID from std::array std::array arr{{ 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 }}; uuid id_from_array(arr); // Create UUID from iterators uuid id_from_iter(std::begin(arr), std::end(arr)); // Access underlying bytes as span auto bytes = id.as_bytes(); assert(bytes.size() == 16); // Check variant and version std::cout << "Variant: " << (id.variant() == uuid_variant::rfc ? "RFC" : "Other") << std::endl; std::cout << "Version: " << static_cast(id.version()) << std::endl; return 0; } ``` -------------------------------- ### Initial from_string Overload Attempts Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Demonstrates the initial approach using separate overloads for string literals and objects, which resulted in compiler errors due to template argument deduction limitations. ```cpp auto id1 = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43"); // [1] std::string str{ "47183823-2574-4bfd-b411-99ed177d3e43" }; auto id2 = uuid::from_string(str); // [2] ``` -------------------------------- ### Get Byte View of UUID Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Obtain a view of the underlying bytes of a UUID using the `as_bytes()` member function. This returns a `std::span`. ```cpp std::array arr{ { 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 } }; uuid const id{ arr }; assert(!id.is_nil()); auto view = id.as_bytes(); assert(memcmp(view.data(), arr.data(), arr.size()) == 0); ``` -------------------------------- ### Alternative from_string Implementations Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Shows two alternative approaches that resolve the compiler errors but are considered less desirable. ```cpp auto id1 = uuid::from_string( std::string_view {"47183823-2574-4bfd-b411-99ed177d3e43"}); // [1] std::string str{ "47183823-2574-4bfd-b411-99ed177d3e43" }; auto id2 = uuid::from_string(std::string_view {str}); // [2] ``` ```cpp auto id1 = uuid::from_string>( "47183823-2574-4bfd-b411-99ed177d3e43"); // [1] std::string str{ "47183823-2574-4bfd-b411-99ed177d3e43" }; auto id2 = uuid::from_string>(str); // [2] ``` -------------------------------- ### Convert String to UUID and Hash Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Demonstrates converting a string to a UUID object and verifying that the hash of the string matches the hash of the UUID. ```cpp uuid id = uuids::uuid::from_string(str).value(); auto h1 = std::hash{}; auto h2 = std::hash{}; assert(h1(str) == h2(id)); ``` -------------------------------- ### Generate Name-Based UUIDs (Version 5) in C++ Source: https://context7.com/mariusbancila/stduuid/llms.txt Demonstrates creating a name generator with a namespace UUID and generating deterministic UUIDs from various string types. ```cpp #include "uuid.h" #include #include using namespace uuids; using namespace std::string_literals; int main() { // Create name generator with a namespace UUID uuid namespace_id = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(); uuid_name_generator gen(namespace_id); // Generate UUIDs from names uuid id_john = gen("john"); uuid id_jane = gen("jane"); assert(!id_john.is_nil()); assert(!id_jane.is_nil()); assert(id_john != id_jane); // Different names produce different UUIDs // Verify version and variant assert(id_john.version() == uuid_version::name_based_sha1); assert(id_john.variant() == uuid_variant::rfc); // Same name always produces same UUID (deterministic) uuid id_jane2 = gen("jane"); assert(id_jane == id_jane2); // Different character encodings produce different UUIDs uuid id_jane_wide = gen(L"jane"); assert(id_jane != id_jane_wide); // Works with std::string uuid id_string = gen("john"s); assert(id_john == id_string); std::cout << "john: " << to_string(id_john) << std::endl; std::cout << "jane: " << to_string(id_jane) << std::endl; // Use predefined namespace constants for standard use cases uuid_name_generator dns_gen(uuid_namespace_dns); uuid domain_id = dns_gen("example.com"); uuid_name_generator url_gen(uuid_namespace_url); uuid url_id = url_gen("https://example.com/page"); std::cout << "DNS namespace UUID: " << to_string(domain_id) << std::endl; std::cout << "URL namespace UUID: " << to_string(url_id) << std::endl; return 0; } ``` -------------------------------- ### Use Namespace Constants for Name-Based UUIDs in C++ Source: https://context7.com/mariusbancila/stduuid/llms.txt Shows how to use predefined RFC 4122 namespace constants for DNS, URL, OID, and X.500 identifiers. ```cpp #include "uuid.h" #include using namespace uuids; int main() { // uuid_namespace_dns - For fully-qualified domain names uuid_name_generator dns_gen(uuid_namespace_dns); uuid domain = dns_gen("example.com"); std::cout << "DNS: " << to_string(domain) << std::endl; // uuid_namespace_url - For URLs uuid_name_generator url_gen(uuid_namespace_url); uuid url = url_gen("https://example.com/api/v1"); std::cout << "URL: " << to_string(url) << std::endl; // uuid_namespace_oid - For ISO OIDs uuid_name_generator oid_gen(uuid_namespace_oid); uuid oid = oid_gen("1.3.6.1.4.1"); std::cout << "OID: " << to_string(oid) << std::endl; // uuid_namespace_x500 - For X.500 Distinguished Names uuid_name_generator x500_gen(uuid_namespace_x500); uuid x500 = x500_gen("CN=John Doe,O=Example Corp,C=US"); std::cout << "X500: " << to_string(x500) << std::endl; // Verify namespace constants match RFC 4122 values assert(uuid_namespace_dns == uuid::from_string("6ba7b810-9dad-11d1-80b4-00c04fd430c8")); assert(uuid_namespace_url == uuid::from_string("6ba7b811-9dad-11d1-80b4-00c04fd430c8")); assert(uuid_namespace_oid == uuid::from_string("6ba7b812-9dad-11d1-80b4-00c04fd430c8")); assert(uuid_namespace_x500 == uuid::from_string("6ba7b814-9dad-11d1-80b4-00c04fd430c8")); return 0; } ``` -------------------------------- ### Create a nil UUID Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Initialize an empty UUID and verify its nil state. ```cpp uuid empty; assert(empty.is_nil()); ``` -------------------------------- ### UUID Comparison and Ordering with std::set and std::map Source: https://context7.com/mariusbancila/stduuid/llms.txt Demonstrates using UUIDs with ordered containers like std::set and std::map by leveraging their overloaded comparison operators. Ensure the 'uuid.h' header is included. ```cpp #include "uuid.h" #include #include #include #include using namespace uuids; int main() { uuid empty; uuid id1 = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(); uuid id2 = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(); uuid id3 = uuid::from_string("fea43102-064f-4444-adc2-02cec42623f8").value(); // Equality comparison assert(id1 == id2); assert(id1 != id3); assert(empty != id1); // Less-than comparison (for ordered containers) assert(empty < id1); // Use in std::set std::set uuid_set; uuid_set.insert(empty); uuid_set.insert(id1); uuid_set.insert(id3); assert(uuid_set.size() == 3); assert(uuid_set.find(id1) != uuid_set.end()); assert(uuid_set.find(empty) != uuid_set.end()); // Use in std::map std::map uuid_map; uuid_map[id1] = "First UUID"; uuid_map[id3] = "Second UUID"; std::cout << uuid_map[id1] << std::endl; // Output: First UUID // Generate multiple UUIDs and store in set std::random_device rd; auto seed_data = std::array{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); uuid_random_generator gen{generator}; std::set unique_ids; for (int i = 0; i < 100; ++i) { unique_ids.insert(gen()); } assert(unique_ids.size() == 100); // All UUIDs should be unique return 0; } ``` -------------------------------- ### uuid Class Usage Source: https://context7.com/mariusbancila/stduuid/llms.txt Demonstrates the instantiation of the uuid class from various sources including byte arrays, std::array, and iterators, as well as accessing metadata. ```APIDOC ## Class: uuids::uuid ### Description Represents a 128-bit universally unique identifier. Supports nil UUID detection, variant/version identification, and byte-level access. ### Methods - **is_nil()** (bool) - Returns true if the UUID is a nil UUID. - **variant()** (uuid_variant) - Returns the UUID variant (e.g., ncs, rfc). - **version()** (uuid_version) - Returns the UUID version (e.g., none, random, name-based). - **as_bytes()** (span) - Returns the underlying 16 bytes of the UUID. ### Example ```cpp uuid empty; // Nil UUID uuid id{{0x47, 0x18, ...}}; // From byte array ``` ``` -------------------------------- ### Generate Visual Studio Project Files with CMake Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Commands to generate Visual Studio 2019 project files using CMake. ```bash cd build cmake -G "Visual Studio 17" -A x64 .. ``` ```bash cd build cmake -G "Visual Studio 17" -A x64 -DUUID_SYSTEM_GENERATOR=ON .. ``` ```bash cd build cmake -G "Visual Studio 17" -A x64 -DUUID_TIME_GENERATOR=ON .. ``` -------------------------------- ### Create a UUID from a sequence of 16 bytes Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Initialize a UUID using an array or sequence of bytes. ```cpp std::array arr{{ 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43}}; uuid id(arr); assert(uuids::to_string(id) == "47183823-2574-4bfd-b411-99ed177d3e43"); // or uuids::uuid::value_type arr[16] = { 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 }; uuid id(std::begin(arr), std::end(arr)); assert(uuids::to_string(id) == "47183823-2574-4bfd-b411-99ed177d3e43"); // or uuids::uuid id{{ 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43}}; assert(uuids::to_string(id) == "47183823-2574-4bfd-b411-99ed177d3e43"); ``` -------------------------------- ### Hashing Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Shows how to use UUIDs in `std::unordered_set` by utilizing the provided `std::hash<>` specialization. ```APIDOC ## Hashing A `std::hash<>` specialization for `uuid` is provided in order to enable the use of `uuid`s in associative unordered containers such as `std::unordered_set`. ```cpp std::unordered_set ids{ uuid{}, uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(), }; assert(ids.size() == 2); assert(ids.find(uuid{}) != ids.end()); ``` ``` -------------------------------- ### Generate Xcode Project (Mac) Source: https://github.com/mariusbancila/stduuid/blob/master/how_to_build.md Generates an Xcode project for the x86 platform using CMake. This command is run from the build directory. ```bash cmake -G Xcode .. ``` -------------------------------- ### UUID Swapping with Member and Non-Member Functions Source: https://context7.com/mariusbancila/stduuid/llms.txt Demonstrates efficient UUID value exchange using both the non-member std::swap and the member swap functions. Requires 'uuid.h'. ```cpp #include "uuid.h" #include #include using namespace uuids; int main() { std::random_device rd; auto seed_data = std::array{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); uuid empty; uuid id = uuid_random_generator{generator}(); assert(empty.is_nil()); assert(!id.is_nil()); std::cout << "Before swap:" << std::endl; std::cout << "empty: " << to_string(empty) << std::endl; std::cout << "id: " << to_string(id) << std::endl; // Non-member swap (ADL-friendly) std::swap(empty, id); assert(!empty.is_nil()); assert(id.is_nil()); std::cout << "After std::swap:" << std::endl; std::cout << "empty: " << to_string(empty) << std::endl; std::cout << "id: " << to_string(id) << std::endl; // Member swap empty.swap(id); assert(empty.is_nil()); assert(!id.is_nil()); return 0; } ``` -------------------------------- ### String Conversion Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Demonstrates converting UUID objects to their canonical string representation and vice versa. ```APIDOC ## String Conversion Non-member template function `to_string()` returns a string with the UUID formatted to the canonical textual representation `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`, where `x` is a lower case hexadecimal digit. ```cpp auto id = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43"); assert(id.has_value()); assert(to_string(id.value()) == "47183823-2574-4bfd-b411-99ed177d3e43"); assert(to_string(id.value()) == L"47183823-2574-4bfd-b411-99ed177d3e43"); ``` The order of the internal UUID bytes reflects directly into the string bytes order. That is, for a UUID with the internal bytes in the form `aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp` the resulted string has the form `"aabbccdd-eeff-gghh-iijj-kkllmmnnoopp"`. ``` -------------------------------- ### Create UUID from Byte Array Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Initialize a UUID using a C-style array of bytes. The array must contain 16 elements of type `uuid::value_type`. ```cpp uuid::value_type arr[16] = { 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 }; uuid id(arr); assert(to_string(id) == "47183823-2574-4bfd-b411-99ed177d3e43"); ``` -------------------------------- ### Compare UUIDs Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Perform equality and inequality checks between UUID instances. ```cpp uuid empty; uuid id = uuids::uuid_system_generator{}(); assert(empty == empty); assert(id == id); assert(empty != id); ``` -------------------------------- ### Generate Visual Studio Projects (Windows) Source: https://github.com/mariusbancila/stduuid/blob/master/how_to_build.md Generates Visual Studio 2017 projects for the x86 platform using CMake. Specify different generators for x64 or ARM targets. ```bash cmake -G "Visual Studio 15 2017" .. ``` -------------------------------- ### Configure Test Executable in CMake Source: https://github.com/mariusbancila/stduuid/blob/master/test/CMakeLists.txt Defines the test executable, sets include directories, links the project library, and configures C++ standards and compiler flags based on the platform. ```cmake add_executable(test_${PROJECT_NAME} main.cpp test_generators.cpp test_uuid.cpp) target_include_directories(test_${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/catch) target_link_libraries(test_${PROJECT_NAME} PRIVATE ${PROJECT_NAME}) if (UUID_USING_CXX20_SPAN) set_target_properties(test_${PROJECT_NAME} PROPERTIES CXX_STANDARD 20) else () set_target_properties(test_${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) endif () if (WIN32) target_compile_options(test_${PROJECT_NAME} PRIVATE /EHc /Zc:hiddenFriend) target_compile_definitions(test_${PROJECT_NAME} PRIVATE _SCL_SECURE_NO_WARNINGS) elseif (APPLE) target_compile_options(test_${PROJECT_NAME} PRIVATE -fexceptions -g -Wall) else () target_compile_options(test_${PROJECT_NAME} PRIVATE -fexceptions -g -Wall) endif () get_target_property(CURRENT_COMPILE_OPTIONS test_${PROJECT_NAME} COMPILE_OPTIONS) message(STATUS "** ${CMAKE_CXX_COMPILER_ID} flags: ${CURRENT_COMPILE_OPTIONS}") ``` -------------------------------- ### Create UUID from Initializer List Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Construct a UUID directly from an initializer list of bytes. Ensure the list contains exactly 16 bytes. ```cpp uuid id{ {0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 } }; assert(to_string(id) == "47183823-2574-4bfd-b411-99ed177d3e43"); ``` -------------------------------- ### Create a UUID from a string Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Parse a UUID from narrow or wide character strings. ```cpp auto str = "47183823-2574-4bfd-b411-99ed177d3e43"s; auto id = uuids::uuid::from_string(str); assert(id.has_value()); assert(uuids::to_string(id.value()) == str); // or auto str = L"47183823-2574-4bfd-b411-99ed177d3e43"s; uuid id = uuids::uuid::from_string(str).value(); assert(uuids::to_string(id) == str); ``` -------------------------------- ### Convert to string Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Convert a UUID to its string representation. ```cpp uuid empty; assert(uuids::to_string(empty) == "00000000-0000-0000-0000-000000000000"); assert(uuids::to_string(empty) == L"00000000-0000-0000-0000-000000000000"); ``` -------------------------------- ### Create a new UUID Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Generate a random UUID using the system generator. ```cpp uuid const id = uuids::uuid_system_generator{}(); assert(!id.is_nil()); assert(id.version() == uuids::uuid_version::random_number_based); assert(id.variant() == uuids::uuid_variant::rfc); ``` -------------------------------- ### Technical Specifications Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Details the header file and enum definitions for the UUID library. ```APIDOC ## V. Technical Specifications ### Header Add a new header called ``. ### `uuid_variant` enum ```cpp namespace std { enum class uuid_variant { ncs, rfc, microsoft, future }; } ``` **Footnote**: Microsoft is a registered trademark of Microsoft Corporation. This information is given for the convenience of users of this document and does not constitute an endorsement by ISO or IEC of these products. ``` -------------------------------- ### Generate Time-Based UUIDs Source: https://context7.com/mariusbancila/stduuid/llms.txt Provides experimental version 1 UUID generation when UUID_TIME_GENERATOR is defined. Requires linking IPHLPAPI.lib on Windows and is not recommended for production. ```cpp // Compile with: -DUUID_TIME_GENERATOR // Windows also requires linking IPHLPAPI.lib #include "uuid.h" #include #include #ifdef UUID_TIME_GENERATOR using namespace uuids; int main() { // WARNING: Experimental feature - not for production use uuid_time_generator gen; uuid id1 = gen(); uuid id2 = gen(); if (!id1.is_nil()) { assert(id1.variant() == uuid_variant::rfc); assert(id1.version() == uuid_version::time_based); assert(id1 != id2); std::cout << "Time UUID 1: " << to_string(id1) << std::endl; std::cout << "Time UUID 2: " << to_string(id2) << std::endl; // Generate multiple time-based UUIDs std::set time_ids; for (int i = 0; i < 100; ++i) { time_ids.insert(gen()); } assert(time_ids.size() == 100); } else { std::cout << "Time-based generation failed (MAC address unavailable)" << std::endl; } return 0; } #else int main() { std::cout << "Time generator not enabled. Compile with -DUUID_TIME_GENERATOR" << std::endl; return 0; } #endif ``` -------------------------------- ### Define namespace constants Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Implementation-specific constants for generating name-based UUIDs. ```cpp namespace std { inline constexpr uuid uuid_namespace_dns = /* implementation-specific */; inline constexpr uuid uuid_namespace_url = /* implementation-specific */; inline constexpr uuid uuid_namespace_oid = /* implementation-specific */; inline constexpr uuid uuid_namespace_x500 = /* implementation-specific */; } ``` -------------------------------- ### Generate Random UUIDs (Version 4) Source: https://context7.com/mariusbancila/stduuid/llms.txt Creates random UUIDs using configurable random number engines. Requires seeding for production environments. ```cpp #include "uuid.h" #include #include #include using namespace uuids; int main() { // Properly seed the random generator for production use std::random_device rd; auto seed_data = std::array{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); // Create generator with reference to engine uuid_random_generator gen{generator}; // Generate UUIDs uuid id1 = gen(); uuid id2 = gen(); assert(!id1.is_nil()); assert(!id2.is_nil()); assert(id1 != id2); // UUIDs should be unique // Verify version and variant assert(id1.version() == uuid_version::random_number_based); assert(id1.variant() == uuid_variant::rfc); std::cout << "Random UUID 1: " << to_string(id1) << std::endl; std::cout << "Random UUID 2: " << to_string(id2) << std::endl; // Alternative: pass pointer to engine uuid_random_generator gen_ptr(&generator); uuid id3 = gen_ptr(); // Using custom random engine (ranlux48_base) std::array seed_data2{}; std::generate(std::begin(seed_data2), std::end(seed_data2), std::ref(rd)); std::seed_seq seq2(std::begin(seed_data2), std::end(seed_data2)); std::ranlux48_base custom_engine(seq2); basic_uuid_random_generator custom_gen{custom_engine}; uuid custom_id = custom_gen(); assert(custom_id.version() == uuid_version::random_number_based); return 0; } ``` -------------------------------- ### Create a new UUID with the name generator Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Generate a name-based UUID using a namespace UUID. ```cpp uuids::uuid_name_generator gen(uuids::uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value()); uuid const id = gen("john"); assert(!id.is_nil()); assert(id.version() == uuids::uuid_version::name_based_sha1); assert(id.variant() == uuids::uuid_variant::rfc); ``` -------------------------------- ### Generate UUIDs with System Generator Source: https://context7.com/mariusbancila/stduuid/llms.txt Utilizes OS-native UUID generation (CoCreateGuid, uuid_generate, or CFUUIDCreate) when the UUID_SYSTEM_GENERATOR macro is defined. ```cpp // Compile with: -DUUID_SYSTEM_GENERATOR // Linux also requires: -luuid #include "uuid.h" #include #ifdef UUID_SYSTEM_GENERATOR using namespace uuids; int main() { // Create system generator (uses OS-native UUID generation) uuid_system_generator gen; // Generate UUIDs uuid id1 = gen(); uuid id2 = gen(); assert(!id1.is_nil()); assert(!id2.is_nil()); assert(id1 != id2); // System-generated UUIDs are typically version 4 (random) std::cout << "System UUID 1: " << to_string(id1) << std::endl; std::cout << "System UUID 2: " << to_string(id2) << std::endl; std::cout << "Version: " << static_cast(id1.version()) << std::endl; std::cout << "Variant: " << (id1.variant() == uuid_variant::rfc ? "RFC" : "Other") << std::endl; return 0; } #else int main() { std::cout << "System generator not enabled. Compile with -DUUID_SYSTEM_GENERATOR" << std::endl; return 0; } #endif ``` -------------------------------- ### UUID Hashing for Unordered Containers Source: https://context7.com/mariusbancila/stduuid/llms.txt Illustrates using UUIDs with unordered containers like std::unordered_set and std::unordered_map via the std::hash specialization. Requires 'uuid.h'. ```cpp #include "uuid.h" #include #include #include #include using namespace uuids; int main() { // Direct hash computation uuid id = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(); std::hash hasher; size_t hash_value = hasher(id); std::cout << "Hash: " << hash_value << std::endl; // Use in std::unordered_set std::unordered_set uuid_set; uuid_set.insert(uuid{}); // nil UUID uuid_set.insert(id); uuid_set.insert(uuid::from_string("fea43102-064f-4444-adc2-02cec42623f8").value()); assert(uuid_set.size() == 3); assert(uuid_set.find(id) != uuid_set.end()); assert(uuid_set.find(uuid{}) != uuid_set.end()); // Use in std::unordered_map std::unordered_map uuid_map; uuid_map[id] = "User session data"; uuid_map[uuid{}] = "Default entry"; std::cout << uuid_map[id] << std::endl; // Generate and store many UUIDs efficiently std::random_device rd; auto seed_data = std::array{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); uuid_random_generator gen{generator}; std::unordered_set large_set; for (int i = 0; i < 1000; ++i) { large_set.insert(gen()); } assert(large_set.size() == 1000); return 0; } ``` -------------------------------- ### Parse UUIDs from Strings in C++ Source: https://context7.com/mariusbancila/stduuid/llms.txt Shows how to parse UUIDs from various string types including C-strings, std::string, std::string_view, and wide strings, with support for compile-time parsing. ```cpp #include "uuid.h" #include #include #include using namespace uuids; using namespace std::string_literals; using namespace std::string_view_literals; int main() { // Parse from C-string literal auto id1 = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43"); if (id1.has_value()) { std::cout << "Parsed: " << to_string(id1.value()) << std::endl; } // Parse from braced format auto id2 = uuid::from_string("{47183823-2574-4bfd-b411-99ed177d3e43}"); assert(id2.has_value()); assert(id1.value() == id2.value()); // Parse from std::string std::string str = "47183823-2574-4bfd-b411-99ed177d3e43"s; auto id3 = uuid::from_string(str); assert(id3.has_value()); // Parse from std::string_view auto id4 = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43"sv); assert(id4.has_value()); // Parse from wide string auto id5 = uuid::from_string(L"47183823-2574-4bfd-b411-99ed177d3e43"); assert(id5.has_value()); // Parse nil UUID auto nil_id = uuid::from_string("00000000-0000-0000-0000-000000000000"); assert(nil_id.has_value() && nil_id.value().is_nil()); // Handle invalid input gracefully auto invalid = uuid::from_string("not-a-valid-uuid"); assert(!invalid.has_value()); auto too_short = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e4"); assert(!too_short.has_value()); // Constexpr parsing (compile-time) constexpr uuid compile_time = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value(); static_assert(!compile_time.is_nil()); return 0; } ``` -------------------------------- ### Define non-member functions Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Utility functions for swapping, streaming, and string conversion of UUIDs. ```cpp namespace std { inline constexpr void swap(uuid & lhs, uuid & rhs) noexcept; template std::basic_ostream & operator<<(std::basic_ostream &s, uuid const & id); template, class Allocator = std::allocator> inline std::basic_string to_string(uuid const & id); } ``` -------------------------------- ### Create a new UUID with a default random generator Source: https://github.com/mariusbancila/stduuid/blob/master/README.md Generate a UUID using a custom random engine seeded with std::random_device. ```cpp std::random_device rd; auto seed_data = std::array {}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 generator(seq); uuids::uuid_random_generator gen{generator}; uuid const id = gen(); assert(!id.is_nil()); assert(id.as_bytes().size() == 16); assert(id.version() == uuids::uuid_version::random_number_based); assert(id.variant() == uuids::uuid_variant::rfc); ``` -------------------------------- ### Configure stduuid CMake build Source: https://github.com/mariusbancila/stduuid/blob/master/CMakeLists.txt The primary CMake configuration file for the stduuid project, defining build options and library targets. ```cmake cmake_minimum_required(VERSION 3.7.0) set(UUID_MAIN_PROJECT OFF) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(UUID_MAIN_PROJECT ON) endif() project(stduuid CXX) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") option(UUID_BUILD_TESTS "Build the unit tests" ${UUID_MAIN_PROJECT}) 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) option(UUID_ENABLE_INSTALL "Create an install target" ${UUID_MAIN_PROJECT}) # Library target add_library(${PROJECT_NAME} INTERFACE) target_include_directories(${PROJECT_NAME} INTERFACE $ $) # Using system uuid generator if (UUID_SYSTEM_GENERATOR) target_compile_definitions(${PROJECT_NAME} INTERFACE UUID_SYSTEM_GENERATOR) if (WIN32) elseif (APPLE) find_library(CFLIB CoreFoundation REQUIRED) target_link_libraries(${PROJECT_NAME} INTERFACE ${CFLIB}) else () find_package(Libuuid REQUIRED) if (Libuuid_FOUND) target_include_directories(${PROJECT_NAME} INTERFACE ${Libuuid_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME} INTERFACE ${Libuuid_LIBRARIES}) endif () 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 $ $) install(DIRECTORY gsl DESTINATION include) endif () if(UUID_ENABLE_INSTALL) # Install step and imported target install(FILES include/uuid.h DESTINATION include) install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets) install(EXPORT ${PROJECT_NAME}-targets DESTINATION lib/cmake/${PROJECT_NAME}) # Config files for find_package() include(CMakePackageConfigHelpers) configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-version.cmake" VERSION "1.0" COMPATIBILITY AnyNewerVersion) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-version.cmake" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLibuuid.cmake" DESTINATION lib/cmake/${PROJECT_NAME}) export(EXPORT ${PROJECT_NAME}-targets FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}-targets.cmake") endif() # Tests if (UUID_BUILD_TESTS) enable_testing() add_subdirectory(test) endif () ``` -------------------------------- ### Using uuid_fast_order with std::map Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md Demonstrates how to use the uuid_fast_order functor as a comparator for std::map when using std::uuid as a key. ```cpp std::map map; ``` -------------------------------- ### uuid::from_string Parsing Source: https://context7.com/mariusbancila/stduuid/llms.txt Documentation for the static method used to parse UUIDs from various string formats including narrow, wide, and string_view types. ```APIDOC ## Static Method: uuid::from_string ### Description Parses a UUID from a string representation. Supports standard UUID format, braced format, and various string types. ### Parameters - **input** (string/string_view/wstring) - Required - The string representation of the UUID. ### Returns - **std::optional** - Returns a populated optional if parsing is successful, otherwise returns std::nullopt. ### Example ```cpp auto id = uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43"); if (id.has_value()) { /* use id.value() */ } ``` ``` -------------------------------- ### Simplified from_string Template Definition Source: https://github.com/mariusbancila/stduuid/blob/master/P0959.md The final, simplified template definition used for from_string, is_valid_uuid, and uuid_name_generator. ```cpp template constexpr static std::optional from_string(StringType const & str); ```