goat3dgfx

view src/dataset.h @ 24:dc5918c62a64

broken: converting to resman
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Mar 2014 22:04:29 +0200
parents 7d6b667821cf
children
line source
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_
22 #include <string>
23 #include <map>
24 #include <resman.h>
26 namespace goatgfx {
28 template <typename T>
29 class DataSet {
30 protected:
31 mutable std::map<std::string, T> data;
32 mutable struct resman *rman;
34 T (*create)();
35 bool (*load)(T, const char*);
36 bool (*done)(T);
37 void (*destroy)(T);
39 static int dataset_load_func(const char *fname, int id, void *cls);
40 static int dataset_done_func(int id, void *cls);
41 static void dataset_destroy_func(int id, void *cls);
43 public:
44 DataSet(T (*create_func)(), bool (*load_func)(T, const char*), bool (*done_func)(T) = 0, void (*destr_func)(T) = 0);
45 ~DataSet();
47 void clear();
48 void update();
50 T get(const char *name) const;
51 };
53 } // namespace goatgfx
55 #include "dataset.inl"
57 #endif // DATASET_H_