summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Common/Code/GUIDUtilities.h
blob: 7bc3e77f0b4d9bc035f62e483268915058754136 (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
/****************************************************************************
**
** 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$
**
****************************************************************************/
#pragma once

//==============================================================================
//	Includes
//==============================================================================
//#include "Qt3DSTime.h"
#include "Qt3DSString.h"
#include "Qt3DSId.h"
#include <stdio.h>

/*
        CId has been revamped to use the more robust OS-dependent GUID generator.
        Because this utility file is heavily used all over the place,
        the best option is to wrap it around CId.
*/

const GUID UNASSIGNED_GUID = { 0, 0, 0, "" };
const GUID SCENE_GUID = {
    0xffffffff, 0xffff, 0xffff, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
};

//==============================================================================
/**
 *	less()	Compared two GUIDs
 *
 *	A compares two GUIDs
 *
 *	@param	<GUID>	inGUID1	The GUID to compare to the second
 *	@param	<GUID>	inGUID2 The GUID to compare to the first
 *
 *	@return	<bool>	true if GUID1 and GUID2 are equal, otherwise false.
 */
inline bool lessGUID(const GUID &inValL, const GUID &inValR)
{
    return ::memcmp(&inValL, &inValR, sizeof(inValR)) < 0;
}

//==============================================================================
/**
 *	equal()	Compared two GUIDs
 *
 *	A compares two GUIDs
 *
 *	@param	<GUID>	inGUID1	The GUID to compare to the second
 *	@param	<GUID>	inGUID2 The GUID to compare to the first
 *
 *	@return	<bool>	true if GUID1 and GUID2 are equal, otherwise false.
 */
inline bool equalGUID(const GUID &inValL, const GUID &inValR)
{
    return ::memcmp(&inValL, &inValR, sizeof(inValR)) == 0;
}

//==============================================================================
/**
 *	@class	CGUIDCompare
 *	@brief	A class that enacapsulates the compare operator for two GUIDs.
 */
class CGUIDCompare
{
public:
    //==============================================================================
    /*
    *	operator()	Compared two GUIDs
    *
    *	A compares two GUIDs
    *
    *	@param	<GUID>	inGUID1	The GUID to compare to the second
    *	@param	<GUID>	inGUID2 The GUID to compare to the first
    *
    *	@return	<bool>	true if GUID1 and GUID2 are equal, otherwise false.
    */
    bool operator()(const GUID &inValL, const GUID &inValR) const
    {
        return lessGUID(inValL, inValR);
    }
};

class CGUIDTools
{
public:
    //==============================================================================
    /**
    *	Prepeare to generate GUIDs.  If this method is not called the GUIDs will
    *	but evil GUIDs that are the same evertime the program runs.
    *	@param	outGuid Returns the generated GUID
    */
    static void Initialize()
    {
        // Q3DStudio::CTime theTime( true );
        //::srand( static_cast<unsigned int>( theTime.GetMilliSecs( ) ) );
    }

    //==============================================================================
    /**
    *	GenerateGuid
    *	@note	This is not a GUID.  Correct creation involves spatial uniqueness and more:
    *	http://hegel.ittc.ukans.edu/topics/internet/internet-drafts/draft-l/draft-leach-uuids-guids-00.txt
    *	@param	outGuid Returns the generated GUID
    */
    static void GenerateGuid(GUID &outGuid)
    {
        // Debug - non-random GUIDs
        /*		static long		theGuidCount = 1;
                        GUID			theGuid = {0};

                        theGuid.Data1 = theGuidCount;

                        ++theGuidCount;
                        outGuid = theGuid;*/

        // ::rand is 16 bits, GUID is 128 bits, smear random bits over the whole GUID
        // for ( long theSlot = 0; theSlot < sizeof(GUID)/sizeof(short); ++theSlot )
        //	reinterpret_cast<short*>( &outGuid )[theSlot] = static_cast<short>( ::rand() );

        Q3DStudio::CId theID;
        theID.Generate();
        outGuid = theID.Convert();
    }

    static GUID FromString(const Q3DStudio::CString &inGuidStr)
    {
        GUID theGUID = { 0, 0, 0, "" };
        // unsigned long theData[8];
        // const char* theBuffer = inGuidStr.GetCharStar( );
        //
        //// All of the %02X formats get scaned into a 32bit long so we have to convert down
        ///below....
        //::sscanf( theBuffer, "{%08x-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x}",
        //																	&theGUID.Data1,
        //																	&theGUID.Data2,
        //																	&theGUID.Data3,
        //																	&theData[0],
        //																	&theData[1],
        //																	&theData[2],
        //																	&theData[3],
        //																	&theData[4],
        //																	&theData[5],
        //																	&theData[6],
        //																	&theData[7]);

        //// This is where we convert the 32 values into unsigned chars.
        // for ( long theIndex = 0; theIndex < 8; ++theIndex )
        //	theGUID.Data4[theIndex] = static_cast<unsigned char>( theData[theIndex] );

        Q3DStudio::CId theID;
        theID.FromString(inGuidStr);
        theGUID = theID.Convert();

        return theGUID;
    }

    static Q3DStudio::CString ToString(REFGUID inGuid)
    {
        Q3DStudio::CString theGuidStr;

        // theGuidStr.Format( _LSTR( "{%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}" ),
        //										inGuid.Data1,
        //										inGuid.Data2,
        //										inGuid.Data3,
        //										inGuid.Data4[0],
        //										(unsigned long)
        //inGuid.Data4[1],
        //										(unsigned long)
        //inGuid.Data4[2],
        //										(unsigned long)
        //inGuid.Data4[3],
        //										(unsigned long)
        //inGuid.Data4[4],
        //										(unsigned long)
        //inGuid.Data4[5],
        //										(unsigned long)
        //inGuid.Data4[6],
        //										(unsigned long)
        //inGuid.Data4[7] );

        Q3DStudio::CId theID(inGuid);
        theGuidStr = theID.ToString();

        return theGuidStr;
    }
};