common_funcs.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <string>
  7. #if !defined(ARCHITECTURE_x86_64)
  8. #include <cstdlib> // for exit
  9. #endif
  10. #include "common/common_types.h"
  11. /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
  12. #define CONCAT2(x, y) DO_CONCAT2(x, y)
  13. #define DO_CONCAT2(x, y) x##y
  14. // helper macro to properly align structure members.
  15. // Calling INSERT_PADDING_BYTES will add a new member variable with a name like "pad121",
  16. // depending on the current source line to make sure variable names are unique.
  17. #define INSERT_PADDING_BYTES(num_bytes) u8 CONCAT2(pad, __LINE__)[(num_bytes)]
  18. #define INSERT_PADDING_WORDS(num_words) u32 CONCAT2(pad, __LINE__)[(num_words)]
  19. // Inlining
  20. #ifdef _WIN32
  21. #define FORCE_INLINE __forceinline
  22. #else
  23. #define FORCE_INLINE inline __attribute__((always_inline))
  24. #endif
  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. std::string GetLastErrorMsg();
  44. namespace Common {
  45. constexpr u32 MakeMagic(char a, char b, char c, char d) {
  46. return a | b << 8 | c << 16 | d << 24;
  47. }
  48. template <class ForwardIt, class T, class Compare = std::less<>>
  49. ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) {
  50. // Note: BOTH type T and the type after ForwardIt is dereferenced
  51. // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
  52. // This is stricter than lower_bound requirement (see above)
  53. first = std::lower_bound(first, last, value, comp);
  54. return first != last && !comp(value, *first) ? first : last;
  55. }
  56. } // namespace Common