dungeon_crawler
view prototype/src/dataset.h @ 75:b05ab29cd17d
color grading done
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 22 Oct 2012 03:55:00 +0300 |
parents | 7f52d6310317 |
children |
line source
1 #ifndef DATASET_H_
2 #define DATASET_H_
4 #include <string.h>
5 #include <string>
6 #include <map>
7 #include <functional>
8 #include "datapath.h"
10 template <typename T>
11 class DataSet {
12 private:
13 mutable std::map<std::string, T> data;
15 std::function<T(const char*)> load;
16 std::function<void(T)> destroy;
18 public:
19 DataSet(std::function<T(const char*)> load_func, std::function<void(T)> destr_func);
20 ~DataSet();
22 T get(const char *name) const;
23 };
25 template <typename T>
26 DataSet<T>::DataSet(std::function<T(const char*)> load_func, std::function<void(T)> destr_func)
27 {
28 load = load_func;
29 destroy = destr_func;
30 }
32 template <typename T>
33 DataSet<T>::~DataSet()
34 {
35 if(destroy) {
36 for(auto it : data) {
37 destroy(it.second);
38 }
39 }
40 }
42 template <typename T>
43 T DataSet<T>::get(const char *name) const
44 {
45 auto iter = data.find(name);
46 if(iter != data.end()) {
47 return iter->second;
48 }
50 const char *fname, *slash;
51 if((slash = strrchr(name, '/'))) {
52 fname = slash + 1;
53 } else {
54 fname = name;
55 }
57 std::string path = datafile_path(fname);
58 if(path.empty()) {
59 fprintf(stderr, "can't find data file: %s\n", name);
60 return 0;
61 }
63 T res = load(path.c_str());
64 if(res) {
65 data[name] = res;
66 }
67 return res;
68 }
70 #endif // DATASET_H_