common_funcs.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <array>
  7. #include <string>
  8. #if !defined(ARCHITECTURE_x86_64)
  9. #include <cstdlib> // for exit
  10. #endif
  11. #include "common/common_types.h"
  12. /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
  13. #define CONCAT2(x, y) DO_CONCAT2(x, y)
  14. #define DO_CONCAT2(x, y) x##y
  15. /// Helper macros to insert unused bytes or words to properly align structs. These values will be
  16. /// zero-initialized.
  17. #define INSERT_PADDING_BYTES(num_bytes) \
  18. std::array<u8, num_bytes> CONCAT2(pad, __LINE__) {}
  19. #define INSERT_PADDING_WORDS(num_words) \
  20. std::array<u32, num_words> CONCAT2(pad, __LINE__) {}
  21. /// These are similar to the INSERT_PADDING_* macros but do not zero-initialize the contents.
  22. /// This keeps the structure trivial to construct.
  23. #define INSERT_PADDING_BYTES_NOINIT(num_bytes) std::array<u8, num_bytes> CONCAT2(pad, __LINE__)
  24. #define INSERT_PADDING_WORDS_NOINIT(num_words) std::array<u32, num_words> CONCAT2(pad, __LINE__)
  25. #ifndef _MSC_VER
  26. #ifdef ARCHITECTURE_x86_64
  27. #define Crash() __asm__ __volatile__("int $3")
  28. #else
  29. #define Crash() exit(1)
  30. #endif
  31. #else // _MSC_VER
  32. // Locale Cross-Compatibility
  33. #define locale_t _locale_t
  34. extern "C" {
  35. __declspec(dllimport) void __stdcall DebugBreak(void);
  36. }
  37. #define Crash() DebugBreak()
  38. #endif // _MSC_VER ndef
  39. // Generic function to get last error message.
  40. // Call directly after the command or use the error num.
  41. // This function might change the error code.
  42. // Defined in misc.cpp.
  43. [[nodiscard]] std::string GetLastErrorMsg();
  44. // Like GetLastErrorMsg(), but passing an explicit error code.
  45. // Defined in misc.cpp.
  46. [[nodiscard]] std::string NativeErrorToString(int e);
  47. #define DECLARE_ENUM_FLAG_OPERATORS(type) \
  48. [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \
  49. using T = std::underlying_type_t<type>; \
  50. return static_cast<type>(static_cast<T>(a) | static_cast<T>(b)); \
  51. } \
  52. [[nodiscard]] constexpr type operator&(type a, type b) noexcept { \
  53. using T = std::underlying_type_t<type>; \
  54. return static_cast<type>(static_cast<T>(a) & static_cast<T>(b)); \
  55. } \
  56. [[nodiscard]] constexpr type operator^(type a, type b) noexcept { \
  57. using T = std::underlying_type_t<type>; \
  58. return static_cast<type>(static_cast<T>(a) ^ static_cast<T>(b)); \
  59. } \
  60. constexpr type& operator|=(type& a, type b) noexcept { \
  61. a = a | b; \
  62. return a; \
  63. } \
  64. constexpr type& operator&=(type& a, type b) noexcept { \
  65. a = a & b; \
  66. return a; \
  67. } \
  68. constexpr type& operator^=(type& a, type b) noexcept { \
  69. a = a ^ b; \
  70. return a; \
  71. } \
  72. [[nodiscard]] constexpr type operator~(type key) noexcept { \
  73. using T = std::underlying_type_t<type>; \
  74. return static_cast<type>(~static_cast<T>(key)); \
  75. } \
  76. [[nodiscard]] constexpr bool True(type key) noexcept { \
  77. using T = std::underlying_type_t<type>; \
  78. return static_cast<T>(key) != 0; \
  79. } \
  80. [[nodiscard]] constexpr bool False(type key) noexcept { \
  81. using T = std::underlying_type_t<type>; \
  82. return static_cast<T>(key) == 0; \
  83. }
  84. #define YUZU_NON_COPYABLE(cls) \
  85. cls(const cls&) = delete; \
  86. cls& operator=(const cls&) = delete
  87. #define YUZU_NON_MOVEABLE(cls) \
  88. cls(cls&&) = delete; \
  89. cls& operator=(cls&&) = delete
  90. namespace Common {
  91. [[nodiscard]] constexpr u32 MakeMagic(char a, char b, char c, char d) {
  92. return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24;
  93. }
  94. // std::size() does not support zero-size C arrays. We're fixing that.
  95. template <class C>
  96. constexpr auto Size(const C& c) -> decltype(c.size()) {
  97. return std::size(c);
  98. }
  99. template <class C>
  100. constexpr std::size_t Size(const C& c) {
  101. if constexpr (sizeof(C) == 0) {
  102. return 0;
  103. } else {
  104. return std::size(c);
  105. }
  106. }
  107. } // namespace Common