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.

198 lines
4.1 KiB

  1. //========= Copyright 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. UTLCLASS_SAVERESTORE_VALIDATE_TYPE( KEY_TYPE );
  20. UTLCLASS_SAVERESTORE_VALIDATE_TYPE( FIELD_TYPE );
  21. }
  22. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  23. {
  24. datamap_t *pKeyDatamap = CTypedescDeducer<KEY_TYPE>::Deduce( (UTLMAP *)NULL );
  25. datamap_t *pFieldDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLMAP *)NULL );
  26. typedescription_t dataDesc[] =
  27. {
  28. {
  29. (fieldtype_t)KEY_TYPE,
  30. "K",
  31. { 0, 0 },
  32. 1,
  33. FTYPEDESC_SAVE,
  34. NULL,
  35. NULL,
  36. NULL,
  37. pKeyDatamap,
  38. sizeof(KEY_TYPE),
  39. },
  40. {
  41. (fieldtype_t)FIELD_TYPE,
  42. "T",
  43. { offsetof(typename UTLMAP::Node_t, elem), 0 },
  44. 1,
  45. FTYPEDESC_SAVE,
  46. NULL,
  47. NULL,
  48. NULL,
  49. pFieldDatamap,
  50. sizeof(FIELD_TYPE),
  51. }
  52. };
  53. datamap_t dataMap =
  54. {
  55. dataDesc,
  56. 2,
  57. "um",
  58. NULL,
  59. false,
  60. false,
  61. 0,
  62. #ifdef _DEBUG
  63. true
  64. #endif
  65. };
  66. typename UTLMAP::CTree *pUtlRBTree = ((UTLMAP *)fieldInfo.pField)->AccessTree();
  67. pSave->StartBlock();
  68. int nElems = pUtlRBTree->Count();
  69. pSave->WriteInt( &nElems, 1 );
  70. typename UTLMAP::CTree::IndexType_t i = pUtlRBTree->FirstInorder();
  71. while ( i != pUtlRBTree->InvalidIndex() )
  72. {
  73. typename UTLMAP::CTree::ElemType_t &elem = pUtlRBTree->Element( i );
  74. pSave->WriteAll( &elem, &dataMap );
  75. i = pUtlRBTree->NextInorder( i );
  76. }
  77. pSave->EndBlock();
  78. }
  79. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  80. {
  81. datamap_t *pKeyDatamap = CTypedescDeducer<KEY_TYPE>::Deduce( (UTLMAP *)NULL );
  82. datamap_t *pFieldDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLMAP *)NULL );
  83. typedescription_t dataDesc[] =
  84. {
  85. {
  86. (fieldtype_t)KEY_TYPE,
  87. "K",
  88. { 0, 0 },
  89. 1,
  90. FTYPEDESC_SAVE,
  91. NULL,
  92. NULL,
  93. NULL,
  94. pKeyDatamap,
  95. sizeof(KEY_TYPE),
  96. },
  97. {
  98. (fieldtype_t)FIELD_TYPE,
  99. "T",
  100. { offsetof(typename UTLMAP::Node_t, elem), 0 },
  101. 1,
  102. FTYPEDESC_SAVE,
  103. NULL,
  104. NULL,
  105. NULL,
  106. pFieldDatamap,
  107. sizeof(FIELD_TYPE),
  108. }
  109. };
  110. datamap_t dataMap =
  111. {
  112. dataDesc,
  113. 2,
  114. "um",
  115. NULL,
  116. false,
  117. false,
  118. 0,
  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), 0 }, 1, FTYPEDESC_SAVE, NULL, CUtlMapDataopsInstantiator<keyType, fieldtype>::GetDataOps(&(((classNameTypedef *)0)->name)), NULL }
  165. #endif // SAVERESTORE_UTLMAP_H