LibrePCB Developers Documentation
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 "../qtcompat.h"
27
28#include <QtCore>
29
30#include <optional.hpp>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37/*******************************************************************************
38 * Class Uuid
39 ******************************************************************************/
40
58class Uuid final {
59 Q_DECLARE_TR_FUNCTIONS(Uuid)
60
61public:
62 // Constructors / Destructor
63
67 Uuid() = delete;
68
74 Uuid(const Uuid& other) noexcept : mUuid(other.mUuid) {}
75
79 ~Uuid() noexcept = default;
80
81 // Getters
82
88 QString toStr() const noexcept { return mUuid; }
89
91
98 Uuid& operator=(const Uuid& rhs) noexcept {
99 mUuid = rhs.mUuid;
100 return *this;
101 }
102 bool operator==(const Uuid& rhs) const noexcept { return mUuid == rhs.mUuid; }
103 bool operator!=(const Uuid& rhs) const noexcept { return mUuid != rhs.mUuid; }
104 bool operator<(const Uuid& rhs) const noexcept { return mUuid < rhs.mUuid; }
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; }
109
110 // Static Methods
111
120 static bool isValid(const QString& str) noexcept;
121
127 static Uuid createRandom() noexcept;
128
138 static Uuid fromString(const QString& str);
139
149 static tl::optional<Uuid> tryFromString(const QString& str) noexcept;
150
151private: // Methods
157 explicit Uuid(const QString& str) noexcept : mUuid(str) {}
158
159private: // Data
160 QString mUuid;
161};
162
163/*******************************************************************************
164 * Non-Member Functions
165 ******************************************************************************/
166
167inline QDataStream& operator<<(QDataStream& stream, const Uuid& uuid) noexcept {
168 stream << uuid.toStr();
169 return stream;
170}
171
172inline QDebug operator<<(QDebug stream, const Uuid& uuid) noexcept {
173 stream << QString("Uuid(%1)").arg(uuid.toStr());
174 return stream;
175}
176
177inline QtCompat::Hash qHash(const Uuid& key, QtCompat::Hash seed = 0) noexcept {
178 return ::qHash(key.toStr(), seed);
179}
180
181} // namespace librepcb
182
183namespace tl {
184inline ::librepcb::QtCompat::Hash qHash(
185 const optional<librepcb::Uuid>& key,
186 ::librepcb::QtCompat::Hash seed = 0) noexcept {
187 return ::qHash(key ? key->toStr() : QString(), seed);
188}
189} // namespace tl
190
191/*******************************************************************************
192 * End of File
193 ******************************************************************************/
194
195#endif
uint Hash
Return type of Qt's qHash() function.
Definition: qtcompat.h:58
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
Uuid & operator=(const Uuid &rhs) noexcept
Operator overloadings.
Definition: uuid.h:98
static tl::optional< Uuid > tryFromString(const QString &str) noexcept
Try creating a Uuid from a string, returning empty optional if invalid.
Definition: uuid.cpp:119
QString mUuid
Guaranteed to always contain a valid UUID.
Definition: uuid.h:160
QString toStr() const noexcept
Get the UUID as a string (without braces)
Definition: uuid.h:88
Uuid(const Uuid &other) noexcept
Copy constructor.
Definition: uuid.h:74
bool operator>(const Uuid &rhs) const noexcept
Definition: uuid.h:105
bool operator==(const Uuid &rhs) const noexcept
Definition: uuid.h:102
bool operator!=(const Uuid &rhs) const noexcept
Definition: uuid.h:103
Uuid()=delete
Default constructor (disabled to avoid creating invalid UUIDs)
~Uuid() noexcept=default
Destructor.
bool operator>=(const Uuid &rhs) const noexcept
Definition: uuid.h:107
bool operator<(const Uuid &rhs) const noexcept
Definition: uuid.h:104
static Uuid createRandom() noexcept
Create a new random UUID.
Definition: uuid.cpp:99
static bool isValid(const QString &str) noexcept
Check if a string is a valid UUID.
Definition: uuid.cpp:40
bool operator<=(const Uuid &rhs) const noexcept
Definition: uuid.h:106
static Uuid fromString(const QString &str)
Create Uuid from a string.
Definition: uuid.cpp:110
Definition: occmodel.cpp:77
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
Definition: uuid.h:183
inline ::librepcb::QtCompat::Hash qHash(const optional< librepcb::Uuid > &key, ::librepcb::QtCompat::Hash seed=0) noexcept
Definition: uuid.h:184