X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=antikythera;a=blobdiff_plain;f=src%2Fdataset.h;fp=src%2Fdataset.h;h=a4736c4c6da748af843d9a3db207c845bf592f88;hp=0000000000000000000000000000000000000000;hb=ccc1a688b59e25bb934a0d3e2bbf477960068d4f;hpb=080d7a779d43f549fc16c44e709cbf5989180fdf diff --git a/src/dataset.h b/src/dataset.h new file mode 100644 index 0000000..a4736c4 --- /dev/null +++ b/src/dataset.h @@ -0,0 +1,53 @@ +/** DataSet is a generic resource database with fast O(logn) lookups by name + * it can be used for texture managers, mesh managers, sound effect managers etc + * + * The constructor takes a load function and a destructor function to be called + * when a nonexistent resource is requested and needs to be loaded, and when + * the DataSet is destroyed. The destructor is optional and can be set to null + * if not needed. + * + * Requesting a resource works by simply calling get, example: + * ---------------------------------------------------------- + * \code + * Texture *load_texture(const char *fname); + * void free_texture(Texture *tex); + * + * DataSet texman(load_texture, free_texture); + * Texture *foo = texman.get("foo.png"); + * \endcode + */ +#ifndef DATASET_H_ +#define DATASET_H_ + +#include +#include +#include + +template +class DataSet { +protected: + mutable std::map data; + mutable struct resman *rman; + + T (*create)(); + bool (*load)(T, const char*); + bool (*done)(T); + void (*destroy)(T); + + static int dataset_load_func(const char *fname, int id, void *cls); + static int dataset_done_func(int id, void *cls); + static void dataset_destroy_func(int id, void *cls); + +public: + DataSet(T (*create_func)(), bool (*load_func)(T, const char*), bool (*done_func)(T) = 0, void (*destr_func)(T) = 0); + ~DataSet(); + + void clear(); + void update(); + + T get(const char *name) const; +}; + +#include "dataset.inl" + +#endif // DATASET_H_