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.

165 lines
3.5 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef SAVERESTORE_UTLRBTREE_H
  7. #define SAVERESTORE_UTLRBTREE_H
  8. #include "utlrbtree.h"
  9. #include "isaverestore.h"
  10. #include "saverestore_utlclass.h"
  11. #if defined( _WIN32 )
  12. #pragma once
  13. #endif
  14. //-------------------------------------
  15. template <class UTLRBTREE, int FIELD_TYPE>
  16. class CUtlRBTreeDataOps : public CDefSaveRestoreOps
  17. {
  18. public:
  19. CUtlRBTreeDataOps()
  20. {
  21. UTLCLASS_SAVERESTORE_VALIDATE_TYPE( FIELD_TYPE );
  22. }
  23. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  24. {
  25. datamap_t *pTreeTypeDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLRBTREE *)NULL );
  26. typedescription_t dataDesc =
  27. {
  28. (fieldtype_t)FIELD_TYPE,
  29. "elem",
  30. 0,
  31. 1,
  32. FTYPEDESC_SAVE,
  33. NULL,
  34. NULL,
  35. NULL,
  36. pTreeTypeDatamap,
  37. -1,
  38. };
  39. datamap_t dataMap =
  40. {
  41. &dataDesc,
  42. 1,
  43. "urb",
  44. NULL,
  45. 0,
  46. NULL,
  47. #ifdef _DEBUG
  48. true
  49. #endif
  50. };
  51. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  52. pSave->StartBlock();
  53. int nElems = pUtlRBTree->Count();
  54. pSave->WriteInt( &nElems, 1 );
  55. typename UTLRBTREE::IndexType_t i = pUtlRBTree->FirstInorder();
  56. while ( i != pUtlRBTree->InvalidIndex() )
  57. {
  58. typename UTLRBTREE::ElemType_t &elem = pUtlRBTree->Element( i );
  59. pSave->WriteAll( &elem, &dataMap );
  60. i = pUtlRBTree->NextInorder( i );
  61. }
  62. pSave->EndBlock();
  63. }
  64. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  65. {
  66. datamap_t *pTreeTypeDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLRBTREE *)NULL );
  67. typedescription_t dataDesc =
  68. {
  69. (fieldtype_t)FIELD_TYPE,
  70. "elems",
  71. 0,
  72. 1,
  73. FTYPEDESC_SAVE,
  74. NULL,
  75. NULL,
  76. NULL,
  77. pTreeTypeDatamap,
  78. -1,
  79. };
  80. datamap_t dataMap =
  81. {
  82. &dataDesc,
  83. 1,
  84. "uv",
  85. NULL,
  86. 0,
  87. NULL,
  88. #ifdef _DEBUG
  89. true
  90. #endif
  91. };
  92. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  93. pRestore->StartBlock();
  94. int nElems = pRestore->ReadInt();
  95. typename UTLRBTREE::ElemType_t temp;
  96. while ( nElems-- )
  97. {
  98. pRestore->ReadAll( &temp, &dataMap );
  99. pUtlRBTree->Insert( temp );
  100. }
  101. pRestore->EndBlock();
  102. }
  103. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  104. {
  105. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  106. pUtlRBTree->RemoveAll();
  107. }
  108. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  109. {
  110. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  111. return ( pUtlRBTree->Count() == 0 );
  112. }
  113. };
  114. //-------------------------------------
  115. template <int FIELD_TYPE>
  116. class CUtlRBTreeDataopsInstantiator
  117. {
  118. public:
  119. template <class UTLRBTREE>
  120. static ISaveRestoreOps *GetDataOps(UTLRBTREE *)
  121. {
  122. static CUtlRBTreeDataOps<UTLRBTREE, FIELD_TYPE> ops;
  123. return &ops;
  124. }
  125. };
  126. //-------------------------------------
  127. #define SaveUtlRBTree( pSave, pUtlRBTree, fieldtype) \
  128. CUtlRBTreeDataopsInstantiator<fieldtype>::GetDataOps( pUtlRBTree )->Save( pUtlRBTree, pSave );
  129. #define RestoreUtlRBTree( pRestore, pUtlRBTree, fieldtype) \
  130. CUtlRBTreeDataopsInstantiator<fieldtype>::GetDataOps( pUtlRBTree )->Restore( pUtlRBTree, pRestore );
  131. //-------------------------------------
  132. #define DEFINE_UTLRBTREE(name,fieldtype) \
  133. { FIELD_CUSTOM, #name, { offsetof(classNameTypedef,name), 0 }, 1, FTYPEDESC_SAVE, NULL, CUtlRBTreeDataopsInstantiator<fieldtype>::GetDataOps(&(((classNameTypedef *)0)->name)), NULL }
  134. #endif // SAVERESTORE_UTLRBTREE_H