Fawkes API Fawkes Development Version
uuid.cpp
1/***************************************************************************
2 * uuid.cpp - uuid_t wrapper
3 *
4 * Created: Tue 17 Nov 2020 10:17:15 CET 10:17
5 * Copyright 2020 Till Hofmann <hofmann@kbsg.rwth-aachen.de>
6 ****************************************************************************/
7
8/* This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
17 *
18 * Read the full text in the LICENSE.GPL file in the doc directory.
19 */
20
21#include "core/exceptions/software.h"
22
23#include <utils/uuid.h>
24#include <uuid/uuid.h>
25
26namespace fawkes {
27
28/** @class Uuid
29 * A convenience class for universally unique identifiers (UUIDs).
30 * It wraps uuid(3) to allow easier creation and deletion of UUIDs.
31 */
32
33/** Generate a new Uuid. */
34Uuid::Uuid() noexcept
35{
36 uuid_generate(uuid_);
37}
38
39/** Destructor, clears up the occupied storage. */
40Uuid::~Uuid() noexcept
41{
42 uuid_clear(uuid_);
43}
44
45/** Copy constructor.
46 * The newly constructed Uuid is guaranteed to be the same as the source Uuid.
47 * @param other The Uuid to copy from
48 */
49Uuid::Uuid(const Uuid &other) noexcept
50{
51 uuid_copy(uuid_, other.uuid_);
52}
53
54/** Move constructor.
55 * This leaves the other Uuid in an undefined state. The newly constructed Uuid
56 * is guaranteed to be the same as the source Uuid.
57 * @param other The Uuid to move from
58 */
59Uuid::Uuid(Uuid &&other) noexcept
60{
61 uuid_copy(uuid_, other.uuid_);
62 uuid_clear(other.uuid_);
63}
64
65/** Construct a Uuid from a string.
66 * @param string The string represenation of the Uuid, of the form
67 * 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb
68 */
69Uuid::Uuid(const char *string)
70{
71 int res = uuid_parse(string, uuid_);
72 if (res != 0) {
73 throw IllegalArgumentException("Cannot parse '%s' into a uuid", string);
74 }
75}
76
77/** Assignment operator.
78 * After assignment, both Uuids are guaranteed to be the same.
79 * @param other The Uuid to assign from
80 * @return A reference to the assigned Uuid
81 */
82Uuid &
83Uuid::operator=(const Uuid &other) noexcept
84{
85 uuid_copy(uuid_, other.uuid_);
86 return *this;
87}
88
89/** Move assignment operator.
90 * This leaves the other Uuid in an undefined state. The assigned Uuid
91 * is guaranteed to be the same as the source Uuid.
92 * @param other The Uuid to assign from
93 * @return A reference to the assigned Uuid
94 */
95Uuid &
96Uuid::operator=(Uuid &&other) noexcept
97{
98 uuid_copy(uuid_, other.uuid_);
99 uuid_clear(other.uuid_);
100 return *this;
101}
102
103/** Get the string representation of the Uuid.
104 * @return The Uuid as string of the form 1b4e28ba-2fa1-11d2-883f-b9a761bde3fb
105 */
106std::string
108{
109 char res[37];
110 uuid_unparse(uuid_, res);
111 return std::string(res);
112}
113
114/** Compare two Uuids.
115 * @param uuid The first Uuid to compare
116 * @param other The second Uuid to compare
117 * @return True if the first Uuid is smaller than the second
118 */
119bool
120operator<(const Uuid &uuid, const Uuid &other) noexcept
121{
122 return (uuid_compare(uuid.uuid_, other.uuid_) < 0);
123}
124
125/** Compare two Uuids.
126 * @param uuid The first Uuid to compare
127 * @param other The second Uuid to compare
128 * @return True if the two Uuids are the same
129 */
130bool
131operator==(const Uuid &uuid, const Uuid &other) noexcept
132{
133 return (uuid_compare(uuid.uuid_, other.uuid_) == 0);
134}
135
136/** Compare two Uuids.
137 * @param uuid The first Uuid to compare
138 * @param other The second Uuid to compare
139 * @return True if the two Uuids are not the same
140 */
141bool
142operator!=(const Uuid &uuid, const Uuid &other) noexcept
143{
144 return (uuid_compare(uuid.uuid_, other.uuid_) != 0);
145}
146
147} // namespace fawkes
Expected parameter is missing.
Definition: software.h:80
A convenience class for universally unique identifiers (UUIDs).
Definition: uuid.h:29
Uuid & operator=(const Uuid &other) noexcept
Assignment operator.
Definition: uuid.cpp:83
~Uuid() noexcept
Destructor, clears up the occupied storage.
Definition: uuid.cpp:40
std::string get_string() const
Get the string representation of the Uuid.
Definition: uuid.cpp:107
Uuid() noexcept
Generate a new Uuid.
Definition: uuid.cpp:34
Fawkes library namespace.
bool operator==(const Uuid &uuid, const Uuid &other) noexcept
Compare two Uuids.
Definition: uuid.cpp:131
bool operator<(const Uuid &uuid, const Uuid &other) noexcept
Compare two Uuids.
Definition: uuid.cpp:120
bool operator!=(const Uuid &uuid, const Uuid &other) noexcept
Compare two Uuids.
Definition: uuid.cpp:142