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.

95 lines
2.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SAVERESTORE_BITSTRING_H
  8. #define SAVERESTORE_BITSTRING_H
  9. #include "isaverestore.h"
  10. #if defined( _WIN32 )
  11. #pragma once
  12. #endif
  13. //-------------------------------------
  14. template <class BITSTRING>
  15. class CVarBitVecSaveRestoreOps : public CDefSaveRestoreOps
  16. {
  17. public:
  18. CVarBitVecSaveRestoreOps()
  19. {
  20. }
  21. // save data type interface
  22. virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  23. {
  24. BITSTRING *pBitString = (BITSTRING *)fieldInfo.pField;
  25. int numBits = pBitString->GetNumBits();
  26. pSave->WriteInt( &numBits );
  27. pSave->WriteInt( pBitString->Base(), pBitString->GetNumDWords() );
  28. }
  29. virtual void Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  30. {
  31. BITSTRING *pBitString = (BITSTRING *)fieldInfo.pField;
  32. int numBits = pRestore->ReadInt();
  33. if ( !pBitString->IsFixedSize() )
  34. pBitString->Resize( numBits );
  35. else
  36. {
  37. Assert( pBitString->GetNumBits() >= numBits );
  38. pBitString->ClearAll();
  39. }
  40. int numIntsInStream = CalcNumIntsForBits( numBits );
  41. int readSize = MIN( pBitString->GetNumDWords(), numIntsInStream );
  42. pRestore->ReadInt( pBitString->Base(), numIntsInStream );
  43. numIntsInStream -= readSize;
  44. while ( numIntsInStream-- > 0 )
  45. {
  46. int ignored;
  47. pRestore->ReadInt( &ignored, 1 );
  48. }
  49. }
  50. virtual void MakeEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  51. {
  52. BITSTRING *pBitString = (BITSTRING *)fieldInfo.pField;
  53. pBitString->ClearAll();
  54. }
  55. virtual bool IsEmpty( const SaveRestoreFieldInfo_t &fieldInfo )
  56. {
  57. BITSTRING *pBitString = (BITSTRING *)fieldInfo.pField;
  58. return pBitString->IsAllClear();
  59. }
  60. };
  61. //-------------------------------------
  62. template <class BITSTRING>
  63. ISaveRestoreOps *GetBitstringDataOps(BITSTRING *)
  64. {
  65. static CVarBitVecSaveRestoreOps<BITSTRING> ops;
  66. return &ops;
  67. }
  68. //-------------------------------------
  69. #define SaveBitString( pSave, pBitString, fieldtype) \
  70. CDataopsInstantiator<fieldtype>::GetDataOps( pBitString )->Save( pBitString, pSave );
  71. #define RestoreBitString( pRestore, pBitString, fieldtype) \
  72. CDataopsInstantiator<fieldtype>::GetDataOps( pBitString )->Restore( pBitString, pRestore );
  73. //-------------------------------------
  74. #define DEFINE_BITSTRING(name) \
  75. { FIELD_CUSTOM, #name, offsetof(classNameTypedef,name), 1, FTYPEDESC_SAVE, NULL, GetBitstringDataOps(&(((classNameTypedef *)0)->name)), NULL }
  76. #endif // SAVERESTORE_BITSTRING_H