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.

174 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef COMMONMACROS_H
  9. #define COMMONMACROS_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier0/platform.h"
  14. // -------------------------------------------------------
  15. //
  16. // commonmacros.h
  17. //
  18. // This should contain ONLY general purpose macros that are
  19. // appropriate for use in engine/launcher/all tools
  20. //
  21. // -------------------------------------------------------
  22. // Makes a 4-byte "packed ID" int out of 4 characters
  23. #define MAKEID(d,c,b,a) ( ((int)(a) << 24) | ((int)(b) << 16) | ((int)(c) << 8) | ((int)(d)) )
  24. // Compares a string with a 4-byte packed ID constant
  25. #define STRING_MATCHES_ID( p, id ) ( (*((int *)(p)) == (id) ) ? true : false )
  26. #define ID_TO_STRING( id, p ) ( (p)[3] = (((id)>>24) & 0xFF), (p)[2] = (((id)>>16) & 0xFF), (p)[1] = (((id)>>8) & 0xFF), (p)[0] = (((id)>>0) & 0xFF) )
  27. #define SETBITS(iBitVector, bits) ((iBitVector) |= (bits))
  28. #define CLEARBITS(iBitVector, bits) ((iBitVector) &= ~(bits))
  29. #define FBitSet(iBitVector, bits) ((iBitVector) & (bits))
  30. template <typename T>
  31. inline bool IsPowerOfTwo( T value )
  32. {
  33. return (value & ( value - (T)1 )) == (T)0;
  34. }
  35. #ifndef REFERENCE
  36. #define REFERENCE(arg) ((void)arg)
  37. #endif
  38. #define CONST_INTEGER_AS_STRING(x) #x //Wraps the integer in quotes, allowing us to form constant strings with it
  39. #define __HACK_LINE_AS_STRING__(x) CONST_INTEGER_AS_STRING(x) //__LINE__ can only be converted to an actual number by going through this, otherwise the output is literally "__LINE__"
  40. #define __LINE__AS_STRING __HACK_LINE_AS_STRING__(__LINE__) //Gives you the line number in constant string form
  41. // Using ARRAYSIZE implementation from winnt.h:
  42. #ifdef ARRAYSIZE
  43. #undef ARRAYSIZE
  44. #endif
  45. // Return the number of elements in a statically sized array.
  46. // DWORD Buffer[100];
  47. // RTL_NUMBER_OF(Buffer) == 100
  48. // This is also popularly known as: NUMBER_OF, ARRSIZE, _countof, NELEM, etc.
  49. //
  50. #define RTL_NUMBER_OF_V1(A) (sizeof(A)/sizeof((A)[0]))
  51. #if defined(__cplusplus) && \
  52. !defined(MIDL_PASS) && \
  53. !defined(RC_INVOKED) && \
  54. (_MSC_FULL_VER >= 13009466) && \
  55. !defined(SORTPP_PASS)
  56. // From crtdefs.h
  57. #if !defined(UNALIGNED)
  58. #if defined(_M_IA64) || defined(_M_AMD64)
  59. #define UNALIGNED __unaligned
  60. #else
  61. #define UNALIGNED
  62. #endif
  63. #endif
  64. // RtlpNumberOf is a function that takes a reference to an array of N Ts.
  65. //
  66. // typedef T array_of_T[N];
  67. // typedef array_of_T &reference_to_array_of_T;
  68. //
  69. // RtlpNumberOf returns a pointer to an array of N chars.
  70. // We could return a reference instead of a pointer but older compilers do not accept that.
  71. //
  72. // typedef char array_of_char[N];
  73. // typedef array_of_char *pointer_to_array_of_char;
  74. //
  75. // sizeof(array_of_char) == N
  76. // sizeof(*pointer_to_array_of_char) == N
  77. //
  78. // pointer_to_array_of_char RtlpNumberOf(reference_to_array_of_T);
  79. //
  80. // We never even call RtlpNumberOf, we just take the size of dereferencing its return type.
  81. // We do not even implement RtlpNumberOf, we just decare it.
  82. //
  83. // Attempts to pass pointers instead of arrays to this macro result in compile time errors.
  84. // That is the point.
  85. extern "C++" // templates cannot be declared to have 'C' linkage
  86. template <typename T, size_t N>
  87. char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];
  88. #ifdef _PREFAST_
  89. // The +0 is so that we can go:
  90. // size = ARRAYSIZE(array) * sizeof(array[0]) without triggering a /analyze
  91. // warning about multiplying sizeof.
  92. #define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A))+0)
  93. #else
  94. #define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))
  95. #endif
  96. // This does not work with:
  97. //
  98. // void Foo()
  99. // {
  100. // struct { int x; } y[2];
  101. // RTL_NUMBER_OF_V2(y); // illegal use of anonymous local type in template instantiation
  102. // }
  103. //
  104. // You must instead do:
  105. //
  106. // struct Foo1 { int x; };
  107. //
  108. // void Foo()
  109. // {
  110. // Foo1 y[2];
  111. // RTL_NUMBER_OF_V2(y); // ok
  112. // }
  113. //
  114. // OR
  115. //
  116. // void Foo()
  117. // {
  118. // struct { int x; } y[2];
  119. // RTL_NUMBER_OF_V1(y); // ok
  120. // }
  121. //
  122. // OR
  123. //
  124. // void Foo()
  125. // {
  126. // struct { int x; } y[2];
  127. // _ARRAYSIZE(y); // ok
  128. // }
  129. #else
  130. #define RTL_NUMBER_OF_V2(A) RTL_NUMBER_OF_V1(A)
  131. #endif
  132. // ARRAYSIZE is more readable version of RTL_NUMBER_OF_V2
  133. // _ARRAYSIZE is a version useful for anonymous types
  134. #define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A)
  135. #define _ARRAYSIZE(A) RTL_NUMBER_OF_V1(A)
  136. #define Q_ARRAYSIZE(p) ARRAYSIZE(p)
  137. #define V_ARRAYSIZE(p) ARRAYSIZE(p)
  138. template< typename IndexType, typename T, unsigned int N >
  139. IndexType ClampedArrayIndex( const T (&buffer)[N], IndexType index )
  140. {
  141. NOTE_UNUSED( buffer );
  142. return clamp( index, 0, (IndexType)N - 1 );
  143. }
  144. template< typename T, unsigned int N >
  145. T ClampedArrayElement( const T (&buffer)[N], unsigned int uIndex )
  146. {
  147. // Put index in an unsigned type to halve the clamping.
  148. if ( uIndex >= N )
  149. uIndex = N - 1;
  150. return buffer[ uIndex ];
  151. }
  152. #endif // COMMONMACROS_H