LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <>
169  return SExpression::createToken(obj.toStr());
170 }
171 
172 template <>
174  bool throwIfEmpty) {
175  return Uuid::fromString(sexpr.getStringOrToken(throwIfEmpty));
176 }
177 
178 template <>
179 inline SExpression serializeToSExpression(const tl::optional<Uuid>& obj) {
180  if (obj) {
181  return serializeToSExpression(*obj);
182  } else {
183  return SExpression::createToken("none");
184  }
185 }
186 
187 template <>
188 inline tl::optional<Uuid> deserializeFromSExpression(const SExpression& sexpr,
189  bool throwIfEmpty) {
190  QString str = sexpr.getStringOrToken(throwIfEmpty);
191  if (str == "none") {
192  return tl::nullopt;
193  } else {
194  return deserializeFromSExpression<Uuid>(sexpr, throwIfEmpty); // can throw
195  }
196 }
197 
198 inline QDataStream& operator<<(QDataStream& stream, const Uuid& uuid) noexcept {
199  stream << uuid.toStr();
200  return stream;
201 }
202 
203 inline QDebug operator<<(QDebug stream, const Uuid& uuid) noexcept {
204  stream << QString("Uuid(%1)").arg(uuid.toStr());
205  return stream;
206 }
207 
208 inline uint qHash(const Uuid& key, uint seed) noexcept {
209  return ::qHash(key.toStr(), seed);
210 }
211 
212 /*******************************************************************************
213  * End of File
214  ******************************************************************************/
215 
216 } // namespace librepcb
217 
218 #endif // LIBREPCB_UUID_H
Uuid(const Uuid &other) noexcept
Copy constructor.
Definition: uuid.h:74
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
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
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
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:208
Uuid()=delete
Default constructor (disabled to avoid creating invalid UUIDs)
static Uuid fromString(const QString &str)
Create Uuid from a string.
Definition: uuid.cpp:63
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:73
static Uuid createRandom() noexcept
Create a new random UUID.
Definition: uuid.cpp:53
The SExpression class.
Definition: sexpression.h:60
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:119