Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
20 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef DATAMAP_H
  8. #define DATAMAP_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #ifndef VECTOR_H
  13. #include "mathlib/vector.h"
  14. #endif
  15. #include "tier1/utlvector.h"
  16. #include "tier0/memdbgon.h"
  17. // SINGLE_INHERITANCE restricts the size of CBaseEntity pointers-to-member-functions to 4 bytes
  18. class SINGLE_INHERITANCE CBaseEntity;
  19. struct inputdata_t;
  20. #define INVALID_TIME (FLT_MAX * -1.0) // Special value not rebased on save/load
  21. typedef enum _fieldtypes
  22. {
  23. FIELD_VOID = 0, // No type or value
  24. FIELD_FLOAT, // Any floating point value
  25. FIELD_STRING, // A string ID (return from ALLOC_STRING)
  26. FIELD_VECTOR, // Any vector, QAngle, or AngularImpulse
  27. FIELD_QUATERNION, // A quaternion
  28. FIELD_INTEGER, // Any integer or enum
  29. FIELD_BOOLEAN, // boolean, implemented as an int, I may use this as a hint for compression
  30. FIELD_SHORT, // 2 byte integer
  31. FIELD_CHARACTER, // a byte
  32. FIELD_COLOR32, // 8-bit per channel r,g,b,a (32bit color)
  33. FIELD_EMBEDDED, // an embedded object with a datadesc, recursively traverse and embedded class/structure based on an additional typedescription
  34. FIELD_CUSTOM, // special type that contains function pointers to it's read/write/parse functions
  35. FIELD_CLASSPTR, // CBaseEntity *
  36. FIELD_EHANDLE, // Entity handle
  37. FIELD_EDICT, // edict_t *
  38. FIELD_POSITION_VECTOR, // A world coordinate (these are fixed up across level transitions automagically)
  39. FIELD_TIME, // a floating point time (these are fixed up automatically too!)
  40. FIELD_TICK, // an integer tick count( fixed up similarly to time)
  41. FIELD_MODELNAME, // Engine string that is a model name (needs precache)
  42. FIELD_SOUNDNAME, // Engine string that is a sound name (needs precache)
  43. FIELD_INPUT, // a list of inputed data fields (all derived from CMultiInputVar)
  44. FIELD_FUNCTION, // A class function pointer (Think, Use, etc)
  45. FIELD_VMATRIX, // a vmatrix (output coords are NOT worldspace)
  46. // NOTE: Use float arrays for local transformations that don't need to be fixed up.
  47. FIELD_VMATRIX_WORLDSPACE,// A VMatrix that maps some local space to world space (translation is fixed up on level transitions)
  48. FIELD_MATRIX3X4_WORLDSPACE, // matrix3x4_t that maps some local space to world space (translation is fixed up on level transitions)
  49. FIELD_INTERVAL, // a start and range floating point interval ( e.g., 3.2->3.6 == 3.2 and 0.4 )
  50. FIELD_MODELINDEX, // a model index
  51. FIELD_MATERIALINDEX, // a material index (using the material precache string table)
  52. FIELD_VECTOR2D, // 2 floats
  53. FIELD_TYPECOUNT, // MUST BE LAST
  54. } fieldtype_t;
  55. //-----------------------------------------------------------------------------
  56. // Field sizes...
  57. //-----------------------------------------------------------------------------
  58. template <int FIELD_TYPE>
  59. class CDatamapFieldSizeDeducer
  60. {
  61. public:
  62. enum
  63. {
  64. SIZE = 0
  65. };
  66. static int FieldSize( )
  67. {
  68. return 0;
  69. }
  70. };
  71. #define DECLARE_FIELD_SIZE( _fieldType, _fieldSize ) \
  72. template< > class CDatamapFieldSizeDeducer<_fieldType> { public: enum { SIZE = _fieldSize }; static int FieldSize() { return _fieldSize; } };
  73. #define FIELD_SIZE( _fieldType ) CDatamapFieldSizeDeducer<_fieldType>::SIZE
  74. #define FIELD_BITS( _fieldType ) (FIELD_SIZE( _fieldType ) * 8)
  75. DECLARE_FIELD_SIZE( FIELD_FLOAT, sizeof(float) )
  76. DECLARE_FIELD_SIZE( FIELD_STRING, sizeof(int) )
  77. DECLARE_FIELD_SIZE( FIELD_VECTOR, 3 * sizeof(float) )
  78. DECLARE_FIELD_SIZE( FIELD_VECTOR2D, 2 * sizeof(float) )
  79. DECLARE_FIELD_SIZE( FIELD_QUATERNION, 4 * sizeof(float))
  80. DECLARE_FIELD_SIZE( FIELD_INTEGER, sizeof(int))
  81. DECLARE_FIELD_SIZE( FIELD_BOOLEAN, sizeof(char))
  82. DECLARE_FIELD_SIZE( FIELD_SHORT, sizeof(short))
  83. DECLARE_FIELD_SIZE( FIELD_CHARACTER, sizeof(char))
  84. DECLARE_FIELD_SIZE( FIELD_COLOR32, sizeof(int))
  85. DECLARE_FIELD_SIZE( FIELD_CLASSPTR, sizeof(int))
  86. DECLARE_FIELD_SIZE( FIELD_EHANDLE, sizeof(int))
  87. DECLARE_FIELD_SIZE( FIELD_EDICT, sizeof(int))
  88. DECLARE_FIELD_SIZE( FIELD_POSITION_VECTOR, 3 * sizeof(float))
  89. DECLARE_FIELD_SIZE( FIELD_TIME, sizeof(float))
  90. DECLARE_FIELD_SIZE( FIELD_TICK, sizeof(int))
  91. DECLARE_FIELD_SIZE( FIELD_MODELNAME, sizeof(int))
  92. DECLARE_FIELD_SIZE( FIELD_SOUNDNAME, sizeof(int))
  93. DECLARE_FIELD_SIZE( FIELD_INPUT, sizeof(int))
  94. #ifdef POSIX
  95. // pointer to members under gnuc are 8bytes if you have a virtual func
  96. DECLARE_FIELD_SIZE( FIELD_FUNCTION, sizeof(uint64))
  97. #else
  98. DECLARE_FIELD_SIZE( FIELD_FUNCTION, sizeof(int *))
  99. #endif
  100. DECLARE_FIELD_SIZE( FIELD_VMATRIX, 16 * sizeof(float))
  101. DECLARE_FIELD_SIZE( FIELD_VMATRIX_WORLDSPACE, 16 * sizeof(float))
  102. DECLARE_FIELD_SIZE( FIELD_MATRIX3X4_WORLDSPACE, 12 * sizeof(float))
  103. DECLARE_FIELD_SIZE( FIELD_INTERVAL, 2 * sizeof( float) ) // NOTE: Must match interval.h definition
  104. DECLARE_FIELD_SIZE( FIELD_MODELINDEX, sizeof(int) )
  105. DECLARE_FIELD_SIZE( FIELD_MATERIALINDEX, sizeof(int) )
  106. #define ARRAYSIZE2D(p) (sizeof(p)/sizeof(p[0][0]))
  107. #define SIZE_OF_ARRAY(p) _ARRAYSIZE(p)
  108. // Normal offset of is invalid on non-array-types, this is dubious as hell. The rest of the codebase converted to the
  109. // legit offsetof from the C headers, so we'll use the old impl here to avoid exposing temptation to others
  110. #define _hacky_datamap_offsetof(s,m) ((size_t)&(((s *)0)->m))
  111. #define _FIELD(name,fieldtype,count,flags,mapname,tolerance) { fieldtype, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, count, flags, mapname, NULL, NULL, NULL, sizeof( ((classNameTypedef *)0)->name ), NULL, 0, tolerance }
  112. #define DEFINE_FIELD_NULL { FIELD_VOID,0, {0,0},0,0,0,0,0,0}
  113. #define DEFINE_FIELD(name,fieldtype) _FIELD(name, fieldtype, 1, FTYPEDESC_SAVE, NULL, 0 )
  114. #define DEFINE_KEYFIELD(name,fieldtype, mapname) _FIELD(name, fieldtype, 1, FTYPEDESC_KEY | FTYPEDESC_SAVE, mapname, 0 )
  115. #define DEFINE_KEYFIELD_NOT_SAVED(name,fieldtype, mapname)_FIELD(name, fieldtype, 1, FTYPEDESC_KEY, mapname, 0 )
  116. #define DEFINE_AUTO_ARRAY(name,fieldtype) _FIELD(name, fieldtype, SIZE_OF_ARRAY(((classNameTypedef *)0)->name), FTYPEDESC_SAVE, NULL, 0 )
  117. #define DEFINE_AUTO_ARRAY_KEYFIELD(name,fieldtype,mapname) _FIELD(name, fieldtype, SIZE_OF_ARRAY(((classNameTypedef *)0)->name), FTYPEDESC_SAVE, mapname, 0 )
  118. #define DEFINE_ARRAY(name,fieldtype, count) _FIELD(name, fieldtype, count, FTYPEDESC_SAVE, NULL, 0 )
  119. #define DEFINE_ENTITY_FIELD(name,fieldtype) _FIELD(edict_t, name, fieldtype, 1, FTYPEDESC_KEY | FTYPEDESC_SAVE, #name, 0 )
  120. #define DEFINE_ENTITY_GLOBAL_FIELD(name,fieldtype) _FIELD(edict_t, name, fieldtype, 1, FTYPEDESC_KEY | FTYPEDESC_SAVE | FTYPEDESC_GLOBAL, #name, 0 )
  121. #define DEFINE_GLOBAL_FIELD(name,fieldtype) _FIELD(name, fieldtype, 1, FTYPEDESC_GLOBAL | FTYPEDESC_SAVE, NULL, 0 )
  122. #define DEFINE_GLOBAL_KEYFIELD(name,fieldtype, mapname) _FIELD(name, fieldtype, 1, FTYPEDESC_GLOBAL | FTYPEDESC_KEY | FTYPEDESC_SAVE, mapname, 0 )
  123. #define DEFINE_CUSTOM_FIELD(name,datafuncs) { FIELD_CUSTOM, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE, NULL, datafuncs, NULL }
  124. #define DEFINE_CUSTOM_KEYFIELD(name,datafuncs,mapname) { FIELD_CUSTOM, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE | FTYPEDESC_KEY, mapname, datafuncs, NULL }
  125. #define DEFINE_AUTO_ARRAY2D(name,fieldtype) _FIELD(name, fieldtype, ARRAYSIZE2D(((classNameTypedef *)0)->name), FTYPEDESC_SAVE, NULL, 0 )
  126. // Used by byteswap datadescs
  127. #define DEFINE_BITFIELD(name,fieldtype,bitcount) DEFINE_ARRAY(name,fieldtype,((bitcount+FIELD_BITS(fieldtype)-1)&~(FIELD_BITS(fieldtype)-1)) / FIELD_BITS(fieldtype) )
  128. #define DEFINE_INDEX(name,fieldtype) _FIELD(name, fieldtype, 1, FTYPEDESC_INDEX, NULL, 0 )
  129. #define DEFINE_EMBEDDED( name ) \
  130. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE, NULL, NULL, NULL, &(((classNameTypedef *)0)->name.m_DataMap), sizeof( ((classNameTypedef *)0)->name ), NULL, 0, 0.0f }
  131. #define DEFINE_EMBEDDED_OVERRIDE( name, overridetype ) \
  132. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE, NULL, NULL, NULL, &((overridetype *)0)->m_DataMap, sizeof( ((classNameTypedef *)0)->name ), NULL, 0, 0.0f }
  133. #define DEFINE_EMBEDDEDBYREF( name ) \
  134. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE | FTYPEDESC_PTR, NULL, NULL, NULL, &(((classNameTypedef *)0)->name->m_DataMap), sizeof( *(((classNameTypedef *)0)->name) ), NULL, 0, 0.0f }
  135. #define DEFINE_EMBEDDED_ARRAY( name, count ) \
  136. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, count, FTYPEDESC_SAVE, NULL, NULL, NULL, &(((classNameTypedef *)0)->name->m_DataMap), sizeof( ((classNameTypedef *)0)->name[0] ), NULL, 0, 0.0f }
  137. #define DEFINE_EMBEDDED_AUTO_ARRAY( name ) \
  138. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, SIZE_OF_ARRAY( ((classNameTypedef *)0)->name ), FTYPEDESC_SAVE, NULL, NULL, NULL, &(((classNameTypedef *)0)->name->m_DataMap), sizeof( ((classNameTypedef *)0)->name[0] ), NULL, 0, 0.0f }
  139. #ifndef NO_ENTITY_PREDICTION
  140. #define DEFINE_PRED_TYPEDESCRIPTION( name, fieldtype ) \
  141. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE, NULL, NULL, NULL, &fieldtype::m_PredMap }
  142. #define DEFINE_PRED_TYPEDESCRIPTION_PTR( name, fieldtype ) \
  143. { FIELD_EMBEDDED, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_SAVE | FTYPEDESC_PTR, NULL, NULL, NULL, &fieldtype::m_PredMap }
  144. #else
  145. #define DEFINE_PRED_TYPEDESCRIPTION( name, fieldtype ) DEFINE_FIELD_NULL
  146. #define DEFINE_PRED_TYPEDESCRIPTION_PTR( name, fieldtype ) DEFINE_FIELD_NULL
  147. #endif
  148. // Extensions to datamap.h macros for predicted entities only
  149. #define DEFINE_PRED_FIELD(name,fieldtype, flags) _FIELD(name, fieldtype, 1, flags, NULL, 0.0f )
  150. #define DEFINE_PRED_ARRAY(name,fieldtype, count,flags) _FIELD(name, fieldtype, count, flags, NULL, 0.0f )
  151. #define DEFINE_FIELD_NAME(localname,netname,fieldtype) _FIELD(localname, fieldtype, 1, 0, #netname, 0.0f )
  152. // Predictable macros, which include a tolerance for floating point values...
  153. #define DEFINE_PRED_FIELD_TOL(name,fieldtype, flags,tolerance) _FIELD(name, fieldtype, 1, flags, NULL, tolerance )
  154. #define DEFINE_PRED_ARRAY_TOL(name,fieldtype, count,flags,tolerance) _FIELD(name, fieldtype, count, flags, NULL, tolerance)
  155. #define DEFINE_FIELD_NAME_TOL(localname,netname,fieldtolerance) _FIELD(localname, fieldtype, 1, 0, #netname, tolerance )
  156. //#define DEFINE_DATA( name, fieldextname, flags ) _FIELD(name, fieldtype, 1, flags, extname )
  157. // INPUTS
  158. #define DEFINE_INPUT( name, fieldtype, inputname ) { fieldtype, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_INPUT | FTYPEDESC_SAVE | FTYPEDESC_KEY, inputname, NULL, NULL, NULL, sizeof( ((classNameTypedef *)0)->name ) }
  159. #define DEFINE_INPUTFUNC( fieldtype, inputname, inputfunc ) { fieldtype, #inputfunc, { NULL, NULL }, 1, FTYPEDESC_INPUT, inputname, NULL, static_cast <inputfunc_t> (&classNameTypedef::inputfunc) }
  160. // OUTPUTS
  161. // the variable 'name' MUST BE derived from CBaseOutput
  162. // we know the output type from the variable itself, so it doesn't need to be specified here
  163. class ISaveRestoreOps;
  164. extern ISaveRestoreOps *eventFuncs;
  165. #define DEFINE_OUTPUT( name, outputname ) { FIELD_CUSTOM, #name, { (int)_hacky_datamap_offsetof(classNameTypedef, name), 0 }, 1, FTYPEDESC_OUTPUT | FTYPEDESC_SAVE | FTYPEDESC_KEY, outputname, eventFuncs }
  166. // replaces EXPORT table for portability and non-DLL based systems (xbox)
  167. #define DEFINE_FUNCTION_RAW( function, func_type ) { FIELD_VOID, nameHolder.GenerateName(#function), { NULL, NULL }, 1, FTYPEDESC_FUNCTIONTABLE, NULL, NULL, (inputfunc_t)((func_type)(&classNameTypedef::function)) }
  168. #define DEFINE_FUNCTION( function ) DEFINE_FUNCTION_RAW( function, inputfunc_t )
  169. #define FTYPEDESC_GLOBAL 0x0001 // This field is masked for global entity save/restore
  170. #define FTYPEDESC_SAVE 0x0002 // This field is saved to disk
  171. #define FTYPEDESC_KEY 0x0004 // This field can be requested and written to by string name at load time
  172. #define FTYPEDESC_INPUT 0x0008 // This field can be written to by string name at run time, and a function called
  173. #define FTYPEDESC_OUTPUT 0x0010 // This field propogates it's value to all targets whenever it changes
  174. #define FTYPEDESC_FUNCTIONTABLE 0x0020 // This is a table entry for a member function pointer
  175. #define FTYPEDESC_PTR 0x0040 // This field is a pointer, not an embedded object
  176. #define FTYPEDESC_OVERRIDE 0x0080 // The field is an override for one in a base class (only used by prediction system for now)
  177. // Flags used by other systems (e.g., prediction system)
  178. #define FTYPEDESC_INSENDTABLE 0x0100 // This field is present in a network SendTable
  179. #define FTYPEDESC_PRIVATE 0x0200 // The field is local to the client or server only (not referenced by prediction code and not replicated by networking)
  180. #define FTYPEDESC_NOERRORCHECK 0x0400 // The field is part of the prediction typedescription, but doesn't get compared when checking for errors
  181. #define FTYPEDESC_MODELINDEX 0x0800 // The field is a model index (used for debugging output)
  182. #define FTYPEDESC_INDEX 0x1000 // The field is an index into file data, used for byteswapping.
  183. // These flags apply to C_BasePlayer derived objects only
  184. #define FTYPEDESC_VIEW_OTHER_PLAYER 0x2000 // By default you can only view fields on the local player (yourself),
  185. // but if this is set, then we allow you to see fields on other players
  186. #define FTYPEDESC_VIEW_OWN_TEAM 0x4000 // Only show this data if the player is on the same team as the local player
  187. #define FTYPEDESC_VIEW_NEVER 0x8000 // Never show this field to anyone, even the local player (unusual)
  188. #define TD_MSECTOLERANCE 0.001f // This is a FIELD_FLOAT and should only be checked to be within 0.001 of the networked info
  189. struct typedescription_t;
  190. class ISaveRestoreOps;
  191. //
  192. // Function prototype for all input handlers.
  193. //
  194. typedef void (CBaseEntity::*inputfunc_t)(inputdata_t &data);
  195. struct datamap_t;
  196. struct typedescription_t;
  197. enum
  198. {
  199. TD_OFFSET_NORMAL = 0,
  200. TD_OFFSET_PACKED = 1,
  201. // Must be last
  202. TD_OFFSET_COUNT,
  203. };
  204. struct typedescription_t
  205. {
  206. fieldtype_t fieldType;
  207. const char *fieldName;
  208. int fieldOffset[ TD_OFFSET_COUNT ]; // 0 == normal, 1 == packed offset
  209. unsigned short fieldSize;
  210. short flags;
  211. // the name of the variable in the map/fgd data, or the name of the action
  212. const char *externalName;
  213. // pointer to the function set for save/restoring of custom data types
  214. ISaveRestoreOps *pSaveRestoreOps;
  215. // for associating function with string names
  216. inputfunc_t inputFunc;
  217. // For embedding additional datatables inside this one
  218. datamap_t *td;
  219. // Stores the actual member variable size in bytes
  220. int fieldSizeInBytes;
  221. // FTYPEDESC_OVERRIDE point to first baseclass instance if chains_validated has occurred
  222. struct typedescription_t *override_field;
  223. // Used to track exclusion of baseclass fields
  224. int override_count;
  225. // Tolerance for field errors for float fields
  226. float fieldTolerance;
  227. };
  228. //-----------------------------------------------------------------------------
  229. // Purpose: stores the list of objects in the hierarchy
  230. // used to iterate through an object's data descriptions
  231. //-----------------------------------------------------------------------------
  232. struct datamap_t
  233. {
  234. typedescription_t *dataDesc;
  235. int dataNumFields;
  236. char const *dataClassName;
  237. datamap_t *baseMap;
  238. bool chains_validated;
  239. // Have the "packed" offsets been computed
  240. bool packed_offsets_computed;
  241. int packed_size;
  242. #if defined( _DEBUG )
  243. bool bValidityChecked;
  244. #endif // _DEBUG
  245. };
  246. //-----------------------------------------------------------------------------
  247. //
  248. // Macros used to implement datadescs
  249. //
  250. #define DECLARE_SIMPLE_DATADESC() \
  251. static datamap_t m_DataMap; \
  252. static datamap_t *GetBaseMap(); \
  253. template <typename T> friend void DataMapAccess(T *, datamap_t **p); \
  254. template <typename T> friend datamap_t *DataMapInit(T *);
  255. #define DECLARE_DATADESC() \
  256. DECLARE_SIMPLE_DATADESC() \
  257. virtual datamap_t *GetDataDescMap( void );
  258. #define BEGIN_DATADESC( className ) \
  259. datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
  260. datamap_t *className::GetDataDescMap( void ) { return &m_DataMap; } \
  261. datamap_t *className::GetBaseMap() { datamap_t *pResult; DataMapAccess((BaseClass *)NULL, &pResult); return pResult; } \
  262. BEGIN_DATADESC_GUTS( className )
  263. #define BEGIN_DATADESC_NO_BASE( className ) \
  264. datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
  265. datamap_t *className::GetDataDescMap( void ) { return &m_DataMap; } \
  266. datamap_t *className::GetBaseMap() { return NULL; } \
  267. BEGIN_DATADESC_GUTS( className )
  268. #define BEGIN_SIMPLE_DATADESC( className ) \
  269. datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
  270. datamap_t *className::GetBaseMap() { return NULL; } \
  271. BEGIN_DATADESC_GUTS( className )
  272. #define BEGIN_SIMPLE_DATADESC_( className, BaseClass ) \
  273. datamap_t className::m_DataMap = { 0, 0, #className, NULL }; \
  274. datamap_t *className::GetBaseMap() { datamap_t *pResult; DataMapAccess((BaseClass *)NULL, &pResult); return pResult; } \
  275. BEGIN_DATADESC_GUTS( className )
  276. #define BEGIN_DATADESC_GUTS( className ) \
  277. template <typename T> datamap_t *DataMapInit(T *); \
  278. template <> datamap_t *DataMapInit<className>( className * ); \
  279. namespace className##_DataDescInit \
  280. { \
  281. datamap_t *g_DataMapHolder = DataMapInit( (className *)NULL ); /* This can/will be used for some clean up duties later */ \
  282. } \
  283. \
  284. template <> datamap_t *DataMapInit<className>( className * ) \
  285. { \
  286. typedef className classNameTypedef; \
  287. static CDatadescGeneratedNameHolder nameHolder(#className); \
  288. className::m_DataMap.baseMap = className::GetBaseMap(); \
  289. static typedescription_t dataDesc[] = \
  290. { \
  291. { FIELD_VOID,0, {0,0},0,0,0,0,0,0}, /* so you can define "empty" tables */
  292. #define END_DATADESC() \
  293. }; \
  294. \
  295. if ( sizeof( dataDesc ) > sizeof( dataDesc[0] ) ) \
  296. { \
  297. classNameTypedef::m_DataMap.dataNumFields = SIZE_OF_ARRAY( dataDesc ) - 1; \
  298. classNameTypedef::m_DataMap.dataDesc = &dataDesc[1]; \
  299. } \
  300. else \
  301. { \
  302. classNameTypedef::m_DataMap.dataNumFields = 1; \
  303. classNameTypedef::m_DataMap.dataDesc = dataDesc; \
  304. } \
  305. return &classNameTypedef::m_DataMap; \
  306. }
  307. // used for when there is no data description
  308. #define IMPLEMENT_NULL_SIMPLE_DATADESC( derivedClass ) \
  309. BEGIN_SIMPLE_DATADESC( derivedClass ) \
  310. END_DATADESC()
  311. #define IMPLEMENT_NULL_SIMPLE_DATADESC_( derivedClass, baseClass ) \
  312. BEGIN_SIMPLE_DATADESC_( derivedClass, baseClass ) \
  313. END_DATADESC()
  314. #define IMPLEMENT_NULL_DATADESC( derivedClass ) \
  315. BEGIN_DATADESC( derivedClass ) \
  316. END_DATADESC()
  317. // helps get the offset of a bitfield
  318. #define BEGIN_BITFIELD( name ) \
  319. union \
  320. { \
  321. char name; \
  322. struct \
  323. {
  324. #define END_BITFIELD() \
  325. }; \
  326. };
  327. //-----------------------------------------------------------------------------
  328. // Forward compatability with potential seperate byteswap datadescs
  329. #define DECLARE_BYTESWAP_DATADESC() DECLARE_SIMPLE_DATADESC()
  330. #define BEGIN_BYTESWAP_DATADESC(name) BEGIN_SIMPLE_DATADESC(name)
  331. #define BEGIN_BYTESWAP_DATADESC_(name,base) BEGIN_SIMPLE_DATADESC_(name,base)
  332. #define END_BYTESWAP_DATADESC() END_DATADESC()
  333. //-----------------------------------------------------------------------------
  334. template <typename T>
  335. inline void DataMapAccess(T *ignored, datamap_t **p)
  336. {
  337. *p = &T::m_DataMap;
  338. }
  339. //-----------------------------------------------------------------------------
  340. class CDatadescGeneratedNameHolder
  341. {
  342. public:
  343. CDatadescGeneratedNameHolder( const char *pszBase )
  344. : m_pszBase(pszBase)
  345. {
  346. m_nLenBase = strlen( m_pszBase );
  347. }
  348. ~CDatadescGeneratedNameHolder()
  349. {
  350. for ( int i = 0; i < m_Names.Count(); i++ )
  351. {
  352. delete m_Names[i];
  353. }
  354. }
  355. const char *GenerateName( const char *pszIdentifier )
  356. {
  357. char *pBuf = new char[m_nLenBase + strlen(pszIdentifier) + 1];
  358. strcpy( pBuf, m_pszBase );
  359. strcat( pBuf, pszIdentifier );
  360. m_Names.AddToTail( pBuf );
  361. return pBuf;
  362. }
  363. private:
  364. const char *m_pszBase;
  365. size_t m_nLenBase;
  366. CUtlVector<char *> m_Names;
  367. };
  368. //-----------------------------------------------------------------------------
  369. #include "tier0/memdbgoff.h"
  370. #endif // DATAMAP_H