forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmxattribute.h
183 lines (143 loc) · 5.41 KB
/
dmxattribute.h
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#ifndef DMXATTRIBUTE_H
#define DMXATTRIBUTE_H
#ifdef _WIN32
#pragma once
#endif
#include "datamodel/dmattributetypes.h"
#include "tier1/utlvector.h"
#include "tier1/utlrbtree.h"
#include "tier1/utlsymbol.h"
#include "tier1/mempool.h"
#include "dmxloader/dmxloader.h"
//-----------------------------------------------------------------------------
// Forward declarations:
//-----------------------------------------------------------------------------
class CDmxElement;
//-----------------------------------------------------------------------------
// Attribute info, modified for use in mod code
//-----------------------------------------------------------------------------
DECLARE_ATTRIBUTE_TYPE( CDmxElement*, AT_ELEMENT, "element", value = 0; )
DECLARE_ATTRIBUTE_ARRAY_TYPE( CDmxElement*, AT_ELEMENT_ARRAY, "element_array" )
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CDmxAttribute
{
DECLARE_DMX_ALLOCATOR( );
public:
// Returns attribute name and type
DmAttributeType_t GetType() const;
const char *GetTypeString() const;
template< class T > bool IsA() const;
// Returns the name. NOTE: The utlsymbol
// can be turned into a string by using g_pDataModel->String();
const char *GetName() const;
CUtlSymbol GetNameSymbol() const;
void SetName( const char *pName );
// Gets values
template< class T > const T& GetValue( ) const;
template< class T > const CUtlVector< T >& GetArray( ) const;
const char *GetValueString() const;
// Sets values (+ type)
template< class T > void SetValue( const T& value );
void SetValue( const char *pString );
void SetValue( const void *pBuffer, size_t nLen );
void SetValue( const CDmxAttribute *pAttribute );
// Method to set values in an array (just directly operate on the array)
// NOTE: This will create a new array of the appropriate type if
// the type doesn't match the current type
template< class T > CUtlVector< T >& GetArrayForEdit();
// Sets the attribute to its default value based on its type
void SetToDefaultValue();
// Convert to and from string
void SetValueFromString( const char *pValue );
const char *GetValueAsString( char *pBuffer, size_t nBufLen ) const;
// Gets the size of an array, returns 0 if it's not an array type
int GetArrayCount() const;
// Read from file
bool Unserialize( DmAttributeType_t type, CUtlBuffer &buf );
bool UnserializeElement( DmAttributeType_t type, CUtlBuffer &buf );
bool Serialize( CUtlBuffer &buf ) const;
bool SerializeElement( int nIndex, CUtlBuffer &buf ) const;
bool SerializesOnMultipleLines() const;
// Returns the size of the variables storing the various attribute types
static int AttributeDataSize( DmAttributeType_t type );
private:
CDmxAttribute( const char *pAttributeName );
CDmxAttribute( CUtlSymbol attributeName );
~CDmxAttribute();
// Allocate, free memory for data
void AllocateDataMemory( DmAttributeType_t type );
void FreeDataMemory( );
// Untyped method for setting used by unpack
void SetValue( DmAttributeType_t type, const void *pSrc, int nLen );
DmAttributeType_t m_Type;
CUtlSymbol m_Name;
void *m_pData;
static CUtlSymbolTableMT s_AttributeNameSymbols;
friend class CDmxElement;
};
//-----------------------------------------------------------------------------
// Inline methods
//-----------------------------------------------------------------------------
inline DmAttributeType_t CDmxAttribute::GetType() const
{
return m_Type;
}
template< class T > inline bool CDmxAttribute::IsA() const
{
return GetType() == CDmAttributeInfo< T >::ATTRIBUTE_TYPE;
}
inline CUtlSymbol CDmxAttribute::GetNameSymbol() const
{
return m_Name;
}
//-----------------------------------------------------------------------------
// Sets a value in the attribute
//-----------------------------------------------------------------------------
template< class T > void CDmxAttribute::SetValue( const T& value )
{
AllocateDataMemory( CDmAttributeInfo<T>::AttributeType() );
CopyConstruct( (T*)m_pData, value );
}
//-----------------------------------------------------------------------------
// Returns data in the attribute
//-----------------------------------------------------------------------------
inline const char *CDmxAttribute::GetValueString() const
{
if ( m_Type == AT_STRING )
return *(CUtlString*)m_pData;
return "";
}
template< class T >
inline const T& CDmxAttribute::GetValue( ) const
{
if ( CDmAttributeInfo<T>::AttributeType() == m_Type )
return *(T*)m_pData;
static T defaultValue;
CDmAttributeInfo<T>::SetDefaultValue( defaultValue );
return defaultValue;
}
template< class T >
inline const CUtlVector< T >& CDmxAttribute::GetArray( ) const
{
if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
return *( CUtlVector< T > *)m_pData;
static CUtlVector<T> defaultArray;
return defaultArray;
}
template< class T >
inline CUtlVector< T >& CDmxAttribute::GetArrayForEdit( )
{
if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
return *( CUtlVector< T > *)m_pData;
AllocateDataMemory( CDmAttributeInfo< CUtlVector< T > >::AttributeType() );
Construct( (CUtlVector<T>*)m_pData );
return *(CUtlVector< T > *)m_pData;
}
#endif // DMXATTRIBUTE_H