Remove a couple unnecessary casts
[openal-soft.git] / al / eax / utils.h
blob8e0f975f177c571115550614e45d9de03766da28
1 #ifndef EAX_UTILS_INCLUDED
2 #define EAX_UTILS_INCLUDED
4 #include <algorithm>
5 #include <cstdint>
6 #include <string>
7 #include <string_view>
8 #include <type_traits>
10 #include "opthelpers.h"
12 using EaxDirtyFlags = unsigned int;
14 struct EaxAlLowPassParam {
15 float gain;
16 float gain_hf;
19 void eax_log_exception(std::string_view message) noexcept;
21 template<typename TException, typename TValue>
22 void eax_validate_range(std::string_view value_name, const TValue& value, const TValue& min_value,
23 const TValue& max_value)
25 if(value >= min_value && value <= max_value) LIKELY
26 return;
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) + ").";
35 throw TException{message.c_str()};
38 namespace detail {
40 template<typename T>
41 struct EaxIsBitFieldStruct {
42 private:
43 using yes = std::true_type;
44 using no = std::false_type;
46 template<typename U>
47 static auto test(int) -> decltype(std::declval<typename U::EaxIsBitFieldStruct>(), yes{});
49 template<typename>
50 static no test(...);
52 public:
53 static constexpr auto value = std::is_same<decltype(test<T>(0)), yes>::value;
56 template<typename T, typename TValue>
57 inline bool eax_bit_fields_are_equal(const T& lhs, const T& rhs) noexcept
59 static_assert(sizeof(T) == sizeof(TValue), "Invalid type size.");
60 return reinterpret_cast<const TValue&>(lhs) == reinterpret_cast<const TValue&>(rhs);
63 } // namespace detail
65 template<
66 typename T,
67 std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
69 inline bool operator==(const T& lhs, const T& rhs) noexcept
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>>>;
82 static_assert(!std::is_same<Value, void>::value, "Unsupported type.");
83 return detail::eax_bit_fields_are_equal<T, Value>(lhs, rhs);
86 template<
87 typename T,
88 std::enable_if_t<detail::EaxIsBitFieldStruct<T>::value, int> = 0
90 inline bool operator!=(const T& lhs, const T& rhs) noexcept
92 return !(lhs == rhs);
95 #endif // !EAX_UTILS_INCLUDED