From 3cb70f504293d04c0a62d16a11312e30a44f15bc Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Mon, 6 Sep 2021 01:57:39 +0300 Subject: [PATCH] Simplify SDLWrap with NonNull --- Source/utils/sdl_wrap.h | 48 ++++++++++++----------------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/Source/utils/sdl_wrap.h b/Source/utils/sdl_wrap.h index af3005567..e3ae3f15e 100644 --- a/Source/utils/sdl_wrap.h +++ b/Source/utils/sdl_wrap.h @@ -14,31 +14,27 @@ namespace devilution { namespace SDLWrap { -inline SDLSurfaceUniquePtr CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) +template +T NonNull(T x) { - SDLSurfaceUniquePtr ret { SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask) }; - if (ret == nullptr) + if (x == nullptr) ErrSdl(); + return x; +} - return ret; +inline SDLSurfaceUniquePtr CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) +{ + return SDLSurfaceUniquePtr { NonNull(SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask)) }; } inline SDLSurfaceUniquePtr CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format) { - SDLSurfaceUniquePtr ret { SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format) }; - if (ret == nullptr) - ErrSdl(); - - return ret; + return SDLSurfaceUniquePtr { NonNull(SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format)) }; } inline SDLSurfaceUniquePtr CreateRGBSurfaceWithFormatFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 format) { - SDLSurfaceUniquePtr ret { SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, depth, pitch, format) }; - if (ret == nullptr) - ErrSdl(); - - return ret; + return SDLSurfaceUniquePtr { NonNull(SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, depth, pitch, format)) }; } #ifndef USE_SDL1 @@ -47,42 +43,26 @@ inline SDLSurfaceUniquePtr ConvertSurface(SDL_Surface *src, const SDL_PixelForma inline SDLSurfaceUniquePtr ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags) #endif { - SDLSurfaceUniquePtr ret { SDL_ConvertSurface(src, fmt, flags) }; - if (ret == nullptr) - ErrSdl(); - - return ret; + return SDLSurfaceUniquePtr { NonNull(SDL_ConvertSurface(src, fmt, flags)) }; } #ifndef USE_SDL1 inline SDLSurfaceUniquePtr ConvertSurfaceFormat(SDL_Surface *src, Uint32 pixel_format, Uint32 flags) { - SDLSurfaceUniquePtr ret { SDL_ConvertSurfaceFormat(src, pixel_format, flags) }; - if (ret == nullptr) - ErrSdl(); - - return ret; + return SDLSurfaceUniquePtr { NonNull(SDL_ConvertSurfaceFormat(src, pixel_format, flags)) }; } #endif #ifndef USE_SDL1 inline SDLTextureUniquePtr CreateTexture(SDL_Renderer *renderer, Uint32 format, int access, int w, int h) { - SDLTextureUniquePtr ret { SDL_CreateTexture(renderer, format, access, w, h) }; - if (ret == nullptr) - ErrSdl(); - - return ret; + return SDLTextureUniquePtr { NonNull(SDL_CreateTexture(renderer, format, access, w, h)) }; } #endif inline SDLPaletteUniquePtr AllocPalette(int ncolors) { - SDLPaletteUniquePtr ret { SDL_AllocPalette(ncolors) }; - if (ret == nullptr) - ErrSdl(); - - return ret; + return SDLPaletteUniquePtr { NonNull(SDL_AllocPalette(ncolors)) }; } } //namespace SDLWrap