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.

180 lines
4.4 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SAVERESTORE_UTLVECTOR_H
  8. #define SAVERESTORE_UTLVECTOR_H
  9. #include "utlvector.h"
  10. #include "isaverestore.h"
  11. #include "saverestore_utlclass.h"
  12. #if defined( _WIN32 )
  13. #pragma once
  14. #endif
  15. //-------------------------------------
  16. template <class UTLVECTOR, int FIELD_TYPE>
  17. class CUtlVectorDataOps : public CDefSaveRestoreOps
  18. {
  19. public:
  20. CUtlVectorDataOps()
  21. {
  22. UTLCLASS_SAVERESTORE_VALIDATE_TYPE( FIELD_TYPE );
  23. }
  24. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  25. {
  26. datamap_t *pArrayTypeDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLVECTOR *)NULL );
  27. typedescription_t dataDesc =
  28. {
  29. (fieldtype_t)FIELD_TYPE,
  30. "elems",
  31. 0,
  32. 1,
  33. FTYPEDESC_SAVE,
  34. NULL,
  35. NULL,
  36. NULL,
  37. pArrayTypeDatamap,
  38. -1,
  39. };
  40. datamap_t dataMap =
  41. {
  42. &dataDesc,
  43. 1,
  44. "uv",
  45. NULL,
  46. 0,
  47. NULL,
  48. #ifdef _DEBUG
  49. true
  50. #endif
  51. };
  52. UTLVECTOR *pUtlVector = (UTLVECTOR *)fieldInfo.pField;
  53. int nElems = pUtlVector->Count();
  54. pSave->WriteInt( &nElems, 1 );
  55. if ( pArrayTypeDatamap == NULL )
  56. {
  57. if ( nElems )
  58. {
  59. dataDesc.fieldSize = nElems;
  60. dataDesc.fieldSizeInBytes = nElems * CDatamapFieldSizeDeducer<FIELD_TYPE>::FieldSize();
  61. pSave->WriteFields("elems", &((*pUtlVector)[0]), &dataMap, &dataDesc, 1 );
  62. }
  63. }
  64. else
  65. {
  66. // @Note (toml 11-21-02): Save load does not support arrays of user defined types (embedded)
  67. dataDesc.fieldSizeInBytes = CDatamapFieldSizeDeducer<FIELD_TYPE>::FieldSize();
  68. for ( int i = 0; i < nElems; i++ )
  69. pSave->WriteAll( &((*pUtlVector)[i]), &dataMap );
  70. }
  71. }
  72. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  73. {
  74. datamap_t *pArrayTypeDatamap = CTypedescDeducer<FIELD_TYPE>::Deduce( (UTLVECTOR *)NULL );
  75. typedescription_t dataDesc =
  76. {
  77. (fieldtype_t)FIELD_TYPE,
  78. "elems",
  79. 0,
  80. 1,
  81. FTYPEDESC_SAVE,
  82. NULL,
  83. NULL,
  84. NULL,
  85. pArrayTypeDatamap,
  86. -1,
  87. };
  88. datamap_t dataMap =
  89. {
  90. &dataDesc,
  91. 1,
  92. "uv",
  93. NULL,
  94. 0,
  95. NULL,
  96. #ifdef _DEBUG
  97. true
  98. #endif
  99. };
  100. UTLVECTOR *pUtlVector = (UTLVECTOR *)fieldInfo.pField;
  101. int nElems = pRestore->ReadInt();
  102. pUtlVector->SetCount( nElems );
  103. if ( pArrayTypeDatamap == NULL )
  104. {
  105. if ( nElems )
  106. {
  107. dataDesc.fieldSize = nElems;
  108. dataDesc.fieldSizeInBytes = nElems * CDatamapFieldSizeDeducer<FIELD_TYPE>::FieldSize();
  109. pRestore->ReadFields("elems", &((*pUtlVector)[0]), &dataMap, &dataDesc, 1 );
  110. }
  111. }
  112. else
  113. {
  114. // @Note (toml 11-21-02): Save load does not support arrays of user defined types (embedded)
  115. dataDesc.fieldSizeInBytes = CDatamapFieldSizeDeducer<FIELD_TYPE>::FieldSize();
  116. for ( int i = 0; i < nElems; i++ )
  117. pRestore->ReadAll( &((*pUtlVector)[i]), &dataMap );
  118. }
  119. }
  120. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  121. {
  122. UTLVECTOR *pUtlVector = (UTLVECTOR *)fieldInfo.pField;
  123. pUtlVector->SetCount( 0 );
  124. }
  125. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  126. {
  127. UTLVECTOR *pUtlVector = (UTLVECTOR *)fieldInfo.pField;
  128. return ( pUtlVector->Count() == 0 );
  129. }
  130. };
  131. //-------------------------------------
  132. template <int FIELD_TYPE>
  133. class CUtlVectorDataopsInstantiator
  134. {
  135. public:
  136. template <class UTLVECTOR>
  137. static ISaveRestoreOps *GetDataOps(UTLVECTOR *)
  138. {
  139. static CUtlVectorDataOps<UTLVECTOR, FIELD_TYPE> ops;
  140. return &ops;
  141. }
  142. };
  143. //-------------------------------------
  144. #define SaveUtlVector( pSave, pUtlVector, fieldtype) \
  145. CUtlVectorDataopsInstantiator<fieldtype>::GetDataOps( pUtlVector )->Save( pUtlVector, pSave );
  146. #define RestoreUtlVector( pRestore, pUtlVector, fieldtype) \
  147. CUtlVectorDataopsInstantiator<fieldtype>::GetDataOps( pUtlVector )->Restore( pUtlVector, pRestore );
  148. //-------------------------------------
  149. #define DEFINE_UTLVECTOR(name,fieldtype) \
  150. { FIELD_CUSTOM, #name, offsetof(classNameTypedef,name), 1, FTYPEDESC_SAVE, NULL, CUtlVectorDataopsInstantiator<fieldtype>::GetDataOps(&(((classNameTypedef *)0)->name)), NULL }
  151. #define DEFINE_GLOBAL_UTLVECTOR(name,fieldtype) \
  152. { FIELD_CUSTOM, #name, offsetof(classNameTypedef,name), 1, FTYPEDESC_SAVE|FTYPEDESC_GLOBAL, NULL, CUtlVectorDataopsInstantiator<fieldtype>::GetDataOps(&(((classNameTypedef *)0)->name)), NULL }
  153. #endif // SAVERESTORE_UTLVECTOR_H