ActiveLib
Loading...
Searching...
No Matches
NameID.h
1
6#ifndef ACTIVE_UTILITY_NAME_ID
7#define ACTIVE_UTILITY_NAME_ID
8
9#include "Active/Utility/Guid.h"
10
11#include <optional>
12
13namespace active::utility {
14
21 class NameID {
22 public:
23
24 // MARK: - Types
25
27 using Option = std::optional<NameID>;
28
29 // MARK: - Constructors
30
34 NameID() {}
40 NameID(const String& str, const Guid& guid = Guid{}) : name{str}, id{guid} {}
46 NameID(const char* str, const Guid& guid = Guid{}) : name{str}, id{guid} {}
52 NameID(const Guid& guid, const String& str = String{}) : name{str}, id{guid} {}
53
54 // MARK: - Variables
55
56 //Optional identifying name (empty = unused)
57 String name;
58 //Optional guid (undefined = unused)
59 Guid id;
60
61 // MARK: - Operators
62
68 bool operator== (const NameID& ref) const { return (id || ref.id) ? (id == ref.id) : (name == ref.name); }
74 bool operator!= (const NameID& ref) const { return !(*this == ref); }
80 bool operator< (const NameID& ref) const { return ((id || ref.id) && (id != ref.id)) ? (id < ref.id) : (name < ref.name); }
85 operator bool() const { return id || !name.empty(); }
86
87 // MARK: - Functions (const)
88
89
90 // MARK: - Functions (mutating)
91
95 void clear() {
96 name.clear();
97 id.clear();
98 }
99 };
100
101}
102
104template<>
105struct std::hash<active::utility::NameID> {
106 std::size_t operator()(const active::utility::NameID& nameID) const noexcept {
107 std::size_t h1 = std::hash<active::utility::String>{}(nameID.name);
108 std::size_t h2 = std::hash<active::utility::Guid>{}(nameID.id);
109 return h1 ^ (h2 << 1);
110 }
111};
112
113#endif //ACTIVE_UTILITY_NAME_ID
Definition Guid.h:16
Definition NameID.h:21
std::optional< NameID > Option
Optional.
Definition NameID.h:27
NameID(const Guid &guid, const String &str=String{})
Definition NameID.h:52
NameID(const char *str, const Guid &guid=Guid{})
Definition NameID.h:46
bool operator<(const NameID &ref) const
Definition NameID.h:80
bool operator!=(const NameID &ref) const
Definition NameID.h:74
NameID()
Definition NameID.h:34
NameID(const String &str, const Guid &guid=Guid{})
Definition NameID.h:40
bool operator==(const NameID &ref) const
Definition NameID.h:68
void clear()
Definition NameID.h:95
A Unicode-aware string class.
Definition String.h:51
Definition Base64Transport.h:11