shadows, textures, resource managers... shaders...
[antikythera] / src / dataset.h
1 /** DataSet is a generic resource database with fast O(logn) lookups by name
2  * it can be used for texture managers, mesh managers, sound effect managers etc
3  *
4  * The constructor takes a load function and a destructor function to be called
5  * when a nonexistent resource is requested and needs to be loaded, and when
6  * the DataSet is destroyed. The destructor is optional and can be set to null
7  * if not needed.
8  *
9  * Requesting a resource works by simply calling get, example:
10  * ----------------------------------------------------------
11  * \code
12  * Texture *load_texture(const char *fname);
13  * void free_texture(Texture *tex);
14  *
15  * DataSet<Texture*> texman(load_texture, free_texture);
16  * Texture *foo = texman.get("foo.png");
17  * \endcode
18  */
19 #ifndef DATASET_H_
20 #define DATASET_H_
21
22 #include <string>
23 #include <map>
24 #include <resman.h>
25
26 template <typename T>
27 class DataSet {
28 protected:
29         mutable std::map<std::string, T> data;
30         mutable struct resman *rman;
31
32         T (*create)();
33         bool (*load)(T, const char*);
34         bool (*done)(T);
35         void (*destroy)(T);
36
37         static int dataset_load_func(const char *fname, int id, void *cls);
38         static int dataset_done_func(int id, void *cls);
39         static void dataset_destroy_func(int id, void *cls);
40
41 public:
42         DataSet(T (*create_func)(), bool (*load_func)(T, const char*), bool (*done_func)(T) = 0, void (*destr_func)(T) = 0);
43         ~DataSet();
44
45         void clear();
46         void update();
47
48         T get(const char *name) const;
49 };
50
51 #include "dataset.inl"
52
53 #endif  // DATASET_H_