functional-dag
Loading...
Searching...
No Matches
guid_impl.hpp
1#pragma once
12#include <expected>
13#include <iostream>
14#include <sstream>
15#include <string>
16#include <string_view>
17
18#include "fb_gen/guid_generated.h"
19#include "functional_dag/error_codes.h"
20
21namespace fn_dag {
22using namespace std;
23
31template <typename T>
32class GUID {
33 public:
35 GUID_vals m_id;
36
43 GUID(const GUID_vals &_values);
44
51 GUID(const GUID<T> &_values);
52
60 string to_uuid() const;
61
72 static expected<GUID<T>, error_codes> from_uuid(const string_view &_uuid);
73};
74
75template <typename T>
76GUID<T>::GUID(const GUID_vals &_values) {
77 m_id = _values;
78}
79
80template <typename T>
81GUID<T>::GUID(const GUID<T> &_values) {
82 m_id = _values.m_id;
83}
84
94template <typename T>
95bool operator==(const GUID<T> &_left, const GUID<T> &_right) {
96 return _left.m_id.bits1() == _right.m_id.bits1() &&
97 _left.m_id.bits2() == _right.m_id.bits2();
98}
99
109template <typename T>
110bool operator<(const GUID<T> &_left, const GUID<T> &_right) {
111 return _left.m_id.bits1() < _right.m_id.bits1() ||
112 (_left.m_id.bits1() == _right.m_id.bits1() &&
113 _left.m_id.bits2() < _right.m_id.bits2());
114}
115
116template <typename T>
117string GUID<T>::to_uuid() const {
118 stringstream uuid{};
119 uint32_t first_four_bytes = (m_id.bits1() >> 4 * 8);
120 uint16_t second_two_bytes = (m_id.bits1() << 4 * 8) >> 6 * 8;
121 uint16_t last_two_bytes = (m_id.bits1() & ((255 << 8) + 255));
122
123 uint16_t first_two_bytes = (m_id.bits2() >> 6 * 8);
124 uint64_t last_six_bytes = (m_id.bits2() << 2 * 8 >> 2 * 8);
125
126 uuid << hex << first_four_bytes << "-" << hex << second_two_bytes << "-"
127 << hex << last_two_bytes << "-" << hex << first_two_bytes << "-" << hex
128 << last_six_bytes;
129 return uuid.str();
130}
131
132template <typename T>
133expected<GUID<T>, error_codes> GUID<T>::from_uuid(const string_view &_uuid) {
134 constexpr uint64_t expect_len[5] = {8, 4, 4, 4, 12};
135 uint64_t first_four_bytes;
136 uint64_t second_two_bytes;
137 uint64_t last_two_bytes;
138 uint64_t first_two_bytes;
139 uint64_t last_six_bytes;
140 uint64_t *const dest_vals[5] = {&first_four_bytes, &second_two_bytes,
141 &last_two_bytes, &first_two_bytes,
142 &last_six_bytes};
143 int i = 0;
144 size_t pos = 0;
145 constexpr char sep = '-';
146 size_t end_pos = _uuid.find(sep, pos);
147 while (end_pos != string_view::npos || i == 4) {
148 const string_view next_hex = _uuid.substr(pos, end_pos - pos);
149 if (next_hex.size() != expect_len[i]) {
150 return unexpected(error_codes::HEX_SIZE_INCORRECT);
151 }
152
153 istringstream(string(next_hex)) >> hex >> *dest_vals[i];
154 i++;
155
156 if (i == 5) {
157 break;
158 }
159 pos = end_pos + 1;
160 end_pos = _uuid.find(sep, pos);
161 }
162 if (i != 5) {
163 return unexpected(error_codes::NOT_ENOUGH_ELEMENTS);
164 }
165 uint64_t bits1 = (first_four_bytes << 4 * 8) + (second_two_bytes << 2 * 8) +
166 last_two_bytes;
167 uint64_t bits2 = (first_two_bytes << 6 * 8) + last_six_bytes;
168 return GUID<T>(GUID_vals(bits1, bits2));
169}
170} // namespace fn_dag
(Utility) For defining a globally unique UUIDv4 identifier.
Definition guid_impl.hpp:32
static expected< GUID< T >, error_codes > from_uuid(const string_view &_uuid)
Parse a UUIDv4 representation into an internal 128bit unsigned int.
Definition guid_impl.hpp:133
GUID_vals m_id
The internal UUID representation.
Definition guid_impl.hpp:35
string to_uuid() const
Get the UUIDv4 representation of the GUID.
Definition guid_impl.hpp:117
GUID(const GUID_vals &_values)
Wrapper of the 128 bit integer.
Definition guid_impl.hpp:76
GUID(const GUID< T > &_values)
Copy constructor of the GUID.
Definition guid_impl.hpp:81
Definition guid_generated.h:16
bool operator<(const GUID< T > &_left, const GUID< T > &_right)
Less than operator.
Definition guid_impl.hpp:110
error_codes
Error codes used by the functional dag.
Definition error_codes.h:19
@ NOT_ENOUGH_ELEMENTS
Not enough UUID elements to parse.
Definition error_codes.h:24
@ HEX_SIZE_INCORRECT
A section of the UUID isn't long enough.
Definition error_codes.h:26
bool operator==(const GUID< T > &_left, const GUID< T > &_right)
Equality operator.
Definition guid_impl.hpp:95