]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Add a helper class for registering image loaders
[libs/gui.git] / source / graphics / imageloader.h
1 #ifndef MSP_GRAPHICS_IMAGELOADER_H_
2 #define MSP_GRAPHICS_IMAGELOADER_H_
3
4 #include "image.h"
5
6 namespace Msp {
7 namespace Graphics {
8
9 class unsupported_image_format: public std::runtime_error
10 {
11 public:
12         unsupported_image_format(const std::string &w): std::runtime_error(w) { }
13         virtual ~unsupported_image_format() throw() { }
14 };
15
16 class bad_image_data: public std::runtime_error
17 {
18 public:
19         bad_image_data(const std::string &w): std::runtime_error(w) { }
20         virtual ~bad_image_data() throw() { }
21 };
22
23
24 class ImageLoader
25 {
26 protected:
27         class RegisterBase
28         {
29         protected:
30                 RegisterBase();
31         public:
32                 virtual ~RegisterBase() { }
33
34                 virtual unsigned get_signature_size() const = 0;
35                 virtual bool detect(const std::string &) const = 0;
36                 virtual ImageLoader *create(IO::Seekable &) const = 0;
37         };
38
39 private:
40         IO::Base *source;
41
42         static std::list<RegisterBase *> &get_registered_loaders();
43         static bool registered_loaders_changed;
44         static bool signature_size_compare(RegisterBase *, RegisterBase *);
45
46 protected:
47         ImageLoader();
48 public:
49         virtual ~ImageLoader();
50
51         static ImageLoader *open_file(const std::string &);
52         static ImageLoader *open_io(IO::Seekable &);
53
54         virtual void load(Image::Data &) = 0;
55 };
56
57
58 template<typename T>
59 class RegisteredImageLoader: public ImageLoader
60 {
61 private:
62         class Register: public RegisterBase
63         {
64         public:
65                 virtual unsigned get_signature_size() const { return T::get_signature_size(); }
66                 virtual bool detect(const std::string &s) const { return T::detect(s); }
67                 virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); }
68         };
69
70         static Register reg;
71
72 protected:
73         RegisteredImageLoader() { (void)reg; }
74 };
75
76 template<typename T>
77 typename RegisteredImageLoader<T>::Register RegisteredImageLoader<T>::reg;
78
79 } // namespace Graphics
80 } // namespace Msp
81
82 #endif