summaryrefslogtreecommitdiffstats
path: root/src/Runtime/ogl-runtime/src/foundation/Qt3DSDiscriminatedUnion.h
blob: 1957eb23936edc56f4ee07f94582be1c72191eee (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/****************************************************************************
**
** Copyright (C) 2008-2012 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$
** 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 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** 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$
**
****************************************************************************/

#ifndef QT3DS_FOUNDATION_DISCRIMINATED_UNION_H
#define QT3DS_FOUNDATION_DISCRIMINATED_UNION_H
#include "Qt3DSAssert.h"
#include "foundation/Qt3DSSimpleTypes.h"
#include "foundation/Qt3DSIntrinsics.h"
#include "foundation/Qt3DSUnionCast.h"

#ifdef WIN32
#pragma warning(disable : 4100)
#endif

namespace qt3ds {
namespace foundation {

    template <typename TUnionTraits, int TBufferSize>
    class DiscriminatedUnion
    {
    public:
        typedef DiscriminatedUnion<TUnionTraits, TBufferSize> TThisType;
        typedef TUnionTraits TTraits;
        typedef typename TTraits::TIdType TIdType;

    protected:
        char m_Data[TBufferSize];
        // Id type must include a no-data type.
        TIdType m_DataType;

    public:
        DiscriminatedUnion() { TTraits::defaultConstruct(m_Data, m_DataType); }

        DiscriminatedUnion(const TThisType &inOther)
            : m_DataType(inOther.m_DataType)
        {
            TTraits::copyConstruct(m_Data, inOther.m_Data, m_DataType);
        }

        template <typename TDataType>
        DiscriminatedUnion(const TDataType &inType)
        {
            TTraits::copyConstruct(m_Data, m_DataType, inType);
        }

        ~DiscriminatedUnion() { TTraits::destruct(m_Data, m_DataType); }

        TThisType &operator=(const TThisType &inType)
        {
            if (this != &inType) {
                TTraits::destruct(m_Data, m_DataType);
                m_DataType = inType.m_DataType;
                TTraits::copyConstruct(m_Data, inType.m_Data, inType.m_DataType);
            }
            return *this;
        }

        typename TTraits::TIdType getType() const { return m_DataType; }

        template <typename TDataType>
        const TDataType *getDataPtr() const
        {
            return TTraits::template getDataPtr<TDataType>(m_Data, m_DataType);
        }

        template <typename TDataType>
        TDataType *getDataPtr()
        {
            return TTraits::template getDataPtr<TDataType>(m_Data, m_DataType);
        }

        template <typename TDataType>
        TDataType getData() const
        {
            const TDataType *dataPtr = getDataPtr<TDataType>();
            if (dataPtr)
                return *dataPtr;
            QT3DS_ASSERT(false);
            return TDataType();
        }

        bool operator==(const TThisType &inOther) const
        {
            return m_DataType == inOther.m_DataType
                && TTraits::areEqual(m_Data, inOther.m_Data, m_DataType);
        }

        bool operator!=(const TThisType &inOther) const
        {
            return m_DataType != inOther.m_DataType
                || TTraits::areEqual(m_Data, inOther.m_Data, m_DataType) == false;
        }

        template <typename TRetType, typename TVisitorType>
        TRetType visit(TVisitorType inVisitor)
        {
            return TTraits::template visit<TRetType>(m_Data, m_DataType, inVisitor);
        }

        template <typename TRetType, typename TVisitorType>
        TRetType visit(TVisitorType inVisitor) const
        {
            return TTraits::template visit<TRetType>(m_Data, m_DataType, inVisitor);
        }
    };

    // Helper system to enable quicker and correct construction of union traits types

    struct CopyConstructVisitor
    {
        const char *m_Src;
        CopyConstructVisitor(const char *inSrc)
            : m_Src(inSrc)
        {
        }

        template <typename TDataType>
        void operator()(TDataType &inDst)
        {
            new (&inDst) TDataType(*NVUnionCast<const TDataType *>(m_Src));
        }
        void operator()() { QT3DS_ASSERT(false); }
    };

    template <typename TDataType>
    struct DestructTraits
    {
        void destruct(TDataType &inType) { inType.~TDataType(); }
    };

    // Until compilers improve a bit, you need this for POD types else you get
    // unused parameter warnings.
    template <>
    struct DestructTraits<QT3DSU8>
    {
        void destruct(QT3DSU8 &) {}
    };
    template <>
    struct DestructTraits<QT3DSI8>
    {
        void destruct(QT3DSI8 &) {}
    };
    template <>
    struct DestructTraits<QT3DSU16>
    {
        void destruct(QT3DSU16 &) {}
    };
    template <>
    struct DestructTraits<QT3DSI16>
    {
        void destruct(QT3DSI16 &) {}
    };
    template <>
    struct DestructTraits<QT3DSU32>
    {
        void destruct(QT3DSU32 &) {}
    };
    template <>
    struct DestructTraits<QT3DSI32>
    {
        void destruct(QT3DSI32 &) {}
    };
    template <>
    struct DestructTraits<QT3DSU64>
    {
        void destruct(QT3DSU64 &) {}
    };
    template <>
    struct DestructTraits<QT3DSI64>
    {
        void destruct(QT3DSI64 &) {}
    };
    template <>
    struct DestructTraits<QT3DSF32>
    {
        void destruct(QT3DSF32 &) {}
    };
    template <>
    struct DestructTraits<QT3DSF64>
    {
        void destruct(QT3DSF64 &) {}
    };
    template <>
    struct DestructTraits<bool>
    {
        void destruct(bool &) {}
    };
    template <>
    struct DestructTraits<void *>
    {
        void destruct(void *&) {}
    };
#ifdef __INTEGRITY
    template <>
    struct DestructTraits<QT3DSVec2>
    {
        void destruct(QT3DSVec2 &) {}
    };
    template <>
    struct DestructTraits<QT3DSVec3>
    {
        void destruct(QT3DSVec3 &) {}
    };
#endif

    struct DestructVisitor
    {
        template <typename TDataType>
        void operator()(TDataType &inDst)
        {
            DestructTraits<TDataType>().destruct(inDst);
        }
        void operator()() { QT3DS_ASSERT(false); }
    };

    template <typename TDataType>
    struct EqualVisitorTraits
    {
        bool operator()(const TDataType &lhs, const TDataType &rhs) { return lhs == rhs; }
    };

    struct EqualVisitor
    {
        const char *m_Rhs;
        EqualVisitor(const char *rhs)
            : m_Rhs(rhs)
        {
        }
        template <typename TDataType>
        bool operator()(const TDataType &lhs)
        {
            const TDataType &rhs(*NVUnionCast<const TDataType *>(m_Rhs));
            return EqualVisitorTraits<TDataType>()(lhs, rhs);
        }
        bool operator()()
        {
            QT3DS_ASSERT(false);
            return true;
        }
    };

    template <typename TBase, QT3DSU32 TBufferSize>
    struct DiscriminatedUnionGenericBase : public TBase
    {
        typedef typename TBase::TIdType TIdType;

        static void zeroBuf(char *outDst) { qt3ds::intrinsics::memZero(outDst, TBufferSize); }

        static void defaultConstruct(char *outDst, TIdType &outType)
        {
            zeroBuf(outDst);
            outType = TBase::getNoDataId();
        }

        template <typename TDataType>
        static void copyConstruct(char *outDst, TIdType &outType, const TDataType &inSrc)
        {
            zeroBuf(outDst);
            StaticAssert<sizeof(TDataType) <= TBufferSize>::valid_expression();
            outType = TBase::template getType<TDataType>();
            new (outDst) TDataType(inSrc);
        }

        static void copyConstruct(char *inDst, const char *inSrc, TIdType inType)
        {
            if (inType == TBase::getNoDataId())
                zeroBuf(inDst);
            else
                TBase::template visit<void>(inDst, inType, CopyConstructVisitor(inSrc));
        }

        static void destruct(char *inDst, TIdType inType)
        {
            if (inType != TBase::getNoDataId())
                TBase::template visit<void>(inDst, inType, DestructVisitor());
            zeroBuf(inDst);
        }

        template <typename TDataType>
        static const TDataType *getDataPtr(const char *inData, const TIdType &inType)
        {
            if (TBase::template getType<TDataType>() == inType)
                return NVUnionCast<const TDataType *>(inData);
            QT3DS_ASSERT(false);
            return NULL;
        }

        template <typename TDataType>
        static TDataType *getDataPtr(char *inData, const TIdType &inType)
        {
            if (TBase::template getType<TDataType>() == inType)
                return NVUnionCast<TDataType *>(inData);
            QT3DS_ASSERT(false);
            return NULL;
        }

        static bool areEqual(const char *inLhs, const char *inRhs, TIdType inType)
        {
            if (inType != TBase::getNoDataId())
                return TBase::template visit<bool>(inLhs, inType, EqualVisitor(inRhs));
            else
                return true;
        }
    };
}
}

#endif