FuncDoodle
Loading...
Searching...
No Matches
UUID.h
Go to the documentation of this file.
1
17
18#pragma once
19#include <array>
20#include <iostream>
21#include <memory.h>
22#include <ostream>
23
24namespace FuncDoodle {
31 class UUID {
32 public:
34 UUID(std::array<unsigned char, 16> bytes) { m_Bytes = bytes; }
35 UUID() { m_Bytes = std::array<unsigned char, 16>(); }
37 std::array<unsigned char, 16> Bytes() { return m_Bytes; }
39 [[nodiscard]] const char* ToString() const;
41 bool operator==(const UUID& other) const;
43 bool operator!=(const UUID& other) const;
45 static UUID Gen();
47 static UUID FromString(const char* str);
49 friend std::ostream& operator<<(std::ostream& os, const UUID& obj);
52 struct Hash {
54 size_t operator()(const UUID& uuid) const {
55 static const size_t FNV_PRIME = 1099511628211ULL;
56 static const size_t FNV_OFFSET_BASIS = 14695981039346656037ULL;
57 size_t hash = FNV_OFFSET_BASIS;
58 for (size_t i = 0; i < 16; ++i) {
59 hash ^= static_cast<size_t>(uuid.m_Bytes[i]);
60 hash *= FNV_PRIME;
61 }
62 return hash;
63 }
64 };
65
66 bool operator<(const UUID& other) const {
67 return m_Bytes < other.m_Bytes;
68 }
69
70 bool operator>(const UUID& other) const {
71 return m_Bytes > other.m_Bytes;
72 }
73
75 bool operator<=(const UUID& other) const {
76 return m_Bytes <= other.m_Bytes;
77 }
78
80 bool operator>=(const UUID& other) const {
81 return m_Bytes >= other.m_Bytes;
82 }
83
85 UUID operator+(const UUID& other) const {
86 UUID result;
87 for (int i = 0; i < 16; ++i) {
88 result.m_Bytes[i] =
89 this->m_Bytes[i] ^ other.m_Bytes[i]; // XOR operation
90 }
91 return result;
92 }
93
95 UUID operator-(const UUID& other) const {
96 UUID result;
97 for (int i = 0; i < 16; ++i) {
98 result.m_Bytes[i] = this->m_Bytes[i] - other.m_Bytes[i];
99 }
100 return result;
101 }
102
103 private:
104 std::array<unsigned char, 16> m_Bytes;
105 };
106} // namespace FuncDoodle
Represents a single 16-byte opaque uuid-like unique identifier.
Definition UUID.h:31
friend std::ostream & operator<<(std::ostream &os, const UUID &obj)
Streams the UUID in its string form.
Definition UUID.cc:11
bool operator<=(const UUID &other) const
Returns true when this UUID sorts before or equal to another.
Definition UUID.h:75
const char * ToString() const
Converts the UUID into its string form.
Definition UUID.cc:15
std::array< unsigned char, 16 > Bytes()
Returns the raw 16-byte UUID payload.
Definition UUID.h:37
static UUID FromString(const char *str)
Parses a UUID from a string representation.
Definition UUID.cc:50
bool operator!=(const UUID &other) const
Compares two UUID values for inequality.
Definition UUID.cc:28
static UUID Gen()
Generates a new UUID value.
Definition UUID.cc:34
std::array< unsigned char, 16 > m_Bytes
Definition UUID.h:104
UUID operator+(const UUID &other) const
Combines two UUID values with byte-wise XOR.
Definition UUID.h:85
bool operator<(const UUID &other) const
Orders UUID values lexicographically by raw bytes.
Definition UUID.h:66
bool operator==(const UUID &other) const
Compares two UUID values for equality.
Definition UUID.cc:31
UUID(std::array< unsigned char, 16 > bytes)
Constructs a UUID from raw bytes.
Definition UUID.h:34
UUID()
Definition UUID.h:35
bool operator>=(const UUID &other) const
Returns true when this UUID sorts after or equal to another.
Definition UUID.h:80
bool operator>(const UUID &other) const
Orders UUID values lexicographically by raw bytes.
Definition UUID.h:70
UUID operator-(const UUID &other) const
Subtracts UUID bytes pairwise.
Definition UUID.h:95
The FuncDoodle C++ namespace.
Definition Common.h:12
Hash functor for using UUID as an associative-container key.
Definition UUID.h:52
size_t operator()(const UUID &uuid) const
Computes a stable hash for a UUID value.
Definition UUID.h:54