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.

167 lines
3.5 KiB

  1. //========= Copyright 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, 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. false,
  46. false,
  47. 0,
  48. #ifdef _DEBUG
  49. true
  50. #endif
  51. };
  52. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  53. pSave->StartBlock();
  54. int nElems = pUtlRBTree->Count();
  55. pSave->WriteInt( &nElems, 1 );
  56. typename UTLRBTREE::IndexType_t i = pUtlRBTree->FirstInorder();
  57. while ( i != pUtlRBTree->InvalidIndex() )
  58. {
  59. typename UTLRBTREE::ElemType_t &elem = pUtlRBTree->Element( i );
  60. pSave->WriteAll( &elem, &dataMap );
  61. i = pUtlRBTree->NextInorder( i );
  62. }
  63. pSave->EndBlock();
  64. }
  65. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  66. {
  67. datamap_t *pTreeTypeDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLRBTREE *)NULL );
  68. typedescription_t dataDesc =
  69. {
  70. (fieldtype_t)FIELD_TYPE,
  71. "elems",
  72. { 0, 0 },
  73. 1,
  74. FTYPEDESC_SAVE,
  75. NULL,
  76. NULL,
  77. NULL,
  78. pTreeTypeDatamap,
  79. -1,
  80. };
  81. datamap_t dataMap =
  82. {
  83. &dataDesc,
  84. 1,
  85. "uv",
  86. NULL,
  87. false,
  88. false,
  89. 0,
  90. #ifdef _DEBUG
  91. true
  92. #endif
  93. };
  94. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  95. pRestore->StartBlock();
  96. int nElems = pRestore->ReadInt();
  97. typename UTLRBTREE::ElemType_t temp;
  98. while ( nElems-- )
  99. {
  100. pRestore->ReadAll( &temp, &dataMap );
  101. pUtlRBTree->Insert( temp );
  102. }
  103. pRestore->EndBlock();
  104. }
  105. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  106. {
  107. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  108. pUtlRBTree->RemoveAll();
  109. }
  110. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  111. {
  112. UTLRBTREE *pUtlRBTree = (UTLRBTREE *)fieldInfo.pField;
  113. return ( pUtlRBTree->Count() == 0 );
  114. }
  115. };
  116. //-------------------------------------
  117. template <int FIELD_TYPE>
  118. class CUtlRBTreeDataopsInstantiator
  119. {
  120. public:
  121. template <class UTLRBTREE>
  122. static ISaveRestoreOps *GetDataOps(UTLRBTREE *)
  123. {
  124. static CUtlRBTreeDataOps<UTLRBTREE, FIELD_TYPE> ops;
  125. return &ops;
  126. }
  127. };
  128. //-------------------------------------
  129. #define SaveUtlRBTree( pSave, pUtlRBTree, fieldtype) \
  130. CUtlRBTreeDataopsInstantiator<fieldtype>::GetDataOps( pUtlRBTree )->Save( pUtlRBTree, pSave );
  131. #define RestoreUtlRBTree( pRestore, pUtlRBTree, fieldtype) \
  132. CUtlRBTreeDataopsInstantiator<fieldtype>::GetDataOps( pUtlRBTree )->Restore( pUtlRBTree, pRestore );
  133. //-------------------------------------
  134. #define DEFINE_UTLRBTREE(name,fieldtype) \
  135. { FIELD_CUSTOM, #name, { offsetof(classNameTypedef,name), 0 }, 1, FTYPEDESC_SAVE, NULL, CUtlRBTreeDataopsInstantiator<fieldtype>::GetDataOps(&(((classNameTypedef *)0)->name)), NULL }
  136. #endif // SAVERESTORE_UTLRBTREE_H