]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/imageloader.h
Add some state checking to ImageLoader
[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 public:
27         enum State
28         {
29                 INITIAL,
30                 FINISHED
31         };
32
33 protected:
34         class RegisterBase
35         {
36         protected:
37                 RegisterBase() { }
38         public:
39                 virtual ~RegisterBase() { }
40
41                 virtual unsigned get_signature_size() const = 0;
42                 virtual bool detect(const std::string &) const = 0;
43                 virtual ImageLoader *create(IO::Seekable &) const = 0;
44         };
45
46         template<typename T>
47         class RegisteredLoader: public RegisterBase
48         {
49         public:
50                 virtual unsigned get_signature_size() const { return T::get_signature_size(); }
51                 virtual bool detect(const std::string &s) const { return T::detect(s); }
52                 virtual ImageLoader *create(IO::Seekable &io) const { return new T(io); }
53         };
54
55         struct Registry
56         {
57                 std::list<RegisterBase *> loaders;
58                 bool changed;
59
60                 Registry();
61                 ~Registry();
62         };
63
64 private:
65         IO::Base *source;
66         State state;
67
68 protected:
69         ImageLoader();
70 public:
71         virtual ~ImageLoader();
72
73         static ImageLoader *open_file(const std::string &);
74         static ImageLoader *open_io(IO::Seekable &);
75
76         virtual void load(Image::Data &);
77 protected:
78         virtual void load_(Image::Data &) = 0;
79
80 public:
81         State get_state() const { return state; }
82
83         template<typename T>
84         static void register_loader();
85 private:
86         static Registry &get_registry();
87
88         static bool signature_size_compare(RegisterBase *, RegisterBase *);
89 };
90
91 template<typename T>
92 void ImageLoader::register_loader()
93 {
94         Registry &registry = get_registry();
95         registry.loaders.push_back(new RegisteredLoader<T>);
96         registry.changed = true;
97 }
98
99 } // namespace Graphics
100 } // namespace Msp
101
102 #endif