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_UUID_H
21 #define LIBREPCB_UUID_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "fileio/sexpression.h"
27 
28 #include <optional/tl/optional.hpp>
29 
30 #include <QtCore>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 /*******************************************************************************
38  * Class Uuid
39  ******************************************************************************/
40 
58 class Uuid final {
59  Q_DECLARE_TR_FUNCTIONS(Uuid)
60 
61 public:
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 
151 private: // Methods
157  explicit Uuid(const QString& str) noexcept : mUuid(str) {}
158 
159 private: // Data
160  QString mUuid;
161 };
162 
163 /*******************************************************************************
164  * Non-Member Functions
165  ******************************************************************************/
166 
167 template <>
168 inline SExpression serialize(const Uuid& obj) {
169  return SExpression::createToken(obj.toStr());
170 }
171 
172 template <>
173 inline Uuid deserialize(const SExpression& sexpr, const Version& fileFormat) {
174  Q_UNUSED(fileFormat);
175  return Uuid::fromString(sexpr.getValue()); // can throw
176 }
177 
178 template <>
179 inline SExpression serialize(const tl::optional<Uuid>& obj) {
180  if (obj) {
181  return serialize(*obj);
182  } else {
183  return SExpression::createToken("none");
184  }
185 }
186 
187 template <>
188 inline tl::optional<Uuid> deserialize(const SExpression& sexpr,
189  const Version& fileFormat) {
190  if (sexpr.getValue() == "none") {
191  return tl::nullopt;
192  } else {
193  return deserialize<Uuid>(sexpr, fileFormat); // can throw
194  }
195 }
196 
197 inline QDataStream& operator<<(QDataStream& stream, const Uuid& uuid) noexcept {
198  stream << uuid.toStr();
199  return stream;
200 }
201 
202 inline QDebug operator<<(QDebug stream, const Uuid& uuid) noexcept {
203  stream << QString("Uuid(%1)").arg(uuid.toStr());
204  return stream;
205 }
206 
207 inline uint qHash(const Uuid& key, uint seed) noexcept {
208  return ::qHash(key.toStr(), seed);
209 }
210 
211 /*******************************************************************************
212  * End of File
213  ******************************************************************************/
214 
215 } // namespace librepcb
216 
217 #endif // LIBREPCB_UUID_H
Uuid(const QString &str) noexcept
Constructor which creates a Uuid object from a string.
Definition: uuid.h:157
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
Uuid(const Uuid &other) noexcept
Copy constructor.
Definition: uuid.h:74
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:110
Uuid & operator=(const Uuid &rhs) noexcept
Operator overloadings.
Definition: uuid.h:98
bool operator>=(const Uuid &rhs) const noexcept
Operator overloadings.
Definition: uuid.h:107
SExpression serialize(const HAlign &obj)
Definition: alignment.h:76
Definition: airwiresbuilder.cpp:32
HAlign deserialize(const SExpression &sexpr, const Version &fileFormat)
Definition: alignment.h:90
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:290
bool operator==(const Uuid &rhs) const noexcept
Operator overloadings.
Definition: uuid.h:102
~Uuid() noexcept=default
Destructor.
bool operator<=(const Uuid &rhs) const noexcept
Operator overloadings.
Definition: uuid.h:106
uint qHash(const Uuid &key, uint seed) noexcept
Definition: uuid.h:207
Uuid()=delete
Default constructor (disabled to avoid creating invalid UUIDs)
static Uuid fromString(const QString &str)
Create Uuid from a string.
Definition: uuid.cpp:106
QString mUuid
Guaranteed to always contain a valid UUID.
Definition: uuid.h:160
static bool isValid(const QString &str) noexcept
Check if a string is a valid UUID.
Definition: uuid.cpp:37
bool operator>(const Uuid &rhs) const noexcept
Operator overloadings.
Definition: uuid.h:105
bool operator<(const Uuid &rhs) const noexcept
Operator overloadings.
Definition: uuid.h:104
bool operator!=(const Uuid &rhs) const noexcept
Operator overloadings.
Definition: uuid.h:103
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
static tl::optional< Uuid > tryFromString(const QString &str) noexcept
Try creating a Uuid from a string, returning empty optional if invalid.
Definition: uuid.cpp:115
static Uuid createRandom() noexcept
Create a new random UUID.
Definition: uuid.cpp:96
const QString & getValue() const
Definition: sexpression.cpp:77
The SExpression class.
Definition: sexpression.h:72
QString toStr() const noexcept
Get the UUID as a string (without braces)
Definition: uuid.h:88
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:120