All Projects → mariusbancila → Stduuid

mariusbancila / Stduuid

Licence: mit
A C++17 cross-platform implementation for UUIDs

Programming Languages

cpp
1120 projects
cpp17
186 projects
proposal
26 projects

Labels

Projects that are alternatives of or similar to Stduuid

Vmime
VMime Mail Library
Stars: ✭ 218 (-0.91%)
Mutual labels:  library
Nusoap
😏 Fixed NuSOAP for PHP 5.6 - 8.0
Stars: ✭ 224 (+1.82%)
Mutual labels:  library
Pubg mobile memory hacking examples
Pubg Mobile Emulator Gameloop Memory Hacking C++ code examples. Ex: Name, Coord, Bones, Weapons, Items, Box, Drop etc.
Stars: ✭ 224 (+1.82%)
Mutual labels:  library
Midir
Cross-platform realtime MIDI processing in Rust.
Stars: ✭ 221 (+0.45%)
Mutual labels:  library
Swift Essentials
A set of essential Swift stuff I use in every single iOS app.
Stars: ✭ 223 (+1.36%)
Mutual labels:  library
Simplenetwork
simple TCP server / client C++ linux socket
Stars: ✭ 225 (+2.27%)
Mutual labels:  library
Animatedrecyclerview
RecyclerView with layout animations
Stars: ✭ 220 (+0%)
Mutual labels:  library
Slideimageview
Simple and convenient library that allows you to slide images through a view.
Stars: ✭ 227 (+3.18%)
Mutual labels:  library
Androidappshortcuts
App Shortcuts for Android on Pre Nougat 7.1!
Stars: ✭ 223 (+1.36%)
Mutual labels:  library
Velocity Engine
Mirror of Apache Velocity Engine
Stars: ✭ 222 (+0.91%)
Mutual labels:  library
C Algorithms
A library of common data structures and algorithms written in C.
Stars: ✭ 2,654 (+1106.36%)
Mutual labels:  library
Lfortran
Official mirror of https://gitlab.com/lfortran/lfortran. Please submit pull requests (PR) there. Any PR sent here will be closed automatically.
Stars: ✭ 220 (+0%)
Mutual labels:  library
Php Validate
Lightweight and feature-rich PHP validation and filtering library. Support scene grouping, pre-filtering, array checking, custom validators, custom messages. 轻量且功能丰富的PHP验证、过滤库。支持场景分组,前置过滤,数组检查,自定义验证器,自定义消息。
Stars: ✭ 225 (+2.27%)
Mutual labels:  library
Material Backdrop
A simple solution for implementing Backdrop pattern for Android
Stars: ✭ 221 (+0.45%)
Mutual labels:  library
Permissionsswiftui
A SwiftUI package to beautifully display and handle permissions.
Stars: ✭ 220 (+0%)
Mutual labels:  library
Sslyze
Fast and powerful SSL/TLS scanning library.
Stars: ✭ 2,623 (+1092.27%)
Mutual labels:  library
Newnode
NewNode decentralized Content Distribution Network
Stars: ✭ 223 (+1.36%)
Mutual labels:  library
Stdlib
✨ Standard library for JavaScript and Node.js. ✨
Stars: ✭ 2,749 (+1149.55%)
Mutual labels:  library
Torchdata
PyTorch dataset extended with map, cache etc. (tensorflow.data like)
Stars: ✭ 226 (+2.73%)
Mutual labels:  library
Libmypaint
libmypaint, a.k.a. "brushlib", is a library for making brushstrokes which is used by MyPaint and other projects.
Stars: ✭ 225 (+2.27%)
Mutual labels:  library

stduuid

A C++17 cross-platform single-header library implementation for universally unique identifiers, simply know as either UUID or GUID (mostly on Windows). A UUID is a 128-bit number used to uniquely identify information in computer systems, such as database table keys, COM interfaces, classes and type libraries, and many others.

Build Status Tests status

For information about UUID/GUIDs see:

Library overview

Although the specification puts the uuid library in the std namespace, this implementation uses the namespace uuids for this purpose, in order to make the library usable without violating the restrictions imposed on the std namespace. The following types and utilities are available:

Basic types:

Name Description
uuid a class representing a UUID; this can be default constructed (a nil UUID), constructed from a range (defined by a pair of iterators), or from a span.
uuid_variant a strongly type enum representing the type of a UUID
uuid_version a strongly type enum representing the version of a UUID

Generators:

Name Description
basic_uuid_random_generator a function object that generates version 4 UUIDs using a pseudo-random number generator engine.
uuid_random_generator a basic_uuid_random_generator using the Marsenne Twister engine (basic_uuid_random_generator<std::mt19937>)
uuid_name_generator a function object that generates version 5, name-based UUIDs using SHA1 hashing.
uuid_system_generator a function object that generates new UUIDs using operating system resources (CoCreateGuid on Windows, uuid_generate on Linux, CFUUIDCreate on Mac)

Note: This is not part of the standard proposal. It is available only if the UUID_SYSTEM_GENERATOR macro is defined.

Utilities:

Name Description
std::swap<> specialization of swap for uuid
std::hash<> specialization of hash for uuid (necessary for storing UUIDs in unordered associative containers, such as std::unordered_set)

Constants:

Name Description
uuid_namespace_dns Namespace ID for name-based uuids when name string is a fully-qualified domain name.
uuid_namespace_url Namespace ID for name-based uuids when name string is a URL.
uuid_namespace_oid Namespace ID for name-based uuids when mame string is an ISO OID (See https://oidref.com/, https://en.wikipedia.org/wiki/Object_identifier).
uuid_namespace_x500 Namespace ID for name-based uuids when name string is an X.500 DN, in DER or a text output format (See https://en.wikipedia.org/wiki/X.500, https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One).

Other:

Name Description
operator== and operator!= for UUIDs comparison for equality/inequality
operator< for comparing whether one UUIDs is less than another. Although this operation does not make much logical sense, it is necessary in order to store UUIDs in a std::set.
operator<< to write a UUID to an output stream using the canonical textual representation.
to_string() creates a string with the canonical textual representation of a UUID.

Library history

This library is an implementation of the proposal P0959.

As the proposal evolves based on the standard committee and the C++ community feedback, this library implementation will reflect those changes.

See the revision history of the proposal for history of changes.

Using the library

The following is a list of examples for using the library:

  • Creating a nil UUID

    uuid empty;
    assert(empty.is_nil());
    
  • Creating a new UUID

    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);
    
  • Creating a new UUID with a default random generator

    std::random_device rd;
    auto seed_data = std::array<int, std::mt19937::state_size> {};
    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);
    
  • Creating a new UUID with a particular random generator

    std::random_device rd;
    auto seed_data = std::array<int, 6> {};
    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 generator(seq);
    
    uuids::basic_uuid_random_generator<std::ranlux48_base> 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);
    
  • Creating a new UUID with the name generator

    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);
    
  • Create a UUID from a string

    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<wchar_t>(id) == str);
    
  • Creating a UUID from a sequence of 16 bytes

    std::array<uuids::uuid::value_type, 16> 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");
    
  • Comparing UUIDs

    uuid empty;
    uuid id = uuids::uuid_system_generator{}();
    assert(empty == empty);
    assert(id == id);
    assert(empty != id);
    
  • Swapping UUIDs

    uuid empty;
    uuid id = uuids::uuid_system_generator{}();
    
    assert(empty.is_nil());
    assert(!id.is_nil());
    
    std::swap(empty, id);
    
    assert(!empty.is_nil());
    assert(id.is_nil());
    
    empty.swap(id);
    
    assert(empty.is_nil());
    assert(!id.is_nil());
    
  • Converting to string

    uuid empty;
    assert(uuids::to_string(empty) == "00000000-0000-0000-0000-000000000000");
    assert(uuids::to_string<wchar_t>(empty) == L"00000000-0000-0000-0000-000000000000");
    
  • Using with an orderered associative container

    std::random_device rd;
    auto seed_data = std::array<int, std::mt19937::state_size> {};
    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);
    uuids::uuid_random_generator gen(&engine);
     
    std::set<uuids::uuid> ids{uuid{}, gen(), gen(), gen(), gen()};
    
    assert(ids.size() == 5);
    assert(ids.find(uuid{}) != ids.end());
    
  • Using in an unordered associative container

    std::random_device rd;
    auto seed_data = std::array<int, std::mt19937::state_size> {};
    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);
    uuids::uuid_random_generator gen(&engine);
    
    std::unordered_set<uuids::uuid> ids{uuid{}, gen(), gen(), gen(), gen()};
    
    assert(ids.size() == 5);
    assert(ids.find(uuid{}) != ids.end());
    
  • Hashing UUIDs

    using namespace std::string_literals;
    auto str = "47183823-2574-4bfd-b411-99ed177d3e43"s;
    uuid id = uuids::uuid::from_string(str).value();
    
    auto h1 = std::hash<std::string>{};
    auto h2 = std::hash<uuid>{};
    assert(h1(str) == h2(id));
    

Support

The library is supported on all major operating systems: Windows, Linux and Mac OS.

Dependencies

Because no major compiler supports std::span yet the Microsoft Guidelines Support Library (aka GSL) is used for its span implementation (from which the standard version was defined).

Testing

A testing project is available in the sources. To build and execute the tests do the following:

  • Clone or download this repository
  • Create a build directory in the root directory of the sources
  • Run the command cmake .. from the build directory; if you do not have CMake you must install it first.
  • Build the project created in the previous step
  • Run the executable.

Credits

The SHA1 implementation is based on the TinySHA1 library.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].