Debian Codegraph @

src/Base/Uuid.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2008 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library 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 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #include <QUuid>
27
28 #include <stdexcept>
29
30 #include "Uuid.h"
31
32
33 using namespace Base;
34
35
36 //**************************************************************************
37 // Construction/Destruction
38
39 /**
40 * A constructor.
41 * A more elaborate description of the constructor.
42 */
43 Uuid::Uuid()
44 {
45 _uuid = createUuid();
46 }
47
48 /**
49 * A destructor.
50 * A more elaborate description of the destructor.
51 */
52 Uuid::~Uuid() = default;
53
54 //**************************************************************************
55 //**************************************************************************
56 // Get the UUID
57 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58 std::string Uuid::createUuid()
59 {
60 std::string Uuid;
61 QString uuid = QUuid::createUuid().toString();
62 uuid = uuid.mid(1);
63 uuid.chop(1);
64 Uuid = uuid.toLatin1().constData();
65 return Uuid;
66 }
67
68 void Uuid::setValue(const char* sString)
69 {
70 if (sString) {
71 QUuid uuid(QString::fromLatin1(sString));
72 if (uuid.isNull()) {
73 throw std::runtime_error("invalid uuid");
74 }
75 // remove curly braces
76 QString id = uuid.toString();
77 id = id.mid(1);
78 id.chop(1);
79 _uuid = id.toLatin1().constData();
80 }
81 }
82
83 void Uuid::setValue(const std::string& sString)
84 {
85 setValue(sString.c_str());
86 }
87
88 const std::string& Uuid::getValue() const
89 {
90 return _uuid;
91 }