Add an inline helper to get a SharedFuture's state
[alure.git] / src / main.h
blob8d6ecb0971f781f342f3ceb895efa5e8674fcaa8
1 #ifndef ALURE_MAIN_H
2 #define ALURE_MAIN_H
4 #include "alure2.h"
6 #include <system_error>
9 #ifdef __GNUC__
10 #define LIKELY(x) __builtin_expect(static_cast<bool>(x), true)
11 #define UNLIKELY(x) __builtin_expect(static_cast<bool>(x), false)
12 #else
13 #define LIKELY(x) static_cast<bool>(x)
14 #define UNLIKELY(x) static_cast<bool>(x)
15 #endif
17 #define DECL_THUNK0(ret, C, Name, cv) \
18 ret C::Name() cv { return pImpl->Name(); }
19 #define DECL_THUNK1(ret, C, Name, cv, T1) \
20 ret C::Name(T1 a) cv \
21 { \
22 using _t1 = T1; \
23 return pImpl->Name(std::forward<_t1>(a)); \
25 #define DECL_THUNK2(ret, C, Name, cv, T1, T2) \
26 ret C::Name(T1 a, T2 b) cv \
27 { \
28 using _t1 = T1; using _t2 = T2; \
29 return pImpl->Name(std::forward<_t1>(a), std::forward<_t2>(b)); \
31 #define DECL_THUNK3(ret, C, Name, cv, T1, T2, T3) \
32 ret C::Name(T1 a, T2 b, T3 c) cv \
33 { \
34 using _t1 = T1; using _t2 = T2; using _t3 = T3; \
35 return pImpl->Name(std::forward<_t1>(a), std::forward<_t2>(b), \
36 std::forward<_t3>(c)); \
38 #define DECL_THUNK6(ret, C, Name, cv, T1, T2, T3, T4, T5, T6) \
39 ret C::Name(T1 a, T2 b, T3 c, T4 d, T5 e, T6 f) cv \
40 { \
41 using _t1 = T1; using _t2 = T2; using _t3 = T3; \
42 using _t4 = T4; using _t5 = T5; using _t6 = T6; \
43 return pImpl->Name(std::forward<_t1>(a), std::forward<_t2>(b), \
44 std::forward<_t3>(c), std::forward<_t4>(d), \
45 std::forward<_t5>(e), std::forward<_t6>(f)); \
49 namespace alure {
51 template<typename T>
52 inline std::future_status GetFutureState(const SharedFuture<T> &future)
53 { return future.wait_for(std::chrono::seconds::zero()); }
55 template<size_t N>
56 struct Bitfield {
57 private:
58 std::array<uint8_t,(N+7)/8> mElems;
60 public:
61 bool operator[](size_t i) const { return mElems[i/8] & (1<<(i%8)); }
63 void clear() { std::fill(mElems.begin(), mElems.end(), 0); }
64 void set(size_t i) { mElems[i/8] |= 1<<(i%8); }
68 class alc_category : public std::error_category {
69 alc_category() noexcept { }
71 public:
72 static alc_category sSingleton;
74 const char *name() const noexcept override final { return "alc_category"; }
75 std::error_condition default_error_condition(int code) const noexcept override final
76 { return std::error_condition(code, *this); }
78 bool equivalent(int code, const std::error_condition &condition) const noexcept override final
79 { return default_error_condition(code) == condition; }
80 bool equivalent(const std::error_code &code, int condition) const noexcept override final
81 { return *this == code.category() && code.value() == condition; }
83 std::string message(int condition) const override final;
85 template<typename T>
86 inline std::system_error alc_error(int code, T&& what)
87 { return std::system_error(code, alc_category::sSingleton, std::forward<T>(what)); }
88 inline std::system_error alc_error(int code)
89 { return std::system_error(code, alc_category::sSingleton); }
91 class al_category : public std::error_category {
92 al_category() noexcept { }
94 public:
95 static al_category sSingleton;
97 const char *name() const noexcept override final { return "al_category"; }
98 std::error_condition default_error_condition(int code) const noexcept override final
99 { return std::error_condition(code, *this); }
101 bool equivalent(int code, const std::error_condition &condition) const noexcept override final
102 { return default_error_condition(code) == condition; }
103 bool equivalent(const std::error_code &code, int condition) const noexcept override final
104 { return *this == code.category() && code.value() == condition; }
106 std::string message(int condition) const override final;
108 template<typename T>
109 inline std::system_error al_error(int code, T&& what)
110 { return std::system_error(code, al_category::sSingleton, std::forward<T>(what)); }
111 inline std::system_error al_error(int code)
112 { return std::system_error(code, al_category::sSingleton); }
114 } // namespace alure
116 #endif /* ALURE_MAIN_H */