diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-04-12 17:06:58 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-04-12 17:06:58 +0000 |
commit | 8a4f9e902cf64f97ee2f15fa3940a7cf183a27b7 (patch) | |
tree | 079aaf36f288a420a1bd1e7dd1477ac445837031 /libdimensionxx | |
parent | 6cfdbf44cce0b0881d1811e7ec06c4e79e637c96 (diff) | |
download | dimension-8a4f9e902cf64f97ee2f15fa3940a7cf183a27b7.tar.xz |
Use exceptions to report errors when possible.
Diffstat (limited to 'libdimensionxx')
-rw-r--r-- | libdimensionxx/dimensionxx/error.hpp | 8 | ||||
-rw-r--r-- | libdimensionxx/error.cpp | 5 | ||||
-rw-r--r-- | libdimensionxx/png.cpp | 44 |
3 files changed, 30 insertions, 27 deletions
diff --git a/libdimensionxx/dimensionxx/error.hpp b/libdimensionxx/dimensionxx/error.hpp index 072949f..8919895 100644 --- a/libdimensionxx/dimensionxx/error.hpp +++ b/libdimensionxx/dimensionxx/error.hpp @@ -22,6 +22,8 @@ #define DIMENSIONXX_ERROR_HPP #include <dimension.h> +#include <stdexcept> +#include <string> namespace Dimension { @@ -33,6 +35,12 @@ namespace Dimension Severity resilience(); void resilience(Severity resilience); + + class Dimension_Error : public std::runtime_error + { + public: + Dimension_Error(const std::string& str); + }; } #endif /* DIMENSIONXX_ERROR_HPP */ diff --git a/libdimensionxx/error.cpp b/libdimensionxx/error.cpp index fce453e..948a8c0 100644 --- a/libdimensionxx/error.cpp +++ b/libdimensionxx/error.cpp @@ -19,6 +19,8 @@ *************************************************************************/ #include "dimensionxx.hpp" +#include <stdexcept> +#include <string> namespace Dimension { @@ -33,4 +35,7 @@ namespace Dimension { dmnsn_set_resilience(static_cast<dmnsn_severity>(resilience)); } + + Dimension_Error::Dimension_Error(const std::string& str) + : std::runtime_error(str) { } } diff --git a/libdimensionxx/png.cpp b/libdimensionxx/png.cpp index 14b8c4d..1ca3047 100644 --- a/libdimensionxx/png.cpp +++ b/libdimensionxx/png.cpp @@ -20,6 +20,8 @@ #include "dimensionxx.hpp" #include "../libdimension-png/dimension-png.h" +#include <cstdio> +#include <stdexcept> namespace Dimension { @@ -38,59 +40,47 @@ namespace Dimension void PNG_Canvas::write() { if (m_written) { - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Attempt to write canvas to PNG twice."); - return; + throw Dimension_Error("Attempt to write canvas to PNG twice."); } if (!m_ostr) { - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Attempt to write canvas to PNG without an output stream."); - return; + throw Dimension_Error("Attempt to write canvas to PNG without an output" + " stream."); } FILE* file = fcookie(*m_ostr); if (!file) { - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Couldn't create C++/C IO interface when writing canvas" - " to PNG."); - return; + throw Dimension_Error("Couldn't create C++/C IO interface when writing" + " canvas to PNG."); } if (dmnsn_png_write_canvas(m_canvas, file)) { - fclose(file); - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Writing canvas to PNG failed."); - return; + std::fclose(file); + throw Dimension_Error("Writing canvas to PNG failed."); } - fclose(file); + std::fclose(file); m_written = true; } void PNG_Canvas::read() { if (!m_istr) { - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Attempt to read canvas from PNG without an input stream."); - return; + throw Dimension_Error("Attempt to read canvas from PNG without an input" + " stream."); } FILE* file = fcookie(*m_istr); if (!file) { - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Couldn't create C++/C IO interface when reading canvas" - " from PNG."); - return; + throw Dimension_Error("Couldn't create C++/C IO interface when reading" + " canvas from PNG."); } if (!(m_canvas = dmnsn_png_read_canvas(file))) { - fclose(file); - dmnsn_error(DMNSN_SEVERITY_MEDIUM, - "Reading canvas from PNG failed."); - return; + std::fclose(file); + throw Dimension_Error("Reading canvas from PNG failed."); } - fclose(file); + std::fclose(file); } } |