conworlds
annotate src/dataset.h @ 15:9b0db7dbde6e
hmmm doesn't work properly yes
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 24 Aug 2014 14:36:00 +0300 |
parents | |
children |
rev | line source |
---|---|
nuclear@13 | 1 /** DataSet is a generic resource database with fast O(logn) lookups by name |
nuclear@13 | 2 * it can be used for texture managers, mesh managers, sound effect managers etc |
nuclear@13 | 3 * |
nuclear@13 | 4 * The constructor takes a load function and a destructor function to be called |
nuclear@13 | 5 * when a nonexistent resource is requested and needs to be loaded, and when |
nuclear@13 | 6 * the DataSet is destroyed. The destructor is optional and can be set to null |
nuclear@13 | 7 * if not needed. |
nuclear@13 | 8 * |
nuclear@13 | 9 * Requesting a resource works by simply calling get, example: |
nuclear@13 | 10 * ---------------------------------------------------------- |
nuclear@13 | 11 * \code |
nuclear@13 | 12 * Texture *load_texture(const char *fname); |
nuclear@13 | 13 * void free_texture(Texture *tex); |
nuclear@13 | 14 * |
nuclear@13 | 15 * DataSet<Texture*> texman(load_texture, free_texture); |
nuclear@13 | 16 * Texture *foo = texman.get("foo.png"); |
nuclear@13 | 17 * \endcode |
nuclear@13 | 18 */ |
nuclear@13 | 19 #ifndef DATASET_H_ |
nuclear@13 | 20 #define DATASET_H_ |
nuclear@13 | 21 |
nuclear@13 | 22 #include <string> |
nuclear@13 | 23 #include <map> |
nuclear@13 | 24 |
nuclear@13 | 25 template <typename T> |
nuclear@13 | 26 class DataSet { |
nuclear@13 | 27 protected: |
nuclear@13 | 28 mutable std::map<std::string, T> data; |
nuclear@13 | 29 |
nuclear@13 | 30 T (*load)(const char*); |
nuclear@13 | 31 void (*destroy)(T); |
nuclear@13 | 32 |
nuclear@13 | 33 public: |
nuclear@13 | 34 DataSet(T (*load_func)(const char*), void (*destr_func)(T) = 0); |
nuclear@13 | 35 ~DataSet(); |
nuclear@13 | 36 |
nuclear@13 | 37 void clear(); |
nuclear@13 | 38 |
nuclear@13 | 39 T get(const char *name) const; |
nuclear@13 | 40 }; |
nuclear@13 | 41 |
nuclear@13 | 42 #include "dataset.inl" |
nuclear@13 | 43 |
nuclear@13 | 44 #endif // DATASET_H_ |