Fix compilation with newer versions of DUMB
[alure.git] / src / main.h
blob8d636f082da090c3124da2283b65152e2dc3b4ed
1 #ifndef ALURE_MAIN_H
2 #define ALURE_MAIN_H
4 #include "alure2.h"
6 #include <system_error>
7 #if __cplusplus >= 201703L
8 #include <variant>
9 #else
10 #include "mpark/variant.hpp"
12 namespace std {
13 using mpark::variant;
14 using mpark::monostate;
15 using mpark::get;
16 using mpark::get_if;
17 using mpark::holds_alternative;
18 } // namespace std
19 #endif
21 #ifdef __GNUC__
22 #define LIKELY(x) __builtin_expect(static_cast<bool>(x), true)
23 #define UNLIKELY(x) __builtin_expect(static_cast<bool>(x), false)
24 #else
25 #define LIKELY(x) static_cast<bool>(x)
26 #define UNLIKELY(x) static_cast<bool>(x)
27 #endif
29 #define DECL_THUNK0(ret, C, Name, cv) \
30 ret C::Name() cv { return pImpl->Name(); }
31 #define DECL_THUNK1(ret, C, Name, cv, T1) \
32 ret C::Name(T1 a) cv { return pImpl->Name(std::forward<T1>(a)); }
33 #define DECL_THUNK2(ret, C, Name, cv, T1, T2) \
34 ret C::Name(T1 a, T2 b) cv \
35 { return pImpl->Name(std::forward<T1>(a), std::forward<T2>(b)); }
36 #define DECL_THUNK3(ret, C, Name, cv, T1, T2, T3) \
37 ret C::Name(T1 a, T2 b, T3 c) cv \
38 { \
39 return pImpl->Name(std::forward<T1>(a), std::forward<T2>(b), \
40 std::forward<T3>(c)); \
44 namespace alure {
46 // Need to use these to avoid extraneous commas in macro parameter lists
47 using Vector3Pair = std::pair<Vector3,Vector3>;
48 using UInt64NSecPair = std::pair<uint64_t,std::chrono::nanoseconds>;
49 using SecondsPair = std::pair<Seconds,Seconds>;
50 using ALfloatPair = std::pair<ALfloat,ALfloat>;
51 using ALuintPair = std::pair<ALuint,ALuint>;
52 using BoolTriple = std::tuple<bool,bool,bool>;
55 template<typename T>
56 inline std::future_status GetFutureState(const SharedFuture<T> &future)
57 { return future.wait_for(std::chrono::seconds::zero()); }
59 // This variant is a poor man's optional
60 std::variant<std::monostate,uint64_t> ParseTimeval(StringView strval, double srate) noexcept;
62 template<size_t N>
63 struct Bitfield {
64 private:
65 Array<uint8_t,(N+7)/8> mElems;
67 public:
68 Bitfield() { std::fill(mElems.begin(), mElems.end(), 0); }
70 bool operator[](size_t i) const noexcept
71 { return static_cast<bool>(mElems[i/8] & (1<<(i%8))); }
73 void set(size_t i) noexcept { mElems[i/8] |= 1<<(i%8); }
77 class alc_category : public std::error_category {
78 alc_category() noexcept { }
80 public:
81 static alc_category sSingleton;
83 const char *name() const noexcept override final { return "alc_category"; }
84 std::error_condition default_error_condition(int code) const noexcept override final
85 { return std::error_condition(code, *this); }
87 bool equivalent(int code, const std::error_condition &condition) const noexcept override final
88 { return default_error_condition(code) == condition; }
89 bool equivalent(const std::error_code &code, int condition) const noexcept override final
90 { return *this == code.category() && code.value() == condition; }
92 std::string message(int condition) const override final;
94 template<typename T>
95 inline std::system_error alc_error(int code, T&& what)
96 { return std::system_error(code, alc_category::sSingleton, std::forward<T>(what)); }
97 inline std::system_error alc_error(int code)
98 { return std::system_error(code, alc_category::sSingleton); }
100 class al_category : public std::error_category {
101 al_category() noexcept { }
103 public:
104 static al_category sSingleton;
106 const char *name() const noexcept override final { return "al_category"; }
107 std::error_condition default_error_condition(int code) const noexcept override final
108 { return std::error_condition(code, *this); }
110 bool equivalent(int code, const std::error_condition &condition) const noexcept override final
111 { return default_error_condition(code) == condition; }
112 bool equivalent(const std::error_code &code, int condition) const noexcept override final
113 { return *this == code.category() && code.value() == condition; }
115 std::string message(int condition) const override final;
117 template<typename T>
118 inline std::system_error al_error(int code, T&& what)
119 { return std::system_error(code, al_category::sSingleton, std::forward<T>(what)); }
120 inline std::system_error al_error(int code)
121 { return std::system_error(code, al_category::sSingleton); }
123 inline void throw_al_error(const char *str)
125 ALenum err = alGetError();
126 if(UNLIKELY(err != AL_NO_ERROR))
127 throw al_error(err, str);
130 } // namespace alure
132 #endif /* ALURE_MAIN_H */