]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Dirty hack to pull the image loaders in when linking statically
[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         friend class ImageLoader;
62
63 private:
64         class Register: public RegisterBase
65         {
66         public:
67                 virtual unsigned get_signature_size() const { return T::get_signature_size(); }
68                 virtual bool detect(const std::string &s) const { return T::detect(s); }
69                 virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); }
70         };
71
72         static Register reg;
73
74 protected:
75         RegisteredImageLoader() { (void)reg; }
76 };
77
78 template<typename T>
79 typename RegisteredImageLoader<T>::Register RegisteredImageLoader<T>::reg;
80
81 } // namespace Graphics
82 } // namespace Msp
83
84 #endif