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