aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/guid.h
blob: 2bf6c8ad99c2c9727d4445ed253fac35ce748abc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once

#include "fwd.h"

#include <util/str_stl.h>

/**
 * UUID generation
 *
 * NOTE: It is not a real GUID (RFC 4122), as described in
 * https://en.wikipedia.org/wiki/Universally_unique_identifier
 * https://en.wikipedia.org/wiki/Globally_unique_identifier
 *
 * See https://clubs.at.yandex-team.ru/stackoverflow/10238/10240
 * and https://st.yandex-team.ru/IGNIETFERRO-768 for details.
 */
struct TGUID {
    ui32 dw[4] = {};

    constexpr bool IsEmpty() const noexcept {
        return (dw[0] | dw[1] | dw[2] | dw[3]) == 0;
    }

    constexpr explicit operator bool() const noexcept {
        return !IsEmpty();
    }

    // xxxx-xxxx-xxxx-xxxx
    TString AsGuidString() const;

    /**
    * RFC4122 GUID, which described in
    * https://en.wikipedia.org/wiki/Universally_unique_identifier
    * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    **/
    TString AsUuidString() const;

    static TGUID Create();

    /**
    * Generate time based UUID version 1 RFC4122 GUID
    * https://datatracker.ietf.org/doc/html/rfc4122#section-4.1
    **/
    static TGUID CreateTimebased();
};

constexpr bool operator==(const TGUID& a, const TGUID& b) noexcept {
    return a.dw[0] == b.dw[0] && a.dw[1] == b.dw[1] && a.dw[2] == b.dw[2] && a.dw[3] == b.dw[3];
}

constexpr bool operator!=(const TGUID& a, const TGUID& b) noexcept {
    return !(a == b);
}

struct TGUIDHash {
    constexpr int operator()(const TGUID& a) const noexcept {
        return a.dw[0] + a.dw[1] + a.dw[2] + a.dw[3];
    }
};

template <>
struct THash<TGUID> {
    constexpr size_t operator()(const TGUID& g) const noexcept {
        return (unsigned int)TGUIDHash()(g);
    }
};

void CreateGuid(TGUID* res);
TString GetGuidAsString(const TGUID& g);
TString CreateGuidAsString();
TGUID GetGuid(TStringBuf s);
bool GetGuid(TStringBuf s, TGUID& result);

/**
* Functions for correct parsing RFC4122 GUID, which described in
* https://en.wikipedia.org/wiki/Universally_unique_identifier
**/
TGUID GetUuid(TStringBuf s);
bool GetUuid(TStringBuf s, TGUID& result);