🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

132 lines
2.4 KiB

  1. #ifndef EAX_UTILS_INCLUDED
  2. #define EAX_UTILS_INCLUDED
  3. #include <algorithm>
  4. #include <cstdint>
  5. #include <string>
  6. #include <type_traits>
  7. struct EaxAlLowPassParam
  8. {
  9. float gain;
  10. float gain_hf;
  11. }; // EaxAlLowPassParam
  12. void eax_log_exception(
  13. const char* message = nullptr) noexcept;
  14. template<
  15. typename TException,
  16. typename TValue
  17. >
  18. void eax_validate_range(
  19. const char* value_name,
  20. const TValue& value,
  21. const TValue& min_value,
  22. const TValue& max_value)
  23. {
  24. if (value >= min_value && value <= max_value)
  25. {
  26. return;
  27. }
  28. const auto message =
  29. std::string{value_name} +
  30. " out of range (value: " +
  31. std::to_string(value) + "; min: " +
  32. std::to_string(min_value) + "; max: " +
  33. std::to_string(max_value) + ").";
  34. throw TException{message.c_str()};
  35. }
  36. namespace detail
  37. {
  38. template<
  39. typename T
  40. >
  41. struct EaxIsBitFieldStruct
  42. {
  43. private:
  44. using yes = std::true_type;
  45. using no = std::false_type;
  46. template<
  47. typename U
  48. >
  49. static auto test(int) -> decltype(std::declval<typename U::EaxIsBitFieldStruct>(), yes{});
  50. template<
  51. typename
  52. >
  53. static no test(...);
  54. public:
  55. static constexpr auto value = std::is_same<decltype(test<T>(0)), yes>::value;
  56. }; // EaxIsBitFieldStruct
  57. template<
  58. typename T,
  59. typename TValue
  60. >
  61. inline bool eax_bit_fields_are_equal(
  62. const T& lhs,
  63. const T& rhs) noexcept
  64. {
  65. static_assert(sizeof(T) == sizeof(TValue), "Invalid type size.");
  66. return reinterpret_cast<const TValue&>(lhs) == reinterpret_cast<const TValue&>(rhs);
  67. }
  68. } // namespace detail
  69. template<
  70. typename T,
  71. std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
  72. >
  73. inline bool operator==(
  74. const T& lhs,
  75. const T& rhs) noexcept
  76. {
  77. using Value = std::conditional_t<
  78. sizeof(T) == 1,
  79. std::uint8_t,
  80. std::conditional_t<
  81. sizeof(T) == 2,
  82. std::uint16_t,
  83. std::conditional_t<
  84. sizeof(T) == 4,
  85. std::uint32_t,
  86. void
  87. >
  88. >
  89. >;
  90. static_assert(!std::is_same<Value, void>::value, "Unsupported type.");
  91. return detail::eax_bit_fields_are_equal<T, Value>(lhs, rhs);
  92. }
  93. template<
  94. typename T,
  95. std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
  96. >
  97. inline bool operator!=(
  98. const T& lhs,
  99. const T& rhs) noexcept
  100. {
  101. return !(lhs == rhs);
  102. }
  103. #endif // !EAX_UTILS_INCLUDED