utils.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef EAX_UTILS_INCLUDED
  2. #define EAX_UTILS_INCLUDED
  3. #include <algorithm>
  4. #include <cstdint>
  5. #include <string>
  6. #include <string_view>
  7. #include <type_traits>
  8. #include "opthelpers.h"
  9. using EaxDirtyFlags = unsigned int;
  10. struct EaxAlLowPassParam {
  11. float gain;
  12. float gain_hf;
  13. };
  14. void eax_log_exception(std::string_view message) noexcept;
  15. template<typename TException, typename TValue>
  16. void eax_validate_range(std::string_view value_name, const TValue& value, const TValue& min_value,
  17. const TValue& max_value)
  18. {
  19. if(value >= min_value && value <= max_value) LIKELY
  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