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.

212 lines
5.5 KiB

  1. //====== Copyright � 1996-2007, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef MDLLIB_UTILS_H
  7. #define MDLLIB_UTILS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "utlmap.h"
  12. #include "utlvector.h"
  13. #include "bitvec.h"
  14. //////////////////////////////////////////////////////////////////////////
  15. //
  16. // Helper macros
  17. //
  18. //////////////////////////////////////////////////////////////////////////
  19. // Declare a pointer and automatically do the cast of initial value to the pointer type
  20. #define DECLARE_PTR( type, name, initval ) type *name = ( type * ) ( initval )
  21. #define DECLARE_UPDATE_PTR( type, name, initval ) name = ( type * ) ( initval )
  22. // Compute a pointer that is offset given number of bytes from the base pointer
  23. #define BYTE_OFF_PTR( initval, offval ) ( ( ( byte * ) ( initval ) ) + ( offval ) )
  24. // Compute difference in bytes between two pointers
  25. #define BYTE_DIFF_PTR( begin, end ) ( ( ( byte * ) ( end ) ) - ( ( byte * ) ( begin ) ) )
  26. // "for {" to iterate children of a studio container
  27. #define ITERATE_CHILDREN( type, name, parent, accessor, count ) \
  28. for ( int name##_idx = 0; name##_idx < (parent)->count; ++ name##_idx ) { \
  29. type *name = (parent)->accessor( name##_idx );
  30. // "for {" to jointly iterate children of 2 studio containers of same size
  31. #define ITERATE_CHILDREN2( type, type2, name, name2, parent, parent2, accessor, accessor2, count ) \
  32. for ( int name##_idx = 0; name##_idx < (parent)->count; ++ name##_idx ) { \
  33. type *name = (parent)->accessor( name##_idx ); \
  34. type2 *name2 = (parent2)->accessor2( name##_idx );
  35. // "}" to mark the end of iteration block
  36. #define ITERATE_END }
  37. // Get the child of a container by index
  38. #define CHILD_AT( parent, accessor, idx ) ( (parent)->accessor( idx ) )
  39. //
  40. // CLessSimple< T >
  41. // Comparison policy to use "t1 < t2" comparison rule.
  42. //
  43. template < typename T >
  44. class CLessSimple
  45. {
  46. public:
  47. bool Less( const T& src1, const T& src2, void *pCtx )
  48. {
  49. pCtx;
  50. return ( src1 < src2 );
  51. }
  52. };
  53. //
  54. // CInsertionTracker
  55. // Class that is tracking insertions that are scheduled to happen at given points.
  56. // Use policy:
  57. // InsertBytes / InsertElements [*] -- schedule insertions
  58. // Finalize -- finalize insertion information
  59. // ComputePointer / ComputeOffset [*] -- compute new pointers/offsets that will happen after insertions
  60. // MemMove -- perform memory moves to apply insertions
  61. //
  62. class CInsertionTracker
  63. {
  64. public:
  65. CInsertionTracker() : m_map( DefLessFunc( byte * ) ) {}
  66. // Schedules a piece of memory for insertion
  67. public:
  68. void InsertBytes( void *pos, int length );
  69. template< typename T >
  70. void InsertElements( T *ptr, int count = 1 ) { InsertBytes( ( byte * ) ptr, count * sizeof( T ) ); }
  71. int GetNumBytesInserted() const;
  72. // Finalizes the insertion information
  73. public:
  74. void Finalize();
  75. // Computes where the pointer would point after memory insertion occurs
  76. public:
  77. void * ComputePointer( void *ptrNothingInserted ) const;
  78. int ComputeOffset( void *ptrBase, int off ) const;
  79. // Perform memory moves, the buffer should be large enough to accommodate inserted bytes
  80. public:
  81. void MemMove( void *ptrBase, int &length ) const;
  82. protected:
  83. typedef CUtlMap< byte *, int, unsigned int > Map;
  84. Map m_map; // pos -> length
  85. };
  86. //
  87. // CMemoryMovingTracker
  88. // Class that is tracking removals that are scheduled to happen at given points.
  89. // Use policy:
  90. // RegisterBytes / RegisterElements[*] -- schedule moving
  91. // Finalize -- finalize moving information
  92. // ComputePointer / ComputeOffset [*] -- compute new pointers/offsets that will happen after moving
  93. // MemMove -- perform memory moves to apply
  94. //
  95. class CMemoryMovingTracker
  96. {
  97. public:
  98. enum MemoryMovingPolicy_t
  99. {
  100. MEMORY_REMOVE,
  101. MEMORY_INSERT,
  102. MEMORY_MODIFY,
  103. };
  104. explicit CMemoryMovingTracker( MemoryMovingPolicy_t ePolicy ) : m_map( DefLessFunc( byte * ) ), m_ePolicy( ePolicy ) {}
  105. // Schedules a piece of memory for removal
  106. public:
  107. void RegisterBytes( void *pos, int length );
  108. template< typename T >
  109. void RegisterElements( T *ptr, int count = 1 ) { RegisterBytes( ( byte * ) ptr, count * sizeof( T ) ); }
  110. int GetNumBytesRegistered() const;
  111. // Finalizes the removal information
  112. public:
  113. void RegisterBaseDelta( void *pOldBase, void *pNewBase );
  114. void Finalize();
  115. // Computes where the pointer would point after memory removal occurs
  116. public:
  117. void * ComputePointer( void *ptrNothingRemoved ) const;
  118. int ComputeOffset( void *ptrBase, int off ) const;
  119. public:
  120. void MemMove( void *ptrBase, int &length ) const;
  121. protected:
  122. typedef CUtlMap< byte *, int, unsigned int > Map;
  123. Map m_map; // pos -> length
  124. struct Item
  125. {
  126. Map::IndexType_t idx;
  127. byte *ptr;
  128. int len;
  129. };
  130. Item m_hint;
  131. MemoryMovingPolicy_t m_ePolicy;
  132. };
  133. //
  134. // CGrowableBitVec
  135. // Serves bit accumulation.
  136. // Provides "GrowSetBit" method to automatically grow to the required size
  137. // and set the given bit.
  138. // Provides safe "IsBitSet" that would return false for missing bits.
  139. //
  140. class CGrowableBitVec : public CLargeVarBitVec
  141. {
  142. public:
  143. void GrowSetBit( int iBit )
  144. {
  145. if ( iBit >= GetNumBits() )
  146. Resize( iBit + 1, false );
  147. Set( iBit );
  148. }
  149. bool IsBitSet( int bitNum ) const
  150. {
  151. return ( bitNum < GetNumBits() ) && CLargeVarBitVec::IsBitSet( bitNum );
  152. }
  153. };
  154. //
  155. // CGrowableVector
  156. // Provides zero-initialization for new elements.
  157. //
  158. template < typename T >
  159. class CGrowableVector : public CUtlVector < T >
  160. {
  161. public:
  162. T & operator[] ( int idx )
  163. {
  164. while ( idx >= Count() )
  165. AddToTail( T() );
  166. return CUtlVector < T >::operator []( idx );
  167. }
  168. };
  169. #endif // #ifndef MDLLIB_UTILS_H