LibrePCB Developers Documentation
Loading...
Searching...
No Matches
uuid.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_CORE_UUID_H
21#define LIBREPCB_CORE_UUID_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include <QtCore>
27
28#include <optional>
29
30/*******************************************************************************
31 * Namespace / Forward Declarations
32 ******************************************************************************/
33namespace librepcb {
34
35/*******************************************************************************
36 * Class Uuid
37 ******************************************************************************/
38
56class Uuid final {
57 Q_DECLARE_TR_FUNCTIONS(Uuid)
58
59public:
60 // Constructors / Destructor
61
65 Uuid() = delete;
66
72 Uuid(const Uuid& other) noexcept : mUuid(other.mUuid) {}
73
77 ~Uuid() noexcept
78#if defined(__GNUC__) && (__GNUC__ == 13) && (__GNUC_MINOR__ == 2)
79 ; // Workaround for compiler warning "maybe-uninitialized".
80#else
81 = default;
82#endif
83
84 // Getters
85
91 QString toStr() const noexcept { return mUuid; }
92
94
101 Uuid& operator=(const Uuid& rhs) noexcept {
102 mUuid = rhs.mUuid;
103 return *this;
104 }
105 bool operator==(const Uuid& rhs) const noexcept { return mUuid == rhs.mUuid; }
106 bool operator!=(const Uuid& rhs) const noexcept { return mUuid != rhs.mUuid; }
107 bool operator<(const Uuid& rhs) const noexcept { return mUuid < rhs.mUuid; }
108 bool operator>(const Uuid& rhs) const noexcept { return mUuid > rhs.mUuid; }
109 bool operator<=(const Uuid& rhs) const noexcept { return mUuid <= rhs.mUuid; }
110 bool operator>=(const Uuid& rhs) const noexcept { return mUuid >= rhs.mUuid; }
112
113 // Static Methods
114
123 static bool isValid(const QString& str) noexcept;
124
130 static Uuid createRandom() noexcept;
131
141 static Uuid fromString(const QString& str);
142
152 static std::optional<Uuid> tryFromString(const QString& str) noexcept;
153
154private: // Methods
160 explicit Uuid(const QString& str) noexcept : mUuid(str) {}
161
162private: // Data
163 QString mUuid;
164};
165
166/*******************************************************************************
167 * Non-Member Functions
168 ******************************************************************************/
169
170inline QDataStream& operator<<(QDataStream& stream, const Uuid& uuid) noexcept {
171 stream << uuid.toStr();
172 return stream;
173}
174
175inline QDebug operator<<(QDebug stream, const Uuid& uuid) noexcept {
176 stream << QString("Uuid(%1)").arg(uuid.toStr());
177 return stream;
178}
179
180inline std::size_t qHash(const Uuid& key, std::size_t seed = 0) noexcept {
181 return ::qHash(key.toStr(), seed);
182}
183
184} // namespace librepcb
185
186namespace std {
187inline size_t qHash(const optional<librepcb::Uuid>& key,
188 size_t seed = 0) noexcept {
189 return ::qHash(key ? key->toStr() : QString(), seed);
190}
191} // namespace std
192
193/*******************************************************************************
194 * End of File
195 ******************************************************************************/
196
197#endif
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition uuid.h:56
Uuid & operator=(const Uuid &rhs) noexcept
Operator overloadings.
Definition uuid.h:101
QString mUuid
Guaranteed to always contain a valid UUID.
Definition uuid.h:163
QString toStr() const noexcept
Get the UUID as a string (without braces)
Definition uuid.h:91
Uuid(const Uuid &other) noexcept
Copy constructor.
Definition uuid.h:72
bool operator>(const Uuid &rhs) const noexcept
Definition uuid.h:108
bool operator==(const Uuid &rhs) const noexcept
Definition uuid.h:105
bool operator!=(const Uuid &rhs) const noexcept
Definition uuid.h:106
Uuid()=delete
Default constructor (disabled to avoid creating invalid UUIDs)
~Uuid() noexcept=default
Destructor.
bool operator>=(const Uuid &rhs) const noexcept
Definition uuid.h:110
bool operator<(const Uuid &rhs) const noexcept
Definition uuid.h:107
static std::optional< Uuid > tryFromString(const QString &str) noexcept
Try creating a Uuid from a string, returning empty optional if invalid.
Definition uuid.cpp:124
static Uuid createRandom() noexcept
Create a new random UUID.
Definition uuid.cpp:104
static bool isValid(const QString &str) noexcept
Check if a string is a valid UUID.
Definition uuid.cpp:45
bool operator<=(const Uuid &rhs) const noexcept
Definition uuid.h:109
static Uuid fromString(const QString &str)
Create Uuid from a string.
Definition uuid.cpp:115
Definition occmodel.cpp:77
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108
Definition uuid.h:186
size_t qHash(const optional< librepcb::Uuid > &key, size_t seed=0) noexcept
Definition uuid.h:187