summaryrefslogtreecommitdiffstats
path: root/src/dm/systems/HandleSystemBase.h
blob: cb8e0f6c65a3c0d1ec5cddd0a4339025a9c2c898 (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
/****************************************************************************
**
** Copyright (C) 1993-2009 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
#ifndef HANDLESYSTEMIMPLH
#define HANDLESYSTEMIMPLH

#include <unordered_map>

namespace qt3dsdm {

class CHandleObject
{
public:
    // Type of CHandleObject
    // Each subclass needs to specify what type it is
    // This is used to avoid dynamic_cast (RTTI) which is expensive
    enum EHandleObjectType {
        EHandleObjectTypeUnknown = 0,
        EHandleObjectTypeCDataModelInstance,
        EHandleObjectTypeCDataModelPropertyDefinitionObject,
        EHandleObjectTypeSAnimationTrack,
        EHandleObjectTypeSKeyframe,
        EHandleObjectTypeSSlide,
        EHandleObjectTypeSSlideGraph,
        EHandleObjectTypeAction,
        EHandleObjectTypeActionHandlerArgument,
        EHandleObjectTypeCustomProperty,
        EHandleObjectTypeEvent,
        EHandleObjectTypeCustomHandler,
        EHandleObjectTypeHandlerParam,
        EHandleObjectTypeEnd,
    };

    CHandleObject(int inHandle = 0)
        : m_Handle(inHandle)
    {
    }
    virtual ~CHandleObject() {}

    virtual EHandleObjectType GetType() = 0;

    int m_Handle;

private: //noncopyable
    CHandleObject(const CHandleObject&) = delete;
    CHandleObject& operator=(const CHandleObject&) = delete;

};

typedef std::shared_ptr<CHandleObject> THandleObjectPtr;

// Note that maps don't need to copy their objects.
typedef std::unordered_map<int, THandleObjectPtr> THandleObjectMap;
typedef std::pair<int, THandleObjectPtr> THandleObjectPair;

class IHandleBase
{
public:
    /**
     *	Check whether a given handle exists
     */
    virtual bool HandleValid(int inHandle) const = 0;
};

struct CHandleBase : public IHandleBase
{
    THandleObjectMap m_Objects;
    int m_NextId;

    CHandleBase()
        : m_NextId(1)
    {
    }
    CHandleBase(const CHandleBase &inOther)
        : m_Objects(inOther.m_Objects)
        , m_NextId(inOther.m_NextId)
    {
    }

    CHandleBase &operator=(const CHandleBase &inOther)
    {
        m_Objects = inOther.m_Objects;
        m_NextId = inOther.m_NextId;
        return *this;
    }

    // IHandleBase
    bool HandleValid(int inHandle) const override
    {
        return m_Objects.find(inHandle) != m_Objects.end();
    }

    template <typename T>
    static inline bool HandleObjectValid(int inHandle, const THandleObjectMap &inMap)
    {
        THandleObjectMap::const_iterator theIter = inMap.find(inHandle);
        if (theIter != inMap.end()) {
            if (theIter->second->GetType() == T::s_Type)
                return true;
        }
        return false;
    }

    template <typename T>
    static inline const T *GetHandleObject(int inHandle, const THandleObjectMap &inMap)
    {
        THandleObjectMap::const_iterator theIter = inMap.find(inHandle);
        if (theIter != inMap.end()) {
            if (theIter->second->GetType() == T::s_Type)
                return static_cast<const T *>(theIter->second.get());
        }
        return NULL;
    }

    template <typename ObjectType, typename HandleType>
    static inline void MaybeAddObject(const std::pair<int, THandleObjectPtr> &inItem,
                                      std::vector<HandleType> &outHandles)
    {
        if (inItem.second->GetType() == ObjectType::s_Type)
            outHandles.push_back(HandleType(inItem.first));
    }

    static void EraseHandle(int inHandle, THandleObjectMap &inObjects)
    {
        THandleObjectMap::iterator theFind = inObjects.find(inHandle);
        if (theFind != inObjects.end())
            inObjects.erase(theFind);
    }

    // Return the next unused id.  There are no guarantees whether positive or negative; what this
    // will
    // guarantee is that it isn't currently used in the object map and it is non-zero.
    int GetNextId()
    {
        do {
            ++m_NextId;
        } while (m_Objects.find(m_NextId) != m_Objects.end());
        return m_NextId;
    }
};
}

#endif