Team Fortress 2 Source Code as on 22/4/2020
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.

149 lines
4.2 KiB

  1. //========= Copyright � 1996-2005, 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. // -------------------------------------------------------
  14. //
  15. // commonmacros.h
  16. //
  17. // This should contain ONLY general purpose macros that are
  18. // appropriate for use in engine/launcher/all tools
  19. //
  20. // -------------------------------------------------------
  21. // Makes a 4-byte "packed ID" int out of 4 characters
  22. #define MAKEID(d,c,b,a) ( ((int)(a) << 24) | ((int)(b) << 16) | ((int)(c) << 8) | ((int)(d)) )
  23. // Compares a string with a 4-byte packed ID constant
  24. #define STRING_MATCHES_ID( p, id ) ( (*((int *)(p)) == (id) ) ? true : false )
  25. #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) )
  26. #define V_ARRAYSIZE(p) (sizeof(p)/sizeof(p[0]))
  27. #define SETBITS(iBitVector, bits) ((iBitVector) |= (bits))
  28. #define CLEARBITS(iBitVector, bits) ((iBitVector) &= ~(bits))
  29. #define FBitSet(iBitVector, bits) ((iBitVector) & (bits))
  30. inline bool IsPowerOfTwo( int value )
  31. {
  32. return (value & ( value - 1 )) == 0;
  33. }
  34. #ifndef REFERENCE
  35. #define REFERENCE(arg) ((void)arg)
  36. #endif
  37. #define CONST_INTEGER_AS_STRING(x) #x //Wraps the integer in quotes, allowing us to form constant strings with it
  38. #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__"
  39. #define __LINE__AS_STRING __HACK_LINE_AS_STRING__(__LINE__) //Gives you the line number in constant string form
  40. // Using ARRAYSIZE implementation from winnt.h:
  41. #ifdef ARRAYSIZE
  42. #undef ARRAYSIZE
  43. #endif
  44. // Return the number of elements in a statically sized array.
  45. // DWORD Buffer[100];
  46. // RTL_NUMBER_OF(Buffer) == 100
  47. // This is also popularly known as: NUMBER_OF, ARRSIZE, _countof, NELEM, etc.
  48. //
  49. #define RTL_NUMBER_OF_V1(A) (sizeof(A)/sizeof((A)[0]))
  50. #if defined(__cplusplus) && \
  51. !defined(MIDL_PASS) && \
  52. !defined(RC_INVOKED) && \
  53. !defined(_PREFAST_) && \
  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. #define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))
  89. // This does not work with:
  90. //
  91. // void Foo()
  92. // {
  93. // struct { int x; } y[2];
  94. // RTL_NUMBER_OF_V2(y); // illegal use of anonymous local type in template instantiation
  95. // }
  96. //
  97. // You must instead do:
  98. //
  99. // struct Foo1 { int x; };
  100. //
  101. // void Foo()
  102. // {
  103. // Foo1 y[2];
  104. // RTL_NUMBER_OF_V2(y); // ok
  105. // }
  106. //
  107. // OR
  108. //
  109. // void Foo()
  110. // {
  111. // struct { int x; } y[2];
  112. // RTL_NUMBER_OF_V1(y); // ok
  113. // }
  114. //
  115. // OR
  116. //
  117. // void Foo()
  118. // {
  119. // struct { int x; } y[2];
  120. // _ARRAYSIZE(y); // ok
  121. // }
  122. #else
  123. #define RTL_NUMBER_OF_V2(A) RTL_NUMBER_OF_V1(A)
  124. #endif
  125. // ARRAYSIZE is more readable version of RTL_NUMBER_OF_V2
  126. // _ARRAYSIZE is a version useful for anonymous types
  127. #define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A)
  128. #define _ARRAYSIZE(A) RTL_NUMBER_OF_V1(A)
  129. #endif // COMMONMACROS_H