Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
GUID.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_GUID_h_
5#define _Stroika_Foundation_Common_GUID_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <compare>
10
11#if qStroika_Foundation_Common_Platform_Windows
12#include <guiddef.h>
13#endif
14
17#include "Stroika/Foundation/Common/Common.h"
18#include "Stroika/Foundation/Common/Concepts.h"
19
20/**
21 */
22
23namespace Stroika::Foundation::Memory {
24 class BLOB; // Forward declare to avoid mutual include issues
25}
26
28
29 /**
30 * A very common 16-byte opaque ID structure.
31 *
32 * \note Satisfies Concepts:
33 * o static_assert (sizeof (GUID) == 16);
34 * o static_assert (ranges::range<GUID>);
35 * o static_assert (regular<GUID>);
36 * o static_assert (totally_ordered<GUID>);
37 *
38 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
39 * o static_assert (totally_ordered<GUID>);
40 */
41 struct GUID {
42 private:
43 static GUID mk_ (const string& src);
44
45 public:
46 /**
47 * \note - when converting from a string, GUID allows the leading/trailing {} to be optionally provided.
48 * - format's supported {61e4d49d-8c26-3480-f5c8-564e155c67a6}
49 * or 61e4d49d-8c26-3480-f5c8-564e155c67a6
50 * no argument CTOR, creates an all-zero GUID.
51 *
52 * \see GUID::GenerateNew () to create a new random GUID.
53 *
54 * @todo maybe support more input formats, such as https://stackoverflow.com/questions/7775439/is-the-format-of-guid-always-the-same
55 * @todo - should allow input format of raw bytes (though unclear of endian interpretation that would be best
56 * in that case)
57 */
58 constexpr GUID () noexcept = default;
59#if qStroika_Foundation_Common_Platform_Windows
60 constexpr GUID (const ::GUID& src) noexcept;
61#endif
62 template <Characters::IConvertibleToString STRISH_TYPE>
63 GUID (STRISH_TYPE&& src);
64 GUID (const Memory::BLOB& src);
65 GUID (const array<byte, 16>& src) noexcept;
66 GUID (const array<uint8_t, 16>& src) noexcept;
67
68 public:
69 /**
70 * Like Windows UuidCreate, or CoCreateGuid - create a random GUID (but portably).
71 *
72 * \alias CreateNew(), CTOR
73 *
74 * \par Example Usage
75 * \code
76 * String newUUIDAsString = GUID::GenerateNew ().As<String> ();
77 * \endcode
78 */
79 static GUID GenerateNew () noexcept;
80
81 public:
82 uint32_t Data1{};
83 uint16_t Data2{};
84 uint16_t Data3{};
85 uint8_t Data4[8]{};
86
87 public:
88 using value_type = byte;
89
90 public:
91 /**
92 * \nb: Stroika v2.1 allowed iterating and modifying in place of the GUID as a sequence of bytes, but no more
93 */
94 nonvirtual const byte* begin () const noexcept;
95
96 public:
97 /**
98 * \nb: Stroika v2.1 allowed iterating and modifying in place of the GUID as a sequence of bytes, but no more
99 */
100 nonvirtual const byte* end () const noexcept;
101
102 public:
103 /**
104 */
105 constexpr size_t size () const noexcept;
106
107 public:
108 /**
109 */
110 nonvirtual explicit operator Memory::BLOB () const;
111
112 public:
113 /**
114 */
115 nonvirtual strong_ordering operator<=> (const GUID&) const noexcept = default;
116
117 public:
118 /**
119 */
120 nonvirtual const uint8_t* data () const noexcept;
121
122 public:
123 /**
124 * For now, only supported formats are
125 * o String -- format: 61e4d49d-8c26-3480-f5c8-564e155c67a6
126 * o string -- same
127 * o BLOB
128 * o array<uint8_t, 16> or array<byte, 16>
129 */
130 template <IAnyOf<Characters::String, std::string, Memory::BLOB, array<byte, 16>, array<uint8_t, 16>> T>
131 nonvirtual T As () const;
132
133 public:
134 /**
135 * @see Characters::ToString ()
136 */
137 nonvirtual Characters::String ToString () const;
138 };
139 static_assert (sizeof (GUID) == 16);
140 static_assert (ranges::range<GUID>);
141 static_assert (regular<GUID>);
142 static_assert (totally_ordered<GUID>);
143
144}
145
146/*
147 * Already default-implemented in ToString() code, but this implementation is better (because by default
148 * 'range version' used) and this takes precedence.
149 */
150template <>
151struct qStroika_Foundation_Characters_FMT_PREFIX_::formatter<Stroika::Foundation::Common::GUID, wchar_t>
152 : qStroika_Foundation_Characters_FMT_PREFIX_::formatter<std::wstring, wchar_t> {
153 using inherited = qStroika_Foundation_Characters_FMT_PREFIX_::formatter<std::wstring, wchar_t>;
154 template <class FmtContext>
155 typename FmtContext::iterator format (const Stroika::Foundation::Common::GUID& s, FmtContext& ctx) const;
156};
157template <>
158struct qStroika_Foundation_Characters_FMT_PREFIX_::formatter<Stroika::Foundation::Common::GUID, char>
159 : qStroika_Foundation_Characters_FMT_PREFIX_::formatter<std::string, char> {
160 using inherited = qStroika_Foundation_Characters_FMT_PREFIX_::formatter<std::string, char>;
161 template <class FmtContext>
162 typename FmtContext::iterator format (const Stroika::Foundation::Common::GUID& s, FmtContext& ctx) const;
163};
164
166 template <typename T>
167 struct DefaultSerializer; // Forward declare to avoid mutual include issues
168 template <>
169 struct DefaultSerializer<Stroika::Foundation::Common::GUID> {
170 Memory::BLOB operator() (const Stroika::Foundation::Common::GUID& arg) const;
171 };
172}
173
174/*
175 ********************************************************************************
176 ***************************** Implementation Details ***************************
177 ********************************************************************************
178 */
179#include "GUID.inl"
180
181#endif /*_Stroika_Foundation_Common_GUID_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
STL namespace.
static GUID GenerateNew() noexcept
Definition GUID.cpp:76
constexpr GUID() noexcept=default
nonvirtual const byte * end() const noexcept
Definition GUID.inl:51
nonvirtual const byte * begin() const noexcept
Definition GUID.inl:47
nonvirtual Characters::String ToString() const
Definition GUID.cpp:64