Overte C++ Documentation
ScriptUUID.h
1 //
2 // ScriptUUID.h
3 // libraries/script-engine/src/
4 //
5 // Created by Andrew Meadows on 2014-04-07
6 // Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
7 // Copyright 2023 Overte e.V.
8 //
9 // Scriptable interface for a UUID helper class object. Used exclusively in the JavaScript API
10 //
11 // Distributed under the Apache License, Version 2.0.
12 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
13 // SPDX-License-Identifier: Apache-2.0
14 //
15 
18 
19 #ifndef hifi_ScriptUUID_h
20 #define hifi_ScriptUUID_h
21 
22 #include <QUuid>
23 #include <QObject>
24 
25 #include "Scriptable.h"
26 
27 /*@jsdoc
28  * The <code>Uuid</code> API provides facilities for working with UUIDs.
29  *
30  * @namespace Uuid
31  * @variation 0
32  *
33  * @hifi-interface
34  * @hifi-client-entity
35  * @hifi-avatar
36  * @hifi-server-entity
37  * @hifi-assignment-client
38  *
39  * @property {Uuid} NULL - The null UUID, <code>"{00000000-0000-0000-0000-000000000000}"</code>.
40  */
42 class ScriptUUID : public QObject, protected Scriptable {
43  Q_OBJECT
44  Q_PROPERTY(QString NULL READ NULL_UUID CONSTANT) // String for use in scripts.
45 
46 public slots:
47  /*@jsdoc
48  * Generates a UUID from a string representation of the UUID.
49  * @function Uuid(0).fromString
50  * @param {string} string - A string representation of a UUID. The curly braces are optional.
51  * @returns {Uuid} A UUID if the given <code>string</code> is valid, <code>null</code> otherwise.
52  * @example <caption>Valid and invalid parameters.</caption>
53  * var uuid = Uuid.fromString("{527c27ea-6d7b-4b47-9ae2-b3051d50d2cd}");
54  * print(uuid); // {527c27ea-6d7b-4b47-9ae2-b3051d50d2cd}
55  *
56  * uuid = Uuid.fromString("527c27ea-6d7b-4b47-9ae2-b3051d50d2cd");
57  * print(uuid); // {527c27ea-6d7b-4b47-9ae2-b3051d50d2cd}
58  *
59  * uuid = Uuid.fromString("527c27ea");
60  * print(uuid); // null
61  */
62  QUuid fromString(const QString& string);
63 
64  /*@jsdoc
65  * Generates a string representation of a UUID. However, because UUIDs are represented in JavaScript as strings, this is in
66  * effect a no-op.
67  * @function Uuid(0).toString
68  * @param {Uuid} id - The UUID to generate a string from.
69  * @returns {string} - A string representation of the UUID.
70  */
71  QString toString(const QUuid& id);
72 
73  /*@jsdoc
74  * Generates a new UUID.
75  * @function Uuid(0).generate
76  * @returns {Uuid} A new UUID.
77  * @example <caption>Generate a new UUID and reports its JavaScript type.</caption>
78  * var uuid = Uuid.generate();
79  * print(uuid); // {nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}
80  * print(typeof uuid); // string
81  */
82  QUuid generate();
83 
84  /*@jsdoc
85  * Tests whether two UUIDs are equal.
86  * @function Uuid(0).isEqual
87  * @param {Uuid} idA - The first UUID to compare.
88  * @param {Uuid} idB - The second UUID to compare.
89  * @returns {boolean} <code>true</code> if the two UUIDs are equal, otherwise <code>false</code>.
90  * @example <caption>Demonstrate <code>true</code> and <code>false</code> cases.</caption>
91  * var uuidA = Uuid.generate();
92  * var uuidB = Uuid.generate();
93  * print(Uuid.isEqual(uuidA, uuidB)); // false
94  * uuidB = uuidA;
95  * print(Uuid.isEqual(uuidA, uuidB)); // true
96  */
97  bool isEqual(const QUuid& idA, const QUuid& idB);
98 
99  /*@jsdoc
100  * Tests whether a UUID is null.
101  * @function Uuid(0).isNull
102  * @param {Uuid} id - The UUID to test.
103  * @returns {boolean} <code>true</code> if the UUID equals <code>Uuid.NULL</code> or is <code>null</code>, otherwise
104  * <code>false</code>.
105  * @example <caption>Demonstrate <code>true</code> and <code>false</code> cases.</caption>
106  * var uuid; // undefined
107  * print(Uuid.isNull(uuid)); // false
108  * uuid = Uuid.generate();
109  * print(Uuid.isNull(uuid)); // false
110  * uuid = Uuid.NULL;
111  * print(Uuid.isNull(uuid)); // true
112  * uuid = null;
113  * print(Uuid.isNull(uuid)); // true
114  */
115  bool isNull(const QUuid& id);
116 
117  /*@jsdoc
118  * Prints a UUID to the program log, as a text label followed by the UUID value.
119  * @function Uuid(0).print
120  * @param {string} label - The label to print.
121  * @param {Uuid} id - The UUID to print.
122  * @example <caption>Two ways of printing a label plus UUID.</caption>
123  * var uuid = Uuid.generate();
124  * Uuid.print("Generated UUID:", uuid); // Generated UUID: {nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}
125  * print("Generated UUID: " + uuid); // Generated UUID: {nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}
126  */
127  void print(const QString& label, const QUuid& id);
128 
129 private:
130  const QString NULL_UUID() { return NULL_ID; }
131  const QString NULL_ID { "{00000000-0000-0000-0000-000000000000}" };
132 };
133 
134 #endif // hifi_ScriptUUID_h
135 
Provides the Uuid scripting interface.
Definition: ScriptUUID.h:42
[ScriptInterface] Provides an engine-independent interface for QScriptable
Definition: Scriptable.h:29