summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Common/Code/Qt3DSId.cpp
blob: 62eba9c92e2a9652ad59f0eff0464200546beed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/****************************************************************************
**
** Copyright (C) 1999-2001 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Qt3DSCommonPrecompile.h"
#include "Qt3DSId.h"
#include "Qt3DSTime.h"

namespace Q3DStudio {

//====================================================================
/**
 * Constructor .
 * Initialize the GUID to Zero.
 */
CId::CId()
    : m_Key(Q3DStudio::UUID_ZERO)
{
}

//====================================================================
/**
 * Constructor. The local Key will get initialized to the Value represented by the string.
 * @param inStringId is an ID in the form of a string.
 *
 */
CId::CId(wchar_t *inStringId)
{
    Q3DStudio::CString theString = inStringId;
    FromString(theString);
}

//====================================================================
/**
 * Constructor.
 * @param inId is an ID to initialize from.
 */
CId::CId(const CId &inId)
{
    m_Key.GuidPacked.Data1 = inId.m_Key.GuidPacked.Data1;
    m_Key.GuidPacked.Data2 = inId.m_Key.GuidPacked.Data2;
    m_Key.GuidPacked.Data3 = inId.m_Key.GuidPacked.Data3;
    m_Key.GuidPacked.Data4 = inId.m_Key.GuidPacked.Data4;
}

CId::CId(const GUID &inGUID)
{
    m_Key.GuidStd.Data1 = inGUID.Data1;
    m_Key.GuidStd.Data2 = inGUID.Data2;
    m_Key.GuidStd.Data3 = inGUID.Data3;
    ::memcpy(m_Key.GuidStd.Data4, inGUID.Data4, sizeof(inGUID.Data4));
}

CId::CId(long in1, long in2, long in3, long in4)
{
    m_Key.GuidPacked.Data1 = in1;
    m_Key.GuidPacked.Data2 = in2;
    m_Key.GuidPacked.Data3 = in3;
    m_Key.GuidPacked.Data4 = in4;
}

//====================================================================
/**
 * Generate a new Key
 *
 */
void CId::Generate()
{
    // Generate unique UUID that works the same way on all platforms. We can't use QUuid as the
    // generated UUID on non-Windows platforms many times fills the packed Data4 with 0,
    // which is considered invalid by the rest of the application
    static long s_LastMilliSecs = 0;
    Q3DStudio::CTime theTime = Q3DStudio::CTime::Now( );
    long theCurrentMilliSecs = theTime.GetMilliSecs( );

    // Don't do this more than once in a 10ms period.
    if ( s_LastMilliSecs != theCurrentMilliSecs )
    {
        s_LastMilliSecs = theCurrentMilliSecs;
        ::srand( (unsigned)( theCurrentMilliSecs ) );
    }

    // ::rand is 16 bits, GUID is 128 bits, smear random bits over the whole GUID
    m_Key.GuidPacked.Data1  = ::rand();
    m_Key.GuidPacked.Data1 <<= 16;
    m_Key.GuidPacked.Data1  |= ::rand();

    m_Key.GuidPacked.Data2  = ::rand();
    m_Key.GuidPacked.Data2 <<= 16;
    m_Key.GuidPacked.Data2  |= ::rand();

    m_Key.GuidPacked.Data3  = ::rand();
    m_Key.GuidPacked.Data3 <<= 16;
    m_Key.GuidPacked.Data3  |= ::rand();

    m_Key.GuidPacked.Data4  = ::rand();
    m_Key.GuidPacked.Data4 <<= 16;
    m_Key.GuidPacked.Data4  |= ::rand();
}

//====================================================================
/**
 * Convert this CId to a GUID.
 * @return a GUID struct.
 */
GUID CId::Convert() const
{
    GUID theThis;

    theThis.Data1 = m_Key.GuidStd.Data1;
    theThis.Data2 = m_Key.GuidStd.Data2;
    theThis.Data3 = m_Key.GuidStd.Data3;
    ::memcpy(theThis.Data4, m_Key.GuidStd.Data4, sizeof(m_Key.GuidStd.Data4));

    return theThis;
}

//====================================================================
/**
 * Compare two CIds.
 * @param inRVal is the Right side CId.
 * @return True if they were equal.
 */
bool CId::operator==(const CId &inRVal) const
{
    bool theResult = ((m_Key.GuidPacked.Data1 == inRVal.m_Key.GuidPacked.Data1)
                      && (m_Key.GuidPacked.Data2 == inRVal.m_Key.GuidPacked.Data2)
                      && (m_Key.GuidPacked.Data3 == inRVal.m_Key.GuidPacked.Data3)
                      && (m_Key.GuidPacked.Data4 == inRVal.m_Key.GuidPacked.Data4));

    return theResult;
}

//====================================================================
/**
 * Compare two CIds.
 * @param inRVal is the Right side CId.
 * @return True if they were equal.
 */
bool CId::operator==(const Q3DStudio::UUID &inRVal) const
{
    bool theResult = ((m_Key.GuidPacked.Data1 == inRVal.GuidPacked.Data1)
                      && (m_Key.GuidPacked.Data2 == inRVal.GuidPacked.Data2)
                      && (m_Key.GuidPacked.Data3 == inRVal.GuidPacked.Data3)
                      && (m_Key.GuidPacked.Data4 == inRVal.GuidPacked.Data4));

    return theResult;
}

//====================================================================
/**
 * Compare two CIds for not equal.
 * @param inRVal is the Right side CId.
 * @return True if they were equal.
 */
bool CId::operator!=(const CId &inRVal) const
{
    return !(*this == inRVal);
}

//====================================================================
/**
 * Compare if this CId is less than the Right Size CId.
 * @param inRVal is the Right side CId.
 * @return True if this CId is Less than.
 */
bool CId::operator<(const CId &inRVal) const
{
    //	const Q3DStudio::TGUIDPacked& theLVal = m_Key.GuidPacked;
    //	const Q3DStudio::TGUIDPacked& theRVal = inRVal.m_Key.GuidPacked;
    //	return !(	theLVal.Data1 >= theRVal.Data1 &&
    //				theLVal.Data2 >= theRVal.Data2 &&
    //				theLVal.Data3 >= theRVal.Data3 &&
    //				theLVal.Data4 >= theRVal.Data4 );

    bool theResult = false;

    if (m_Key.GuidPacked.Data1 < inRVal.m_Key.GuidPacked.Data1) {
        theResult = true;
    }
    // If it is equal then move on to the next element.
    else if (m_Key.GuidPacked.Data1 == inRVal.m_Key.GuidPacked.Data1) {
        if (m_Key.GuidPacked.Data2 < inRVal.m_Key.GuidPacked.Data2) {
            theResult = true;
        }
        // If it is equal then move on to the next element.
        else if (m_Key.GuidPacked.Data2 == inRVal.m_Key.GuidPacked.Data2) {
            if (m_Key.GuidPacked.Data3 < inRVal.m_Key.GuidPacked.Data3) {
                theResult = true;
            } else if (m_Key.GuidPacked.Data3 == inRVal.m_Key.GuidPacked.Data3) {
                if (m_Key.GuidPacked.Data4 < inRVal.m_Key.GuidPacked.Data4) {
                    theResult = true;
                }
            }
        }
    }
    return theResult;
}

//====================================================================
/**
 * Set this Cid equal to the Right Side CId.
 * @param inRVal is the Right side CId.
 * @return this CId.
 */
CId &CId::operator=(const CId &inRVal)
{
    m_Key.GuidPacked.Data1 = inRVal.m_Key.GuidPacked.Data1;
    m_Key.GuidPacked.Data2 = inRVal.m_Key.GuidPacked.Data2;
    m_Key.GuidPacked.Data3 = inRVal.m_Key.GuidPacked.Data3;
    m_Key.GuidPacked.Data4 = inRVal.m_Key.GuidPacked.Data4;

    return *this;
}
//====================================================================
/**
 * Convert this CId into a String.
 * @return The String version of this CId.
 */
Q3DStudio::CString CId::ToString() const
{
    Q3DStudio::CString theGuidStr;
    theGuidStr.Format(_LSTR("{%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
                      m_Key.GuidStd.Data1, m_Key.GuidStd.Data2, m_Key.GuidStd.Data3,
                      m_Key.GuidStd.Data4[0], (unsigned long)m_Key.GuidStd.Data4[1],
                      (unsigned long)m_Key.GuidStd.Data4[2], (unsigned long)m_Key.GuidStd.Data4[3],
                      (unsigned long)m_Key.GuidStd.Data4[4], (unsigned long)m_Key.GuidStd.Data4[5],
                      (unsigned long)m_Key.GuidStd.Data4[6], (unsigned long)m_Key.GuidStd.Data4[7]);

    return theGuidStr;
}

//====================================================================
/**
 * Convert this CId into the Incomming String.
 * @param inStringId is a CId represented as a String.
 * @return This CId.
 */
CId &CId::FromString(const Q3DStudio::CString &inStringId)
{
    unsigned long theData[8];
    const wchar_t *theBuffer = (const wchar_t *)inStringId;

    // All of the %02X formats get scaned into a 32bit long so we have to convert down below....
    long theFields = ::swscanf(theBuffer, L"{%08x-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x}",
                               &m_Key.GuidStd.Data1, &m_Key.GuidStd.Data2, &m_Key.GuidStd.Data3,
                               &theData[0], &theData[1], &theData[2], &theData[3], &theData[4],
                               &theData[5], &theData[6], &theData[7]);

    if (theFields == 11) // magic number
    {
        // This is where we convert the 32 values into unsigned chars.
        for (long theIndex = 0; theIndex < 8; ++theIndex) {
            m_Key.GuidStd.Data4[theIndex] = static_cast<unsigned char>(theData[theIndex]);
        }
    } else {
        // If we didn't read in all the fields, zero out the key
        m_Key = Q3DStudio::UUID_ZERO;
    }

    return *this;
}

bool CId::IsZero() const
{
    return (*this == Q3DStudio::UUID_ZERO);
}

CId::operator GUID() const
{
    return Convert();
}
};