conworlds
view src/dataset.inl @ 17:c814f77d177e
moved to SDL2
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 25 Aug 2014 22:02:08 +0300 |
parents | |
children |
line source
1 #include <stdio.h>
2 #include <string.h>
4 template <typename T>
5 DataSet<T>::DataSet(T (*load_func)(const char*), void (*destr_func)(T))
6 {
7 load = load_func;
8 destroy = destr_func;
9 }
11 template <typename T>
12 DataSet<T>::~DataSet()
13 {
14 clear();
15 }
17 template <typename T>
18 void DataSet<T>::clear()
19 {
20 if(destroy) {
21 typename std::map<std::string, T>::iterator it = data.begin();
22 while(it != data.end()) {
23 destroy(it++->second);
24 }
25 }
26 data.clear();
27 }
29 template <typename T>
30 T DataSet<T>::get(const char *name) const
31 {
32 typename std::map<std::string, T>::const_iterator iter = data.find(name);
33 if(iter != data.end()) {
34 return iter->second;
35 }
37 const char *fname, *slash;
38 if((slash = strrchr(name, '/'))) {
39 fname = slash + 1;
40 } else {
41 fname = name;
42 }
44 std::string path = fname;
45 if(path.empty()) {
46 fprintf(stderr, "can't find data file: %s\n", name);
47 return 0;
48 }
50 T res = load(path.c_str());
51 if(res) {
52 data[name] = res;
53 }
54 return res;
55 }