🛠️🐜 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.

93 lines
2.3 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. float gain;
  9. float gain_hf;
  10. };
  11. void eax_log_exception(const char* message = nullptr) noexcept;
  12. template<typename TException, typename TValue>
  13. void eax_validate_range(
  14. const char* value_name,
  15. const TValue& value,
  16. const TValue& min_value,
  17. const TValue& max_value)
  18. {
  19. if (value >= min_value && value <= max_value)
  20. return;
  21. const auto message =
  22. std::string{value_name} +
  23. " out of range (value: " +
  24. std::to_string(value) + "; min: " +
  25. std::to_string(min_value) + "; max: " +
  26. std::to_string(max_value) + ").";
  27. throw TException{message.c_str()};
  28. }
  29. namespace detail {
  30. template<typename T>
  31. struct EaxIsBitFieldStruct {
  32. private:
  33. using yes = std::true_type;
  34. using no = std::false_type;
  35. template<typename U>
  36. static auto test(int) -> decltype(std::declval<typename U::EaxIsBitFieldStruct>(), yes{});
  37. template<typename>
  38. static no test(...);
  39. public:
  40. static constexpr auto value = std::is_same<decltype(test<T>(0)), yes>::value;
  41. };
  42. template<typename T, typename TValue>
  43. inline bool eax_bit_fields_are_equal(const T& lhs, const T& rhs) noexcept
  44. {
  45. static_assert(sizeof(T) == sizeof(TValue), "Invalid type size.");
  46. return reinterpret_cast<const TValue&>(lhs) == reinterpret_cast<const TValue&>(rhs);
  47. }
  48. } // namespace detail
  49. template<
  50. typename T,
  51. std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
  52. >
  53. inline bool operator==(const T& lhs, const T& rhs) noexcept
  54. {
  55. using Value = std::conditional_t<
  56. sizeof(T) == 1,
  57. std::uint8_t,
  58. std::conditional_t<
  59. sizeof(T) == 2,
  60. std::uint16_t,
  61. std::conditional_t<
  62. sizeof(T) == 4,
  63. std::uint32_t,
  64. void>>>;
  65. static_assert(!std::is_same<Value, void>::value, "Unsupported type.");
  66. return detail::eax_bit_fields_are_equal<T, Value>(lhs, rhs);
  67. }
  68. template<
  69. typename T,
  70. std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
  71. >
  72. inline bool operator!=(const T& lhs, const T& rhs) noexcept
  73. {
  74. return !(lhs == rhs);
  75. }
  76. #endif // !EAX_UTILS_INCLUDED