Ac64BitHelpers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Copyright 2015 Autodesk, Inc. All rights reserved.
  5. //
  6. // Use of this software is subject to the terms of the Autodesk license
  7. // agreement provided at the time of installation or download, or which
  8. // otherwise accompanies this software in either electronic or hard copy form.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. #pragma once
  13. // replace _tcslen and wcslen with ac_wcslen, which returns unsigned
  14. #include "ac_tcslen.h"
  15. #include "adesk.h"
  16. #define Ac64Assert AcTcsLen_Assert
  17. #if defined(_ADESK_WINDOWS_)
  18. #if !defined(_WIN64) && !defined (_AC64)
  19. #ifdef GetWindowLongPtr
  20. #undef GetWindowLongPtr
  21. #endif
  22. // The WinUser.h definitions omits the (LONG_PTR) casts, which can
  23. // cause warnings in the 32-bit build with /Wp64
  24. //
  25. inline LONG_PTR __stdcall GetWindowLongPtr(HWND h, int n)
  26. {
  27. return static_cast<LONG_PTR>(::GetWindowLong(h, n));
  28. }
  29. #ifdef SetWindowLongPtr
  30. #undef SetWindowLongPtr
  31. #endif
  32. inline LONG_PTR __stdcall SetWindowLongPtr(HWND h, int n, LONG_PTR v)
  33. {
  34. const long nLongV = static_cast<long>(v);
  35. return static_cast<LONG_PTR>(::SetWindowLong(h, n, nLongV));
  36. }
  37. inline LONG_PTR __stdcall SetWindowLongPtr(HWND h, int n, void * v)
  38. {
  39. const LONG_PTR vPtr = reinterpret_cast<LONG_PTR>(v);
  40. return ::SetWindowLongPtr(h, n, vPtr); // call above overload
  41. }
  42. #ifdef GetClassLongPtr
  43. #undef GetClassLongPtr
  44. #endif
  45. inline ULONG_PTR __stdcall GetClassLongPtr(HWND h, int n)
  46. {
  47. return static_cast<ULONG_PTR>(::GetClassLong(h, n));
  48. }
  49. #ifdef SetClassLongPtr
  50. #undef SetClassLongPtr
  51. #endif
  52. inline ULONG_PTR __stdcall SetClassLongPtr(HWND h, int n, LONG_PTR v)
  53. {
  54. const long vLong = static_cast<long>(v);
  55. return static_cast<ULONG_PTR>(::SetClassLong(h, n, vLong));
  56. }
  57. inline ULONG_PTR __stdcall SetClassLongPtr(HWND h, int n, void * v)
  58. {
  59. const ULONG_PTR vPtr = reinterpret_cast<ULONG_PTR>(v);
  60. return ::SetClassLongPtr(h, n, vPtr); // call above overload
  61. }
  62. // The msvc definition of this in stddef.h has a (size_t) cast on a
  63. // pointer which causes a /Wp64 warning. So we insert a (UINT_PTR)
  64. // cast in between. And then we go ahead and cast down to unsigned,
  65. // since we know that member offsets will never exceed 4G.
  66. //
  67. #ifdef offsetof
  68. #undef offsetof
  69. #endif
  70. #define offsetof(s,m) static_cast<unsigned>( \
  71. reinterpret_cast<UINT_PTR> (\
  72. &reinterpret_cast<const volatile char&>( \
  73. reinterpret_cast<const s *>(0)->m )))
  74. #else // !(_WIN64 || _AC64)
  75. //These redefinitions are no longer necessary with VS 2012
  76. #if _MSC_VER < 1700
  77. // The msvc definition of this in windef.h has a (WORD) cast on a
  78. // DWORD_PTR that is shifted right 16 bits. This might cause Run
  79. // Time check failure #1 when RTTC is enabled, if the bits in the
  80. // range from 16 to 64 are not all equal to 0 before the (WORD)
  81. // cast. Here, we forcibly set bits at position from 16 to 64
  82. // all to be 0.
  83. //
  84. #ifdef HIWORD
  85. #undef HIWORD
  86. #endif
  87. // note: we leave in one C-style (DWORD_PTR) cast, because we don't know
  88. // if v is an integer type (requiring static_cast) or a pointer type
  89. // (requiring reinterpret_cast).
  90. #define HIWORD(v) (static_cast<WORD>((((DWORD_PTR)(v) >> 16) & 0xFFFF)))
  91. #endif
  92. #endif // _WIN64 || _AC64
  93. #endif // _ADESK_WINDOWS_
  94. // The msvc definition of this in windef.h is bad in both 32-bit and
  95. // 64-bit builds, triggering a Runtime Check assert.
  96. // #define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8))
  97. #ifdef HIBYTE
  98. #undef HIBYTE
  99. #endif
  100. #define HIBYTE(w) LOBYTE((DWORD_PTR)(w) >> 8)
  101. #pragma warning(push)
  102. #pragma warning(disable:4100)//disable unreferenced formal parameter warning
  103. inline int AcIntPtrToInt(Adesk::LongPtr nVal, Adesk::LongPtr nLimit = /*16M*/0x1000000)
  104. {
  105. Ac64Assert(nVal < nLimit);
  106. Ac64Assert(nVal > -nLimit); // it's a signed value, so assert at both ends of the range
  107. const int nRet = static_cast<int>(nVal);
  108. Ac64Assert(nRet == nVal); // a little redundant, but make sure no bits are lost
  109. return nRet;
  110. }
  111. inline unsigned int AcUIntPtrToUInt(Adesk::ULongPtr nVal, Adesk::ULongPtr nLimit = /*16M*/0x1000000)
  112. {
  113. Ac64Assert(nVal < nLimit);
  114. const unsigned int nRet = static_cast<unsigned int>(nVal);
  115. Ac64Assert(nRet == nVal); // a little redundant, but make sure no bits are lost
  116. return nRet;
  117. }
  118. #pragma warning(pop)
  119. // provide a polymophic function to parse INT_PTR string.
  120. inline Adesk::LongPtr __stdcall AcStrToIntPtr(const wchar_t * s)
  121. {
  122. #if defined(_WIN64) || defined(_AC64)
  123. return _wtoi64(s);
  124. #else
  125. return _wtoi(s);
  126. #endif
  127. }