diff --git a/src/Magnum/Audio/Context.cpp b/src/Magnum/Audio/Context.cpp index 9c5d0437a..58977313e 100644 --- a/src/Magnum/Audio/Context.cpp +++ b/src/Magnum/Audio/Context.cpp @@ -70,6 +70,13 @@ Debug& operator<<(Debug& debug, const Context::HrtfStatus value) { Context* Context::_current = nullptr; +bool Context::hasCurrent() { return _current; } + +Context& Context::current() { + CORRADE_ASSERT(_current, "Audio::Context::current(): no current context", *_current); + return *_current; +} + Context::Context(): Context{Configuration{}} {} Context::Context(const Configuration& config) { diff --git a/src/Magnum/Audio/Context.h b/src/Magnum/Audio/Context.h index 23dfa8142..f44782c20 100644 --- a/src/Magnum/Audio/Context.h +++ b/src/Magnum/Audio/Context.h @@ -127,8 +127,20 @@ class MAGNUM_AUDIO_EXPORT Context { UnsupportedFormat = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT }; - /** @brief Current context */ - static Context* current() { return _current; } + /** + * @brief Whether there is any current context + * + * @see @ref current() + */ + static bool hasCurrent(); + + /** + * @brief Current context + * + * Expect that there is current context. + * @see @ref hasCurrent() + */ + static Context& current(); class Configuration; @@ -151,6 +163,11 @@ class MAGNUM_AUDIO_EXPORT Context { */ ~Context(); + #if defined(MAGNUM_BUILD_DEPRECATED) && !defined(DOXYGEN_GENERATING_OUTPUT) + CORRADE_DEPRECATED("Audio::Context::current() returns reference now") Context* operator->() { return this; } + CORRADE_DEPRECATED("Audio::Context::current() returns reference now") operator Context*() { return this; } + #endif + /** * @brief Whether HRTFs (Head Related Transfer Functions) are enabled * @@ -265,7 +282,7 @@ class MAGNUM_AUDIO_EXPORT Context { } private: - static Context* _current; + MAGNUM_AUDIO_LOCAL static Context* _current; /* Create a context with given configuration. Returns `true` on success. * @ref alcCreateContext(). */ @@ -410,9 +427,9 @@ MAGNUM_ASSERT_AUDIO_EXTENSION_SUPPORTED(Extensions::ALC::SOFTX::HRTF); #ifdef CORRADE_NO_ASSERT #define MAGNUM_ASSERT_AUDIO_EXTENSION_SUPPORTED(extension) do {} while(0) #else -#define MAGNUM_ASSERT_AUDIO_EXTENSION_SUPPORTED(extension) \ +#define MAGNUM_ASSERT_AUDIO_EXTENSION_SUPPORTED(extension) \ do { \ - if(!Magnum::Audio::Context::current()->isExtensionSupported()) { \ + if(!Magnum::Audio::Context::current().isExtensionSupported()) { \ Corrade::Utility::Error() << "Magnum: required OpenAL extension" << extension::string() << "is not supported"; \ std::abort(); \ } \ @@ -429,7 +446,7 @@ inline bool Context::isHrtfEnabled() const { } inline Context::HrtfStatus Context::hrtfStatus() const { - if(!Context::current()->isExtensionSupported()) + if(!Context::current().isExtensionSupported()) return isHrtfEnabled() ? HrtfStatus::Enabled : HrtfStatus::Disabled; Int status; diff --git a/src/Magnum/Audio/Test/ContextTest.cpp b/src/Magnum/Audio/Test/ContextTest.cpp index dbd384ceb..652488904 100644 --- a/src/Magnum/Audio/Test/ContextTest.cpp +++ b/src/Magnum/Audio/Test/ContextTest.cpp @@ -56,7 +56,7 @@ void ContextTest::extensionsString() { } void ContextTest::isExtensionEnabled() { - CORRADE_VERIFY(Context::current()->isExtensionSupported()); + CORRADE_VERIFY(Context::current().isExtensionSupported()); } void ContextTest::hrtfStatus() {