mptBaseMacros.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * mptBaseMacros.h
  3. * ---------------
  4. * Purpose: Basic assorted compiler-related helpers.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "mpt/base/preprocessor.hpp"
  12. #include "mpt/base/compiletime_warning.hpp"
  13. #include "mpt/base/macros.hpp"
  14. #if MPT_CXX_AT_LEAST(20)
  15. #include <version>
  16. #else // !C++20
  17. #include <array>
  18. #endif // C++20
  19. #include <array>
  20. #include <iterator>
  21. #include <type_traits>
  22. #include <cstddef>
  23. #include <cstdint>
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. OPENMPT_NAMESPACE_BEGIN
  27. #define MPT_UNREFERENCED_PARAMETER(x) MPT_UNUSED(x)
  28. #define MPT_UNUSED_VARIABLE(x) MPT_UNUSED(x)
  29. // C++17 std::size
  30. #if MPT_CXX_AT_LEAST(17)
  31. namespace mpt
  32. {
  33. using std::size;
  34. } // namespace mpt
  35. #else
  36. namespace mpt
  37. {
  38. template <typename T>
  39. MPT_CONSTEXPR11_FUN auto size(const T &v) -> decltype(v.size())
  40. {
  41. return v.size();
  42. }
  43. template <typename T, std::size_t N>
  44. MPT_CONSTEXPR11_FUN std::size_t size(const T (&)[N]) noexcept
  45. {
  46. return N;
  47. }
  48. } // namespace mpt
  49. #endif
  50. // legacy
  51. #if MPT_COMPILER_MSVC
  52. OPENMPT_NAMESPACE_END
  53. #include <cstdlib>
  54. #include <stdlib.h>
  55. OPENMPT_NAMESPACE_BEGIN
  56. #define MPT_ARRAY_COUNT(x) _countof(x)
  57. #else
  58. #define MPT_ARRAY_COUNT(x) (sizeof((x)) / sizeof((x)[0]))
  59. #endif
  60. #define CountOf(x) MPT_ARRAY_COUNT(x)
  61. #if MPT_COMPILER_MSVC
  62. // warning LNK4221: no public symbols found; archive member will be inaccessible
  63. // There is no way to selectively disable linker warnings.
  64. // #pragma warning does not apply and a command line option does not exist.
  65. // Some options:
  66. // 1. Macro which generates a variable with a unique name for each file (which means we have to pass the filename to the macro)
  67. // 2. unnamed namespace containing any symbol (does not work for c++11 compilers because they actually have internal linkage now)
  68. // 3. An unused trivial inline function.
  69. // Option 3 does not actually solve the problem though, which leaves us with option 1.
  70. // In any case, for optimized builds, the linker will just remove the useless symbol.
  71. #define MPT_MSVC_WORKAROUND_LNK4221_CONCAT_DETAIL(x, y) x##y
  72. #define MPT_MSVC_WORKAROUND_LNK4221_CONCAT(x, y) MPT_MSVC_WORKAROUND_LNK4221_CONCAT_DETAIL(x, y)
  73. #define MPT_MSVC_WORKAROUND_LNK4221(x) int MPT_MSVC_WORKAROUND_LNK4221_CONCAT(mpt_msvc_workaround_lnk4221_, x) = 0;
  74. #endif
  75. #ifndef MPT_MSVC_WORKAROUND_LNK4221
  76. #define MPT_MSVC_WORKAROUND_LNK4221(x)
  77. #endif
  78. OPENMPT_NAMESPACE_END