From: Mikko Rasa Date: Sun, 7 Feb 2021 14:36:12 +0000 (+0200) Subject: Add some state checking to ImageLoader X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2ebdf45974a0a7649b3488f9da4b8cf90a1db584;p=libs%2Fgui.git Add some state checking to ImageLoader Reject attemps to call load again on the same loader --- diff --git a/source/graphics/bmploader.cpp b/source/graphics/bmploader.cpp index f711dc1..39629f7 100644 --- a/source/graphics/bmploader.cpp +++ b/source/graphics/bmploader.cpp @@ -44,7 +44,7 @@ bool BmpLoader::detect(const std::string &sig) return !sig.compare(0, sizeof(bmp_sig), bmp_sig, sizeof(bmp_sig)); } -void BmpLoader::load(Image::Data &data) +void BmpLoader::load_(Image::Data &data) { char bm_header[14]; read_full(io, bm_header+sig_bytes, sizeof(bm_header)-sig_bytes); diff --git a/source/graphics/bmploader.h b/source/graphics/bmploader.h index 273e104..9ae8f4e 100644 --- a/source/graphics/bmploader.h +++ b/source/graphics/bmploader.h @@ -18,7 +18,7 @@ public: static unsigned get_signature_size() { return 2; } static bool detect(const std::string &); - virtual void load(Image::Data &); + virtual void load_(Image::Data &); }; } // namespace Graphics diff --git a/source/graphics/devil/devilloader.cpp b/source/graphics/devil/devilloader.cpp index e837154..423b108 100644 --- a/source/graphics/devil/devilloader.cpp +++ b/source/graphics/devil/devilloader.cpp @@ -101,7 +101,7 @@ bool DevilLoader::detect(const string &sig) return type!=IL_TYPE_UNKNOWN; } -void DevilLoader::load(Image::Data &data) +void DevilLoader::load_(Image::Data &data) { ilSetRead(0, 0, eof, get, read, seek, tell); ilBindImage(id); diff --git a/source/graphics/devil/devilloader.h b/source/graphics/devil/devilloader.h index 42ac2b2..3cba0d5 100644 --- a/source/graphics/devil/devilloader.h +++ b/source/graphics/devil/devilloader.h @@ -19,7 +19,7 @@ public: static unsigned get_signature_size() { return 128; } static bool detect(const std::string &); - virtual void load(Image::Data &); + virtual void load_(Image::Data &); }; } // namespace Graphics diff --git a/source/graphics/imageloader.cpp b/source/graphics/imageloader.cpp index b2a126e..2c6ce21 100644 --- a/source/graphics/imageloader.cpp +++ b/source/graphics/imageloader.cpp @@ -22,7 +22,8 @@ namespace Msp { namespace Graphics { ImageLoader::ImageLoader(): - source(0) + source(0), + state(INITIAL) { } ImageLoader::~ImageLoader() @@ -83,6 +84,15 @@ ImageLoader *ImageLoader::open_io(IO::Seekable &io) return loader; } +void ImageLoader::load(Image::Data &data) +{ + if(state>=FINISHED) + throw logic_error("already loaded"); + + load_(data); + state = FINISHED; +} + ImageLoader::Registry &ImageLoader::get_registry() { static Registry registry; diff --git a/source/graphics/imageloader.h b/source/graphics/imageloader.h index 9dc1726..cb3f54a 100644 --- a/source/graphics/imageloader.h +++ b/source/graphics/imageloader.h @@ -23,6 +23,13 @@ public: class ImageLoader { +public: + enum State + { + INITIAL, + FINISHED + }; + protected: class RegisterBase { @@ -56,6 +63,7 @@ protected: private: IO::Base *source; + State state; protected: ImageLoader(); @@ -65,7 +73,12 @@ public: static ImageLoader *open_file(const std::string &); static ImageLoader *open_io(IO::Seekable &); - virtual void load(Image::Data &) = 0; + virtual void load(Image::Data &); +protected: + virtual void load_(Image::Data &) = 0; + +public: + State get_state() const { return state; } template static void register_loader(); diff --git a/source/graphics/jpeg/jpegloader.cpp b/source/graphics/jpeg/jpegloader.cpp index 9b7150d..f61f651 100644 --- a/source/graphics/jpeg/jpegloader.cpp +++ b/source/graphics/jpeg/jpegloader.cpp @@ -119,7 +119,7 @@ bool JpegLoader::detect(const string &sig) return !sig.compare(0, sizeof(jpeg_sig), jpeg_sig, sizeof(jpeg_sig)); } -void JpegLoader::load(Image::Data &data) +void JpegLoader::load_(Image::Data &data) { if(setjmp(priv->err_mgr.jmp)) throw bad_image_data(priv->err_mgr.message); diff --git a/source/graphics/jpeg/jpegloader.h b/source/graphics/jpeg/jpegloader.h index 04e8917..bf902d6 100644 --- a/source/graphics/jpeg/jpegloader.h +++ b/source/graphics/jpeg/jpegloader.h @@ -20,7 +20,7 @@ public: static unsigned get_signature_size() { return 3; } static bool detect(const std::string &); - virtual void load(Image::Data &); + virtual void load_(Image::Data &); }; } // namespace Graphics diff --git a/source/graphics/png/pngloader.cpp b/source/graphics/png/pngloader.cpp index 537dcc1..16ad36b 100644 --- a/source/graphics/png/pngloader.cpp +++ b/source/graphics/png/pngloader.cpp @@ -54,7 +54,7 @@ bool PngLoader::detect(const std::string &sig) return !png_sig_cmp(reinterpret_cast(const_cast(sig.data())), 0, sig.size()); } -void PngLoader::load(Image::Data &data) +void PngLoader::load_(Image::Data &data) { png_byte **rows = 0; diff --git a/source/graphics/png/pngloader.h b/source/graphics/png/pngloader.h index 6ed57cc..21c1c72 100644 --- a/source/graphics/png/pngloader.h +++ b/source/graphics/png/pngloader.h @@ -20,7 +20,7 @@ public: static unsigned get_signature_size() { return 8; } static bool detect(const std::string &); - virtual void load(Image::Data &); + virtual void load_(Image::Data &); }; } // namespace Graphics diff --git a/source/graphics/quartz/quartzloader.cpp b/source/graphics/quartz/quartzloader.cpp index 5493e45..472e066 100644 --- a/source/graphics/quartz/quartzloader.cpp +++ b/source/graphics/quartz/quartzloader.cpp @@ -81,7 +81,7 @@ bool QuartzLoader::detect(const string &sig) return status==kCGImageStatusIncomplete || status==kCGImageStatusReadingHeader; } -void QuartzLoader::load(Image::Data &data) +void QuartzLoader::load_(Image::Data &data) { CGImageRef image = CGImageSourceCreateImageAtIndex(priv->source, 0, 0); if(!image) diff --git a/source/graphics/quartz/quartzloader.h b/source/graphics/quartz/quartzloader.h index 4885ff5..cf4a447 100644 --- a/source/graphics/quartz/quartzloader.h +++ b/source/graphics/quartz/quartzloader.h @@ -22,7 +22,7 @@ public: static unsigned get_signature_size() { return 12; } static bool detect(const std::string &); - virtual void load(Image::Data &); + virtual void load_(Image::Data &); }; } // namespace Graphics