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.

85 lines
2.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef DT_UTLVECTOR_COMMON_H
  7. #define DT_UTLVECTOR_COMMON_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "utlvector.h"
  12. typedef void (*EnsureCapacityFn)( void *pVoid, int offsetToUtlVector, int len );
  13. typedef void (*ResizeUtlVectorFn)( void *pVoid, int offsetToUtlVector, int len );
  14. template< class T >
  15. void UtlVector_InitializeAllocatedElements( T *pBase, int count )
  16. {
  17. memset( (void *)pBase, 0, count * sizeof( T ) );
  18. }
  19. template< class T, class A >
  20. class UtlVectorTemplate
  21. {
  22. public:
  23. static void ResizeUtlVector( void *pStruct, int offsetToUtlVector, int len )
  24. {
  25. CUtlVector<T,A> *pVec = (CUtlVector<T,A>*)((char*)pStruct + offsetToUtlVector);
  26. if ( pVec->Count() < len )
  27. pVec->AddMultipleToTail( len - pVec->Count() );
  28. else if ( pVec->Count() > len )
  29. pVec->RemoveMultiple( len, pVec->Count()-len );
  30. // Ensure capacity
  31. pVec->EnsureCapacity( len );
  32. int nNumAllocated = pVec->NumAllocated();
  33. // This is important to do because EnsureCapacity doesn't actually call the constructors
  34. // on the elements, but we need them to be initialized, otherwise it'll have out-of-range
  35. // values which will piss off the datatable encoder.
  36. UtlVector_InitializeAllocatedElements( pVec->Base() + pVec->Count(), nNumAllocated - pVec->Count() );
  37. }
  38. static void EnsureCapacity( void *pStruct, int offsetToUtlVector, int len )
  39. {
  40. CUtlVector<T,A> *pVec = (CUtlVector<T,A>*)((char*)pStruct + offsetToUtlVector);
  41. pVec->EnsureCapacity( len );
  42. int nNumAllocated = pVec->NumAllocated();
  43. // This is important to do because EnsureCapacity doesn't actually call the constructors
  44. // on the elements, but we need them to be initialized, otherwise it'll have out-of-range
  45. // values which will piss off the datatable encoder.
  46. UtlVector_InitializeAllocatedElements( pVec->Base() + pVec->Count(), nNumAllocated - pVec->Count() );
  47. }
  48. };
  49. template< class T, class A >
  50. inline ResizeUtlVectorFn GetResizeUtlVectorTemplate( CUtlVector<T,A> &vec )
  51. {
  52. return &UtlVectorTemplate<T,A>::ResizeUtlVector;
  53. }
  54. template< class T, class A >
  55. inline EnsureCapacityFn GetEnsureCapacityTemplate( CUtlVector<T,A> &vec )
  56. {
  57. return &UtlVectorTemplate<T,A>::EnsureCapacity;
  58. }
  59. // Format and allocate a string.
  60. char* AllocateStringHelper( PRINTF_FORMAT_STRING const char *pFormat, ... );
  61. // Allocates a string for a data table name. Data table names must be unique, so this will
  62. // assert if you try to allocate a duplicate.
  63. char* AllocateUniqueDataTableName( bool bSendTable, PRINTF_FORMAT_STRING const char *pFormat, ... );
  64. #endif // DT_UTLVECTOR_COMMON_H