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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#ifndef DMEHANDLE_H
#define DMEHANDLE_H
#ifdef _WIN32
#pragma once
#endif
#include "datamodel/idatamodel.h"
#include "datamodel/dmelement.h"
#include "datamodel/dmattribute.h"
#include "datamodel/dmattributevar.h"
//-----------------------------------------------------------------------------
// Purpose: CDmeHandle is a templatized wrapper around DmElementHandle_t
//-----------------------------------------------------------------------------
template< class DmeType, bool Counted = false >
class CDmeHandle : public CDmeElementRefHelper
{
public:
CDmeHandle() : m_handle( DMELEMENT_HANDLE_INVALID )
{
}
explicit CDmeHandle( CDmElement *pObject ) : m_handle( DMELEMENT_HANDLE_INVALID )
{
Set( pObject );
}
CDmeHandle( DmElementHandle_t h ) : m_handle( DMELEMENT_HANDLE_INVALID )
{
Set( h );
}
CDmeHandle( const CDmeHandle< DmeType, Counted > &handle ) : m_handle( DMELEMENT_HANDLE_INVALID )
{
Set( handle.m_handle );
}
template < class T, bool B >
CDmeHandle( const CDmeHandle< T, B > &handle ) : m_handle( DMELEMENT_HANDLE_INVALID )
{
DmeType *p = ( T* )NULL; // triggers compiler error if converting from invalid handle type
NOTE_UNUSED( p );
Set( handle.GetHandle() );
}
~CDmeHandle()
{
if ( !g_pDataModel )
return; // some handles are static, and don't get destroyed until program termination
Unref( m_handle, Counted );
}
template < class T, bool B >
CDmeHandle& operator=( const CDmeHandle< T, B > &handle )
{
DmeType *p = ( T* )NULL; // triggers compiler error if converting from invalid handle type
NOTE_UNUSED( p );
Set( handle.GetHandle() );
return *this;
}
DmeType *Get()
{
return static_cast< DmeType* >( g_pDataModel->GetElement( m_handle ) );
}
const DmeType *Get() const
{
return static_cast< DmeType* >( g_pDataModel->GetElement( m_handle ) );
}
DmElementHandle_t GetHandle() const
{
return m_handle;
}
void Set( CDmElement *pObject )
{
Set( pObject ? pObject->GetHandle() : DMELEMENT_HANDLE_INVALID );
}
void Set( DmElementHandle_t h )
{
if ( h == m_handle )
return;
Unref( m_handle, Counted );
m_handle = h;
if ( h != DMELEMENT_HANDLE_INVALID )
{
CDmElement *pElement = g_pDataModel->GetElement( m_handle );
Assert( pElement );
if ( pElement && !pElement->IsA( DmeType::GetStaticTypeSymbol() ) )
{
m_handle = DMELEMENT_HANDLE_INVALID;
}
}
Ref( m_handle, Counted );
}
operator DmeType*()
{
return Get();
}
operator const DmeType*() const
{
return Get();
}
operator DmElementHandle_t() const
{
return m_handle;
}
DmeType* operator->()
{
return Get();
}
const DmeType* operator->() const
{
return Get();
}
CDmeHandle& operator=( DmElementHandle_t h )
{
Set( h );
return *this;
}
CDmeHandle& operator=( CDmElement *pObject )
{
Set( pObject );
return *this;
}
bool operator==( const CDmeHandle< DmeType > &h ) const
{
return m_handle == h.m_handle;
}
bool operator!=( const CDmeHandle< DmeType > &h ) const
{
return !operator==( h );
}
bool operator<( const CDmeHandle< DmeType > &h ) const
{
return m_handle < h.m_handle;
}
bool operator==( DmeType *pObject ) const
{
DmElementHandle_t h = pObject ? pObject->GetHandle() : DMELEMENT_HANDLE_INVALID;
return m_handle == h;
}
bool operator!=( DmeType *pObject ) const
{
return !operator==( pObject );
}
bool operator==( DmElementHandle_t h ) const
{
return ( m_handle == h );
}
bool operator!=( DmElementHandle_t h ) const
{
return ( m_handle != h );
}
operator bool() const
{
return ( Get() != NULL );
}
bool operator!() const
{
return ( Get() == NULL );
}
private:
DmElementHandle_t m_handle;
};
typedef CDmeHandle< CDmElement, true > CDmeCountedHandle;
//-----------------------------------------------------------------------------
// Vector of element handles
//-----------------------------------------------------------------------------
typedef CUtlVector< CDmeHandle<CDmElement> > DmeHandleVec_t;
//-----------------------------------------------------------------------------
// helper class for undo classes to allow them to hold onto refcounted element handles
//-----------------------------------------------------------------------------
template< typename T >
class CDmAttributeUndoStorageType
{
public:
typedef T UndoStorageType;
};
template<>
class CDmAttributeUndoStorageType< DmElementHandle_t >
{
public:
typedef CDmeCountedHandle UndoStorageType;
};
template<>
class CDmAttributeUndoStorageType< CUtlVector< DmElementHandle_t > >
{
public:
typedef CUtlVector< CDmeCountedHandle > UndoStorageType;
};
#endif // DMEHANDLE_H
|