goat3dgfx

view src/dataset.h @ 13:25bf39105c82

lalal
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Nov 2013 08:08:59 +0200
parents
children 7d6b667821cf
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>
25 template <typename T>
26 class DataSet {
27 protected:
28 mutable std::map<std::string, T> data;
30 T (*load)(const char*);
31 void (*destroy)(T);
33 public:
34 DataSet(T (*load_func)(const char*), void (*destr_func)(T) = 0);
35 ~DataSet();
37 void clear();
39 T get(const char *name) const;
40 };
42 #include "dataset.inl"
44 #endif // DATASET_H_