From: Mikko Rasa Date: Thu, 25 Aug 2011 09:16:25 +0000 (+0300) Subject: Exception changes X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7d2b4349289578b8e7f322186a1f251684ddeb01;p=libs%2Fgui.git Exception changes --- diff --git a/source/gbase/display.cpp b/source/gbase/display.cpp index 6ae2906..da036a1 100644 --- a/source/gbase/display.cpp +++ b/source/gbase/display.cpp @@ -5,7 +5,6 @@ #include #endif #endif -#include #include #include #include "display.h" @@ -48,6 +47,11 @@ int x_error_handler(Display *display, XErrorEvent *event) namespace Msp { namespace Graphics { +unsupported_video_mode::unsupported_video_mode(const VideoMode &mode): + runtime_error(format("%dx%d", mode.width, mode.height)) +{ } + + Display::Display(const string &disp_name): priv(new Private) { @@ -77,7 +81,7 @@ Display::Display(const string &disp_name): else priv->display = XOpenDisplay(disp_name.c_str()); if(!priv->display) - throw Exception("Couldn't open X display"); + throw runtime_error("XOpenDisplay"); XSetErrorHandler(x_error_handler); @@ -141,7 +145,9 @@ void Display::set_mode(const VideoMode &mode) info.dmDisplayFrequency = mode.rate; } - ChangeDisplaySettings(&info, CDS_FULLSCREEN); + LONG ret = ChangeDisplaySettings(&info, CDS_FULLSCREEN); + if(ret!=DISP_CHANGE_SUCCESSFUL) + throw unsupported_video_mode(mode); #elif defined(WITH_XF86VIDMODE) int screen = DefaultScreen(priv->display); @@ -163,10 +169,10 @@ void Display::set_mode(const VideoMode &mode) } } - throw InvalidParameterValue("Requested mode not supported"); + throw unsupported_video_mode(mode); #else (void)mode; - throw Exception("Video mode switching not supported"); + throw runtime_error("no xf86vidmode support"); #endif } @@ -229,7 +235,7 @@ void Display::check_error() if(error_flag) { error_flag = false; - throw Exception(error_msg); + throw runtime_error(error_msg); } } diff --git a/source/gbase/display.h b/source/gbase/display.h index c058e41..662e73f 100644 --- a/source/gbase/display.h +++ b/source/gbase/display.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace Msp { @@ -20,6 +21,15 @@ struct VideoMode VideoMode(unsigned w, unsigned h): width(w), height(h), rate(0) { } }; + +class unsupported_video_mode: public std::runtime_error +{ +public: + unsupported_video_mode(const VideoMode &); + virtual ~unsupported_video_mode() throw () { } +}; + + class Display { public: diff --git a/source/gbase/drawcontext.cpp b/source/gbase/drawcontext.cpp index 0f8d44b..0396567 100644 --- a/source/gbase/drawcontext.cpp +++ b/source/gbase/drawcontext.cpp @@ -1,3 +1,4 @@ +#include #ifndef WIN32 #include #include @@ -5,12 +6,13 @@ #include #include #endif -#include #include "display.h" #include "drawcontext.h" #include "window.h" #include "display_priv.h" +using namespace std; + namespace Msp { namespace Graphics { @@ -28,7 +30,7 @@ DrawContext::DrawContext(Window &w): window(w) { #ifdef WIN32 - throw Exception("DrawContext not supported on win32 yet"); + throw runtime_error("no DrawContext support on windows"); #else priv = new Private; @@ -43,7 +45,7 @@ DrawContext::DrawContext(Window &w): { priv->image = XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height); if(!priv->image) - throw Exception("Could not create shared memory XImage"); + throw runtime_error("XShmCreateImage"); priv->shminfo.shmid = shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666); priv->shminfo.shmaddr=priv->image->data = reinterpret_cast(shmat(priv->shminfo.shmid, 0, 0)); @@ -58,7 +60,7 @@ DrawContext::DrawContext(Window &w): { priv->image = XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0); if(!priv->image) - throw Exception("Could not create XImage"); + throw runtime_error("XCreateImage"); priv->image->data = new char[priv->image->bytes_per_line*priv->image->height]; } #endif diff --git a/source/gbase/glcontext.cpp b/source/gbase/glcontext.cpp index 22dca90..b713830 100644 --- a/source/gbase/glcontext.cpp +++ b/source/gbase/glcontext.cpp @@ -9,7 +9,7 @@ #endif #endif #include -#include +#include #include "display.h" #include "glcontext.h" #include "window.h" @@ -26,6 +26,12 @@ GLOptions::GLOptions(): { } +unsupported_gl_mode::unsupported_gl_mode(const GLOptions &opts): + runtime_error(format("{ .alpha=%s, .stencil=%s, .doublebuffer=%s, .multisample=%d }", + opts.alpha, opts.stencil, opts.doublebuffer, opts.multisample)) +{ } + + #ifdef WITH_OPENGL #ifdef WIN32 typedef HGLRC Context; @@ -71,7 +77,7 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts): int pf_index = ChoosePixelFormat(dc, &pfd); if(!pf_index) - throw Exception("Couldn't find a suitable pixel format"); + throw unsupported_gl_mode(opts); SetPixelFormat(dc, pf_index, &pfd); priv->context = wglCreateContext(dc); @@ -112,7 +118,7 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts): XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front()); if(!vi) - throw Exception("Couldn't find a suitable GLX visual"); + throw unsupported_gl_mode(opts); priv->context = glXCreateContext(dpy, vi, 0, true); XSetWindowAttributes attr; @@ -128,7 +134,7 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts): #else (void)wnd; (void)opts; - throw Exception("OpenGL support not compiled in"); + throw runtime_error("no OpenGL support"); #endif window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized)); diff --git a/source/gbase/glcontext.h b/source/gbase/glcontext.h index a719b85..dfac328 100644 --- a/source/gbase/glcontext.h +++ b/source/gbase/glcontext.h @@ -1,6 +1,8 @@ #ifndef MSP_GBASE_GLCONTEXT_H_ #define MSP_GBASE_GLCONTEXT_H_ +#include + namespace Msp { namespace Graphics { @@ -17,6 +19,15 @@ struct GLOptions GLOptions(); }; + +class unsupported_gl_mode: public std::runtime_error +{ +public: + unsupported_gl_mode(const GLOptions &); + virtual ~unsupported_gl_mode() throw () { } +}; + + class GLContext { private: diff --git a/source/gbase/image.cpp b/source/gbase/image.cpp index 24b760e..3360b72 100644 --- a/source/gbase/image.cpp +++ b/source/gbase/image.cpp @@ -6,7 +6,6 @@ #include #include #endif -#include #include "image.h" using namespace std; @@ -64,7 +63,7 @@ void load_png(IO::Base &in, Image::Private &priv) info = png_create_info_struct(png); if(setjmp(png_jmpbuf(png))) - throw Exception("Error loading PNG image"); + throw bad_image_data("PNG error"); png_set_read_fn(png, &in, read); png_read_info(png, info); @@ -76,7 +75,7 @@ void load_png(IO::Base &in, Image::Private &priv) priv.width = width; priv.height = height; if(depth!=8) - throw Exception("Only 8-bit PNG images are supported"); + throw unsupported_image_format("depth!=8"); switch(color) { case PNG_COLOR_TYPE_PALETTE: priv.fmt = COLOR_INDEX; break; @@ -84,7 +83,7 @@ void load_png(IO::Base &in, Image::Private &priv) case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break; case PNG_COLOR_TYPE_RGB: priv.fmt = RGB; break; case PNG_COLOR_TYPE_RGB_ALPHA: priv.fmt = RGBA; break; - default: throw Exception("Unknown color type"); + default: throw unsupported_image_format("unknown color type"); } unsigned nchans = png_get_channels(png, info); @@ -133,7 +132,7 @@ Image::Image(): priv(new Private) { #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG) - throw Exception("Image needs either DevIL or libpng support"); + throw runtime_error("no image support"); #endif } @@ -164,9 +163,9 @@ void Image::load_file(const string &fn) ensure_devil_image(priv->id); ilBindImage(priv->id); if(!ilLoadImage(const_cast(fn.c_str()))) - throw Exception("Error loading image "+fn); + throw bad_image_data("IL error"); #else - throw Exception("Not a PNG image and DevIL support not compiled in"); + throw unsupported_image_format("DevIL needed for non-PNG images"); #endif } (void)fn; @@ -187,9 +186,9 @@ void Image::load_memory(const void *data, unsigned size) ensure_devil_image(priv->id); ilBindImage(priv->id); if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast(data), size)) - throw Exception("Error loading image from memory"); + throw bad_image_data("IL error"); #else - throw Exception("Not a PNG image and DevIL support not compiled in"); + throw unsupported_image_format("DevIL needed for non-PNG images"); #endif } (void)data; @@ -215,7 +214,8 @@ PixelFormat Image::get_format() const case IL_RGBA: return RGBA; case IL_BGR: return BGR; case IL_BGRA: return BGRA; - default: throw InvalidParameterValue("Unknown pixel format in image"); + // XXX bad, should throw when loading + default: throw invalid_argument("unknown pixel format in image"); } } #endif diff --git a/source/gbase/image.h b/source/gbase/image.h index 60dd895..aec1b0d 100644 --- a/source/gbase/image.h +++ b/source/gbase/image.h @@ -7,6 +7,21 @@ namespace Msp { namespace Graphics { +class unsupported_image_format: public std::runtime_error +{ +public: + unsupported_image_format(const std::string &w): std::runtime_error(w) { } + virtual ~unsupported_image_format() throw() { } +}; + +class bad_image_data: public std::runtime_error +{ +public: + bad_image_data(const std::string &w): std::runtime_error(w) { } + virtual ~bad_image_data() throw() { } +}; + + class Image { public: diff --git a/source/gbase/window.cpp b/source/gbase/window.cpp index fbcbd51..77959de 100644 --- a/source/gbase/window.cpp +++ b/source/gbase/window.cpp @@ -9,7 +9,6 @@ #include #endif #include -#include #include "display.h" #include "window.h" #include "display_priv.h" @@ -104,7 +103,7 @@ void Window::init() wndcl.hIconSm = 0; if(!RegisterClassEx(&wndcl)) - throw Exception("Couldn't register window class"); + throw system_error("RegisterClassEx"); wndclass_created = true; } @@ -129,7 +128,7 @@ void Window::init() reinterpret_cast(Application::get_data()), this); if(!priv->window) - throw Exception("CreateWindowEx failed"); + throw system_error("CreateWindowEx"); #else ::Display *dpy = display.get_private().display; diff --git a/source/input/control.cpp b/source/input/control.cpp index 565d5c3..eee0f15 100644 --- a/source/input/control.cpp +++ b/source/input/control.cpp @@ -1,4 +1,3 @@ -#include #include #include "control.h" #include "device.h" @@ -90,8 +89,6 @@ void Control::connect_signals() case AXIS_NEG: src.dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion)); break; - default: - throw Exception("Invalid source in Control"); } } diff --git a/source/input/hub.cpp b/source/input/hub.cpp index 6a78882..cbf9f78 100644 --- a/source/input/hub.cpp +++ b/source/input/hub.cpp @@ -1,7 +1,9 @@ +#include #include -#include #include "hub.h" +using namespace std; + namespace Msp { namespace Input { @@ -24,7 +26,7 @@ std::string Hub::get_button_name(unsigned btn) const { unsigned dev_index = btn>>12; if(dev_index>=devices.size()) - throw InvalidParameterValue("Button does not exist"); + throw invalid_argument("Hub::get_button_name"); const Device &dev = *devices[dev_index]; return dev.get_name()+": "+dev.get_button_name(btn&0xFFF); @@ -34,7 +36,7 @@ std::string Hub::get_axis_name(unsigned axis) const { unsigned dev_index = axis>>12; if(dev_index>=devices.size()) - throw InvalidParameterValue("Axis does not exist"); + throw invalid_argument("Hub::get_axis_name"); const Device &dev = *devices[dev_index]; return dev.get_name()+": "+dev.get_axis_name(axis&0xFFF); diff --git a/source/input/keys.cpp b/source/input/keys.cpp index 082e022..508c06f 100644 --- a/source/input/keys.cpp +++ b/source/input/keys.cpp @@ -1,11 +1,11 @@ #include +#include #ifdef WIN32 #include #else #include #include #endif -#include #include "keys.h" using namespace std; @@ -135,7 +135,7 @@ unsigned key_from_sys(unsigned code) unsigned key_to_sys(unsigned key) { if(key>=N_KEYS_) - throw InvalidParameterValue("Key out of range"); + throw invalid_argument("key_to_sys"); return keymap[key]; }