From 2344840588ba759d7a83d584e1a36b7bedde073b Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 16 Nov 2017 04:48:46 -0800 Subject: [PATCH] Mark the nothrow methods explicitly noexcept --- include/AL/alure2.h | 10 +++++----- src/device.cpp | 44 ++++++++++++++++++++++++++++++-------------- src/device.h | 2 +- src/devicemanager.cpp | 36 ++++++++++++++++++++++-------------- src/devicemanager.h | 2 +- 5 files changed, 59 insertions(+), 35 deletions(-) diff --git a/include/AL/alure2.h b/include/AL/alure2.h index 63aeca7..65d369e 100644 --- a/include/AL/alure2.h +++ b/include/AL/alure2.h @@ -345,11 +345,11 @@ public: * Opens the playback device given by name, or the default if blank. * Returns an empty Device on error. */ - Device openPlayback(const String &name, const std::nothrow_t&); - Device openPlayback(const char *name, const std::nothrow_t&); + Device openPlayback(const String &name, const std::nothrow_t&) noexcept; + Device openPlayback(const char *name, const std::nothrow_t&) noexcept; /** Opens the default playback device. Returns an empty Device on error. */ - Device openPlayback(const std::nothrow_t&); + Device openPlayback(const std::nothrow_t&) noexcept; }; @@ -425,8 +425,8 @@ public: * Creates a new Context on this device, using the specified attributes. * Returns an empty Context if context creation fails. */ - Context createContext(ArrayView attributes, const std::nothrow_t&); - Context createContext(const std::nothrow_t&); + Context createContext(ArrayView attributes, const std::nothrow_t&) noexcept; + Context createContext(const std::nothrow_t&) noexcept; /** * Pauses device processing, stopping updates for its contexts. Multiple diff --git a/src/device.cpp b/src/device.cpp index bfeca09..b2a38cd 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -223,13 +223,8 @@ void DeviceImpl::reset(ArrayView attributes) } -Context Device::createContext(ArrayView attrs) -{ return pImpl->createContext(attrs, true); } -Context Device::createContext(ArrayView attrs, const std::nothrow_t&) -{ return pImpl->createContext(attrs, false); } -Context Device::createContext(const std::nothrow_t&) -{ return pImpl->createContext(ArrayView(), false); } -Context DeviceImpl::createContext(ArrayView attributes, bool dothrow) +DECL_THUNK1(Context, Device, createContext,, ArrayView) +Context DeviceImpl::createContext(ArrayView attributes) { ALCcontext *ctx = nullptr; if(attributes.empty()) /* No explicit attributes. */ @@ -254,15 +249,36 @@ Context DeviceImpl::createContext(ArrayView attributes, bool doth else ctx = alcCreateContext(mDevice, &std::get<0>(attributes.front())); } - if(!ctx) + if(ctx) { - if(dothrow) - throw std::runtime_error("Failed to create context"); - return Context(); + try { + mContexts.emplace_back(MakeUnique(ctx, this)); + return Context(mContexts.back().get()); + } + catch(...) { + alcDestroyContext(ctx); + throw; + } } - - mContexts.emplace_back(MakeUnique(ctx, this)); - return Context(mContexts.back().get()); + throw std::runtime_error("Failed to create context"); +} +Context Device::createContext(ArrayView attrs, const std::nothrow_t&) noexcept +{ + try { + return pImpl->createContext(attrs); + } + catch(...) { + } + return Context(); +} +Context Device::createContext(const std::nothrow_t&) noexcept +{ + try { + return pImpl->createContext({}); + } + catch(...) { + } + return Context(); } diff --git a/src/device.h b/src/device.h index fa52535..791dc60 100644 --- a/src/device.h +++ b/src/device.h @@ -59,7 +59,7 @@ public: String getCurrentHRTF() const; void reset(ArrayView attributes); - Context createContext(ArrayView attributes, bool dothrow); + Context createContext(ArrayView attributes); void pauseDSP(); void resumeDSP(); diff --git a/src/devicemanager.cpp b/src/devicemanager.cpp index 6f67731..1442d30 100644 --- a/src/devicemanager.cpp +++ b/src/devicemanager.cpp @@ -82,27 +82,35 @@ DECL_THUNK1(Device, DeviceManager, openPlayback,, const char*) Device DeviceManagerImpl::openPlayback(const char *name) { ALCdevice *dev = alcOpenDevice(name); - if(!dev) + if(dev) { - if(!name || !name[0]) - throw std::runtime_error("Failed to open default device"); - throw std::runtime_error(StringView("Failed to open device \"")+name+"\""); + try { + mDevices.emplace_back(MakeUnique(dev)); + return Device(mDevices.back().get()); + } + catch(...) { + alcCloseDevice(dev); + throw; + } } - mDevices.emplace_back(MakeUnique(dev)); - return Device(mDevices.back().get()); + if(!name || !name[0]) + throw std::runtime_error("Failed to open default device"); + throw std::runtime_error(StringView("Failed to open device \"")+name+"\""); } -Device DeviceManager::openPlayback(const String &name, const std::nothrow_t &nt) +Device DeviceManager::openPlayback(const String &name, const std::nothrow_t &nt) noexcept { return pImpl->openPlayback(name.c_str(), nt); } -Device DeviceManager::openPlayback(const std::nothrow_t &) +Device DeviceManager::openPlayback(const std::nothrow_t&) noexcept { return pImpl->openPlayback(nullptr, std::nothrow); } -DECL_THUNK2(Device, DeviceManager, openPlayback,, const char*, const std::nothrow_t&) -Device DeviceManagerImpl::openPlayback(const char *name, const std::nothrow_t&) +DECL_THUNK2(Device, DeviceManager, openPlayback, noexcept, const char*, const std::nothrow_t&) +Device DeviceManagerImpl::openPlayback(const char *name, const std::nothrow_t&) noexcept { - ALCdevice *dev = alcOpenDevice(name); - if(!dev) return Device(); - mDevices.emplace_back(MakeUnique(dev)); - return Device(mDevices.back().get()); + try { + openPlayback(name); + } + catch(...) { + } + return Device(); } void DeviceManagerImpl::removeDevice(DeviceImpl *dev) diff --git a/src/devicemanager.h b/src/devicemanager.h index 9f79847..3123464 100644 --- a/src/devicemanager.h +++ b/src/devicemanager.h @@ -24,7 +24,7 @@ public: String defaultDeviceName(DefaultDeviceType type) const; Device openPlayback(const char *name); - Device openPlayback(const char *name, const std::nothrow_t&); + Device openPlayback(const char *name, const std::nothrow_t&) noexcept; }; } // namespace alure -- 2.11.4.GIT