]> git.tdb.fi Git - ext/openal.git/blob - al/eax/utils.h
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / al / eax / utils.h
1 #ifndef EAX_UTILS_INCLUDED
2 #define EAX_UTILS_INCLUDED
3
4 #include <algorithm>
5 #include <cstdint>
6 #include <string>
7 #include <type_traits>
8
9 using EaxDirtyFlags = unsigned int;
10
11 struct EaxAlLowPassParam {
12     float gain;
13     float gain_hf;
14 };
15
16 void eax_log_exception(const char *message) noexcept;
17
18 template<typename TException, typename TValue>
19 void eax_validate_range(
20     const char* value_name,
21     const TValue& value,
22     const TValue& min_value,
23     const TValue& max_value)
24 {
25     if (value >= min_value && value <= max_value)
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
35     throw TException{message.c_str()};
36 }
37
38 namespace detail {
39
40 template<typename T>
41 struct EaxIsBitFieldStruct {
42 private:
43     using yes = std::true_type;
44     using no = std::false_type;
45
46     template<typename U>
47     static auto test(int) -> decltype(std::declval<typename U::EaxIsBitFieldStruct>(), yes{});
48
49     template<typename>
50     static no test(...);
51
52 public:
53     static constexpr auto value = std::is_same<decltype(test<T>(0)), yes>::value;
54 };
55
56 template<typename T, typename TValue>
57 inline bool eax_bit_fields_are_equal(const T& lhs, const T& rhs) noexcept
58 {
59     static_assert(sizeof(T) == sizeof(TValue), "Invalid type size.");
60     return reinterpret_cast<const TValue&>(lhs) == reinterpret_cast<const TValue&>(rhs);
61 }
62
63 } // namespace detail
64
65 template<
66     typename T,
67     std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
68 >
69 inline bool operator==(const T& lhs, const T& rhs) noexcept
70 {
71     using Value = std::conditional_t<
72         sizeof(T) == 1,
73         std::uint8_t,
74         std::conditional_t<
75             sizeof(T) == 2,
76             std::uint16_t,
77             std::conditional_t<
78                 sizeof(T) == 4,
79                 std::uint32_t,
80                 void>>>;
81
82     static_assert(!std::is_same<Value, void>::value, "Unsupported type.");
83     return detail::eax_bit_fields_are_equal<T, Value>(lhs, rhs);
84 }
85
86 template<
87     typename T,
88     std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
89 >
90 inline bool operator!=(const T& lhs, const T& rhs) noexcept
91 {
92     return !(lhs == rhs);
93 }
94
95 #endif // !EAX_UTILS_INCLUDED