Counter Strike : Global Offensive Source Code
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.

198 lines
4.2 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef SAVERESTORE_UTLMAP_H
  7. #define SAVERESTORE_UTLMAP_H
  8. #include "utlmap.h"
  9. #include "saverestore_utlrbtree.h"
  10. #if defined( _WIN32 )
  11. #pragma once
  12. #endif
  13. template <class UTLMAP, int KEY_TYPE, int FIELD_TYPE>
  14. class CUtlMapDataOps : public CDefSaveRestoreOps
  15. {
  16. public:
  17. CUtlMapDataOps()
  18. {
  19. // These COMPILE_TIME_ASSERT checks need to be in individual scopes to avoid build breaks
  20. // on MacOS and Linux due to a gcc bug.
  21. { UTLCLASS_SAVERESTORE_VALIDATE_TYPE( KEY_TYPE ); }
  22. { UTLCLASS_SAVERESTORE_VALIDATE_TYPE( FIELD_TYPE ); }
  23. }
  24. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  25. {
  26. datamap_t *pKeyDatamap = CTypedescDeducer<KEY_TYPE>::Deduce( (UTLMAP *)NULL );
  27. datamap_t *pFieldDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLMAP *)NULL );
  28. typedescription_t dataDesc[] =
  29. {
  30. {
  31. (fieldtype_t)KEY_TYPE,
  32. "K",
  33. 0,
  34. 1,
  35. FTYPEDESC_SAVE,
  36. NULL,
  37. NULL,
  38. NULL,
  39. pKeyDatamap,
  40. sizeof(KEY_TYPE),
  41. },
  42. {
  43. (fieldtype_t)FIELD_TYPE,
  44. "T",
  45. offsetof(typename UTLMAP::Node_t, elem),
  46. 1,
  47. FTYPEDESC_SAVE,
  48. NULL,
  49. NULL,
  50. NULL,
  51. pFieldDatamap,
  52. sizeof(FIELD_TYPE),
  53. }
  54. };
  55. datamap_t dataMap =
  56. {
  57. dataDesc,
  58. 2,
  59. "um",
  60. NULL,
  61. 0,
  62. NULL,
  63. #ifdef _DEBUG
  64. true
  65. #endif
  66. };
  67. typename UTLMAP::CTree *pUtlRBTree = ((UTLMAP *)fieldInfo.pField)->AccessTree();
  68. pSave->StartBlock();
  69. int nElems = pUtlRBTree->Count();
  70. pSave->WriteInt( &nElems, 1 );
  71. typename UTLMAP::CTree::IndexType_t i = pUtlRBTree->FirstInorder();
  72. while ( i != pUtlRBTree->InvalidIndex() )
  73. {
  74. typename UTLMAP::CTree::ElemType_t &elem = pUtlRBTree->Element( i );
  75. pSave->WriteAll( &elem, &dataMap );
  76. i = pUtlRBTree->NextInorder( i );
  77. }
  78. pSave->EndBlock();
  79. }
  80. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  81. {
  82. datamap_t *pKeyDatamap = CTypedescDeducer<KEY_TYPE>::Deduce( (UTLMAP *)NULL );
  83. datamap_t *pFieldDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLMAP *)NULL );
  84. typedescription_t dataDesc[] =
  85. {
  86. {
  87. (fieldtype_t)KEY_TYPE,
  88. "K",
  89. 0,
  90. 1,
  91. FTYPEDESC_SAVE,
  92. NULL,
  93. NULL,
  94. NULL,
  95. pKeyDatamap,
  96. sizeof(KEY_TYPE),
  97. },
  98. {
  99. (fieldtype_t)FIELD_TYPE,
  100. "T",
  101. offsetof(typename UTLMAP::Node_t, elem),
  102. 1,
  103. FTYPEDESC_SAVE,
  104. NULL,
  105. NULL,
  106. NULL,
  107. pFieldDatamap,
  108. sizeof(FIELD_TYPE),
  109. }
  110. };
  111. datamap_t dataMap =
  112. {
  113. dataDesc,
  114. 2,
  115. "um",
  116. NULL,
  117. 0,
  118. NULL,
  119. #ifdef _DEBUG
  120. true
  121. #endif
  122. };
  123. UTLMAP *pUtlMap = ((UTLMAP *)fieldInfo.pField);
  124. pRestore->StartBlock();
  125. int nElems = pRestore->ReadInt();
  126. typename UTLMAP::CTree::ElemType_t temp;
  127. while ( nElems-- )
  128. {
  129. pRestore->ReadAll( &temp, &dataMap );
  130. pUtlMap->Insert( temp.key, temp.elem );
  131. }
  132. pRestore->EndBlock();
  133. }
  134. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  135. {
  136. UTLMAP *pUtlMap = (UTLMAP *)fieldInfo.pField;
  137. pUtlMap->RemoveAll();
  138. }
  139. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  140. {
  141. UTLMAP *pUtlMap = (UTLMAP *)fieldInfo.pField;
  142. return ( pUtlMap->Count() == 0 );
  143. }
  144. };
  145. //-------------------------------------
  146. template <int KEYTYPE, int FIELD_TYPE>
  147. class CUtlMapDataopsInstantiator
  148. {
  149. public:
  150. template <class UTLMAP>
  151. static ISaveRestoreOps *GetDataOps(UTLMAP *)
  152. {
  153. static CUtlMapDataOps<UTLMAP, KEYTYPE, FIELD_TYPE> ops;
  154. return &ops;
  155. }
  156. };
  157. //-------------------------------------
  158. #define SaveUtlMap( pSave, pUtlMap, fieldtype) \
  159. CUtlMapDataopsInstantiator<fieldtype>::GetDataOps( pUtlMap )->Save( pUtlMap, pSave );
  160. #define RestoreUtlMap( pRestore, pUtlMap, fieldtype) \
  161. CUtlMapDataopsInstantiator<fieldtype>::GetDataOps( pUtlMap )->Restore( pUtlMap, pRestore );
  162. //-------------------------------------
  163. #define DEFINE_UTLMAP(name,keyType,fieldtype) \
  164. { FIELD_CUSTOM, #name, offsetof(classNameTypedef,name), 1, FTYPEDESC_SAVE, NULL, CUtlMapDataopsInstantiator<keyType, fieldtype>::GetDataOps(&(((classNameTypedef *)0)->name)), NULL }
  165. #endif // SAVERESTORE_UTLMAP_H