From 3622b0ea080737b2cec682bcc794a22984983b1b Mon Sep 17 00:00:00 2001 From: Dimitri Diakopoulos Date: Thu, 24 Dec 2015 14:34:39 -0600 Subject: [PATCH] begin to deprecate error codes in favor of exceptions --- include/libnyquist/AudioDecoder.h | 12 ++++++++---- include/libnyquist/AudioDevice.h | 2 +- src/AudioDecoder.cpp | 29 ++++++++++++----------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/include/libnyquist/AudioDecoder.h b/include/libnyquist/AudioDecoder.h index 3ee5a35..d665077 100644 --- a/include/libnyquist/AudioDecoder.h +++ b/include/libnyquist/AudioDecoder.h @@ -30,17 +30,21 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include namespace nqr { + + struct UnsupportedExtensionException : public std::runtime_error + { + UnsupportedExtensionException() : std::runtime_error("Unsupported file extension") {} + }; // Individual decoder classes will throw std::exceptions for bad things, // but NyquistIO implements this enum for high-level error notifications. enum IOError { NoError, - NoDecodersLoaded, - ExtensionNotSupported, LoadPathNotImplemented, LoadBufferNotImplemented, UnknownError @@ -63,8 +67,8 @@ public: NyquistIO(); ~NyquistIO(); - int Load(AudioData * data, const std::string & path); - int Load(AudioData *data, std::string extension, const std::vector & buffer); + void Load(AudioData * data, const std::string & path); + void Load(AudioData *data, std::string extension, const std::vector & buffer); bool IsFileSupported(const std::string path) const; diff --git a/include/libnyquist/AudioDevice.h b/include/libnyquist/AudioDevice.h index 0327dd9..b3b2922 100644 --- a/include/libnyquist/AudioDevice.h +++ b/include/libnyquist/AudioDevice.h @@ -38,7 +38,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace nqr { - + const uint32_t FRAME_SIZE = 512; const int32_t CHANNELS = 2; const int32_t BUFFER_LENGTH = FRAME_SIZE * CHANNELS; diff --git a/src/AudioDecoder.cpp b/src/AudioDecoder.cpp index 9392a26..1938717 100644 --- a/src/AudioDecoder.cpp +++ b/src/AudioDecoder.cpp @@ -42,7 +42,7 @@ NyquistIO::NyquistIO() NyquistIO::~NyquistIO() { } -int NyquistIO::Load(AudioData * data, const std::string & path) +void NyquistIO::Load(AudioData * data, const std::string & path) { if (IsFileSupported(path)) { @@ -53,30 +53,27 @@ int NyquistIO::Load(AudioData * data, const std::string & path) try { - return decoder->LoadFromPath(data, path); + decoder->LoadFromPath(data, path); } - catch (std::exception e) + catch (const std::exception & e) { - std::cerr << "Caught fatal exception: " << e.what() << std::endl; + std::cerr << "Caught internal exception: " << e.what() << std::endl; } } - return IOError::NoDecodersLoaded; + throw std::runtime_error("No available decoders."); } else { - return IOError::ExtensionNotSupported; + throw UnsupportedExtensionException(); } - - // Should never be reached - return IOError::UnknownError; } -int NyquistIO::Load(AudioData * data, std::string extension, const std::vector & buffer) +void NyquistIO::Load(AudioData * data, std::string extension, const std::vector & buffer) { if (decoderTable.find(extension) == decoderTable.end()) { - return IOError::ExtensionNotSupported; + throw UnsupportedExtensionException(); } if (decoderTable.size()) @@ -84,20 +81,18 @@ int NyquistIO::Load(AudioData * data, std::string extension, const std::vectorLoadFromBuffer(data, buffer); + decoder->LoadFromBuffer(data, buffer); } - catch (std::exception e) + catch (const std::exception & e) { - std::cerr << "Caught fatal exception: " << e.what() << std::endl; + std::cerr << "Caught internal exception: " << e.what() << std::endl; } } else { - return IOError::NoDecodersLoaded; + throw std::runtime_error("No available decoders."); } - // Should never be reached - return IOError::UnknownError; } bool NyquistIO::IsFileSupported(const std::string path) const