From 0885a7e155653acbbc1b15eff590feabbb6f00d9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 19 Dec 2014 05:24:52 +0200 Subject: [PATCH] Add a helper class for registering image loaders --- source/graphics/devil/devilloader.cpp | 2 -- source/graphics/devil/devilloader.h | 4 +-- source/graphics/imageloader.h | 34 +++++++++++++++++-------- source/graphics/jpeg/jpegloader.cpp | 2 -- source/graphics/jpeg/jpegloader.h | 4 +-- source/graphics/png/pngloader.cpp | 1 - source/graphics/png/pngloader.h | 4 +-- source/graphics/quartz/quartzloader.cpp | 2 -- source/graphics/quartz/quartzloader.h | 4 +-- 9 files changed, 27 insertions(+), 30 deletions(-) diff --git a/source/graphics/devil/devilloader.cpp b/source/graphics/devil/devilloader.cpp index 131b6ba..ab962d1 100644 --- a/source/graphics/devil/devilloader.cpp +++ b/source/graphics/devil/devilloader.cpp @@ -74,8 +74,6 @@ std::string error_string(ILenum err) namespace Msp { namespace Graphics { -ImageLoader::Register DevilLoader::reg; - DevilLoader::DevilLoader(IO::Seekable &i): io(i) { diff --git a/source/graphics/devil/devilloader.h b/source/graphics/devil/devilloader.h index daab2a2..e94a31b 100644 --- a/source/graphics/devil/devilloader.h +++ b/source/graphics/devil/devilloader.h @@ -6,14 +6,12 @@ namespace Msp { namespace Graphics { -class DevilLoader: public ImageLoader +class DevilLoader: public RegisteredImageLoader { private: IO::Base &io; unsigned id; - static Register reg; - public: DevilLoader(IO::Seekable &); virtual ~DevilLoader(); diff --git a/source/graphics/imageloader.h b/source/graphics/imageloader.h index c06a759..85e6b52 100644 --- a/source/graphics/imageloader.h +++ b/source/graphics/imageloader.h @@ -23,7 +23,7 @@ public: class ImageLoader { -private: +protected: class RegisterBase { protected: @@ -36,16 +36,6 @@ private: virtual ImageLoader *create(IO::Seekable &) const = 0; }; -protected: - template - class Register: RegisterBase - { - public: - virtual unsigned get_signature_size() const { return T::get_signature_size(); } - virtual bool detect(const std::string &s) const { return T::detect(s); } - virtual T *create(IO::Seekable &io) const { return new T(io); } - }; - private: IO::Base *source; @@ -64,6 +54,28 @@ public: virtual void load(Image::Data &) = 0; }; + +template +class RegisteredImageLoader: public ImageLoader +{ +private: + class Register: public RegisterBase + { + public: + virtual unsigned get_signature_size() const { return T::get_signature_size(); } + virtual bool detect(const std::string &s) const { return T::detect(s); } + virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); } + }; + + static Register reg; + +protected: + RegisteredImageLoader() { (void)reg; } +}; + +template +typename RegisteredImageLoader::Register RegisteredImageLoader::reg; + } // namespace Graphics } // namespace Msp diff --git a/source/graphics/jpeg/jpegloader.cpp b/source/graphics/jpeg/jpegloader.cpp index 9aadcb9..844446c 100644 --- a/source/graphics/jpeg/jpegloader.cpp +++ b/source/graphics/jpeg/jpegloader.cpp @@ -84,8 +84,6 @@ struct JpegLoader::Private }; -ImageLoader::Register JpegLoader::reg; - JpegLoader::JpegLoader(IO::Seekable &io): priv(new Private) { diff --git a/source/graphics/jpeg/jpegloader.h b/source/graphics/jpeg/jpegloader.h index d45e599..b93d5bc 100644 --- a/source/graphics/jpeg/jpegloader.h +++ b/source/graphics/jpeg/jpegloader.h @@ -6,15 +6,13 @@ namespace Msp { namespace Graphics { -class JpegLoader: public ImageLoader +class JpegLoader: public RegisteredImageLoader { private: struct Private; Private *priv; - static Register reg; - public: JpegLoader(IO::Seekable &); virtual ~JpegLoader(); diff --git a/source/graphics/png/pngloader.cpp b/source/graphics/png/pngloader.cpp index 35e6a97..ede91b0 100644 --- a/source/graphics/png/pngloader.cpp +++ b/source/graphics/png/pngloader.cpp @@ -31,7 +31,6 @@ struct PngLoader::Private png_info *info; }; -ImageLoader::Register PngLoader::reg; PngLoader::PngLoader(IO::Base &io, unsigned sig_bytes): priv(new Private) diff --git a/source/graphics/png/pngloader.h b/source/graphics/png/pngloader.h index 683f4d3..de8b535 100644 --- a/source/graphics/png/pngloader.h +++ b/source/graphics/png/pngloader.h @@ -6,15 +6,13 @@ namespace Msp { namespace Graphics { -class PngLoader: public ImageLoader +class PngLoader: public RegisteredImageLoader { private: struct Private; Private *priv; - static Register reg; - public: PngLoader(IO::Base &, unsigned = 0); virtual ~PngLoader(); diff --git a/source/graphics/quartz/quartzloader.cpp b/source/graphics/quartz/quartzloader.cpp index d1baaef..2821190 100644 --- a/source/graphics/quartz/quartzloader.cpp +++ b/source/graphics/quartz/quartzloader.cpp @@ -55,8 +55,6 @@ struct QuartzLoader::Private }; -ImageLoader::Register QuartzLoader::reg; - QuartzLoader::QuartzLoader(IO::Seekable &io): priv(new Private) { diff --git a/source/graphics/quartz/quartzloader.h b/source/graphics/quartz/quartzloader.h index d3bef7b..963e45c 100644 --- a/source/graphics/quartz/quartzloader.h +++ b/source/graphics/quartz/quartzloader.h @@ -6,15 +6,13 @@ namespace Msp { namespace Graphics { -class QuartzLoader: public ImageLoader +class QuartzLoader: public RegisteredImageLoader { private: struct Private; Private *priv; - static Register reg; - public: QuartzLoader(IO::Seekable &); virtual ~QuartzLoader(); -- 2.45.2