Line data Source code
1 : /**
2 : Copyright (c) 2019-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_CORE_MEMORY_SPMEMUUID_H_
25 : #define STAPPLER_CORE_MEMORY_SPMEMUUID_H_
26 :
27 : #include "SPMemString.h"
28 : #include "SPMemVector.h"
29 : #include "SPBytesView.h"
30 : #include "SPStringView.h"
31 :
32 : namespace STAPPLER_VERSIONIZED stappler::memory {
33 :
34 : struct uuid : AllocPool {
35 : static constexpr size_t FormattedLength = 36;
36 :
37 : using uuid_t = std::array<uint8_t, 16>;
38 :
39 : static bool parse(uuid_t &, const char *);
40 : static void format(char *, const uuid_t &);
41 : static uuid generate();
42 :
43 2975 : uuid() {
44 2975 : memset(_uuid.data(), 0, 16);
45 2975 : }
46 :
47 0 : uuid(StringView str) {
48 0 : parse(_uuid, str.data());
49 0 : }
50 :
51 150 : uuid(BytesView b) {
52 150 : if (b.size() == 16) {
53 150 : memcpy(_uuid.data(), b.data(), 16);
54 : }
55 150 : }
56 :
57 25 : uuid(const uuid_t &u) : _uuid(u) { }
58 : uuid(const uuid &u) : _uuid(u._uuid) { }
59 :
60 175 : uuid &operator= (const uuid &u) { _uuid = u._uuid; return *this; }
61 :
62 : uuid &operator= (const memory::string &str) {
63 : parse(_uuid, str.data());
64 : return *this;
65 : }
66 :
67 : uuid &operator= (const std::string &str) {
68 : parse(_uuid, str.data());
69 : return *this;
70 : }
71 :
72 : uuid &operator= (const memory::vector<uint8_t> &b) {
73 : if (b.size() == 16) {
74 : memcpy(_uuid.data(), b.data(), 16);
75 : }
76 : return *this;
77 : }
78 :
79 : uuid &operator= (const std::vector<uint8_t> &b) {
80 : if (b.size() == 16) {
81 : memcpy(_uuid.data(), b.data(), 16);
82 : }
83 : return *this;
84 : }
85 :
86 : template <typename Str = string>
87 0 : auto str() const -> Str {
88 0 : char buf[FormattedLength] = { 0 };
89 0 : format(buf, _uuid);
90 0 : return Str(buf, FormattedLength);
91 : }
92 :
93 : template <typename B = memory::vector<uint8_t>>
94 25 : auto bytes() const -> B {
95 25 : return B(_uuid.data(), _uuid.data() + 16);
96 : }
97 :
98 : uuid_t array() const {
99 : return _uuid;
100 : }
101 :
102 350 : BytesView view() const {
103 350 : return BytesView(_uuid);
104 : }
105 :
106 : const uint8_t *data() const { return _uuid.data(); }
107 : size_t size() const { return 16; }
108 :
109 : uuid_t _uuid;
110 : };
111 :
112 : }
113 :
114 : #endif /* STAPPLER_CORE_MEMORY_SPMEMUUID_H_ */
|