goat3dgfx

diff src/dataset.h @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7d6b667821cf
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dataset.h	Thu Nov 14 05:27:09 2013 +0200
     1.3 @@ -0,0 +1,44 @@
     1.4 +/** DataSet is a generic resource database with fast O(logn) lookups by name
     1.5 + * it can be used for texture managers, mesh managers, sound effect managers etc
     1.6 + *
     1.7 + * The constructor takes a load function and a destructor function to be called
     1.8 + * when a nonexistent resource is requested and needs to be loaded, and when
     1.9 + * the DataSet is destroyed. The destructor is optional and can be set to null
    1.10 + * if not needed.
    1.11 + *
    1.12 + * Requesting a resource works by simply calling get, example:
    1.13 + * ----------------------------------------------------------
    1.14 + * \code
    1.15 + * Texture *load_texture(const char *fname);
    1.16 + * void free_texture(Texture *tex);
    1.17 + *
    1.18 + * DataSet<Texture*> texman(load_texture, free_texture);
    1.19 + * Texture *foo = texman.get("foo.png");
    1.20 + * \endcode
    1.21 + */
    1.22 +#ifndef DATASET_H_
    1.23 +#define DATASET_H_
    1.24 +
    1.25 +#include <string>
    1.26 +#include <map>
    1.27 +
    1.28 +template <typename T>
    1.29 +class DataSet {
    1.30 +protected:
    1.31 +	mutable std::map<std::string, T> data;
    1.32 +
    1.33 +	T (*load)(const char*);
    1.34 +	void (*destroy)(T);
    1.35 +
    1.36 +public:
    1.37 +	DataSet(T (*load_func)(const char*), void (*destr_func)(T) = 0);
    1.38 +	~DataSet();
    1.39 +
    1.40 +	void clear();
    1.41 +
    1.42 +	T get(const char *name) const;
    1.43 +};
    1.44 +
    1.45 +#include "dataset.inl"
    1.46 +
    1.47 +#endif	// DATASET_H_