source: trunk/src/image/image-private.h @ 2506

Last change on this file since 2506 was 2506, checked in by sam, 7 years ago

base: start removing occurrences of NULL on our long journey to nullptr.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The ImageData class
13// -------------------
14//
15
16#if !defined __LOL_IMAGE_PRIVATE_H__
17#define __LOL_IMAGE_PRIVATE_H__
18
19#include "image.h"
20
21namespace lol
22{
23
24class ImageLoader
25{
26    friend class Image;
27    friend class ImageData;
28
29public:
30    ImageData *(*fun)(char const *path);
31    ImageLoader *next;
32    int priority;
33
34    static void RegisterLoader(ImageLoader *loader)
35    {
36        Helper(loader);
37    }
38
39private:
40    static ImageData *Load(char const *path)
41    {
42        ImageLoader *parser = Helper(nullptr);
43        ImageData *ret = nullptr;
44
45        while (parser && !ret)
46        {
47            ret = parser->fun(path);
48            parser = parser->next;
49        }
50
51        return ret;
52    }
53
54    static ImageLoader *Helper(ImageLoader *set)
55    {
56        static ImageLoader *loaders = nullptr;
57
58        if (!set)
59            return loaders;
60
61        ImageLoader **parser = &loaders;
62        while (*parser && (*parser)->priority > set->priority)
63            parser = &(*parser)->next;
64        set->next = *parser;
65        *parser = set;
66
67        return nullptr;
68    }
69};
70
71class ImageData
72{
73    friend class Image;
74
75public:
76    virtual ~ImageData() {}
77
78    virtual bool Open(char const *) = 0;
79    virtual bool Close() = 0;
80
81    virtual void *GetData() const = 0;
82
83protected:
84    ivec2 size;
85    Image::format_t format;
86};
87
88#define REGISTER_IMAGE_LOADER(name) \
89    extern void Register##name(); \
90    Register##name();
91
92#define DECLARE_IMAGE_LOADER(name, prio) \
93    template<typename T> class name##ImageLoader : public ImageLoader \
94    { \
95    public: \
96        name##ImageLoader() \
97        { \
98            static ImageLoader loader; \
99            loader.fun = Load; \
100            loader.priority = prio; \
101            RegisterLoader(&loader); \
102        } \
103        static ImageData *Load(char const *path) \
104        { \
105            T *ret = new T(); \
106            if (!ret->Open(path)) \
107            { \
108                delete ret; \
109                return nullptr; \
110            } \
111            return ret; \
112        } \
113    }; \
114    class name; \
115    name##ImageLoader<name> name##ImageLoaderInstance; \
116    void Register##name() \
117    { \
118        (void)&name##ImageLoaderInstance; \
119    } \
120    class name : public ImageData
121
122} /* namespace lol */
123
124#endif // __LOL_IMAGE_PRIVATE_H__
125
Note: See TracBrowser for help on using the repository browser.