conworlds
annotate 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 |
rev | line source |
---|---|
nuclear@13 | 1 #include <stdio.h> |
nuclear@13 | 2 #include <string.h> |
nuclear@13 | 3 |
nuclear@13 | 4 template <typename T> |
nuclear@13 | 5 DataSet<T>::DataSet(T (*load_func)(const char*), void (*destr_func)(T)) |
nuclear@13 | 6 { |
nuclear@13 | 7 load = load_func; |
nuclear@13 | 8 destroy = destr_func; |
nuclear@13 | 9 } |
nuclear@13 | 10 |
nuclear@13 | 11 template <typename T> |
nuclear@13 | 12 DataSet<T>::~DataSet() |
nuclear@13 | 13 { |
nuclear@13 | 14 clear(); |
nuclear@13 | 15 } |
nuclear@13 | 16 |
nuclear@13 | 17 template <typename T> |
nuclear@13 | 18 void DataSet<T>::clear() |
nuclear@13 | 19 { |
nuclear@13 | 20 if(destroy) { |
nuclear@13 | 21 typename std::map<std::string, T>::iterator it = data.begin(); |
nuclear@13 | 22 while(it != data.end()) { |
nuclear@13 | 23 destroy(it++->second); |
nuclear@13 | 24 } |
nuclear@13 | 25 } |
nuclear@13 | 26 data.clear(); |
nuclear@13 | 27 } |
nuclear@13 | 28 |
nuclear@13 | 29 template <typename T> |
nuclear@13 | 30 T DataSet<T>::get(const char *name) const |
nuclear@13 | 31 { |
nuclear@13 | 32 typename std::map<std::string, T>::const_iterator iter = data.find(name); |
nuclear@13 | 33 if(iter != data.end()) { |
nuclear@13 | 34 return iter->second; |
nuclear@13 | 35 } |
nuclear@13 | 36 |
nuclear@13 | 37 const char *fname, *slash; |
nuclear@13 | 38 if((slash = strrchr(name, '/'))) { |
nuclear@13 | 39 fname = slash + 1; |
nuclear@13 | 40 } else { |
nuclear@13 | 41 fname = name; |
nuclear@13 | 42 } |
nuclear@13 | 43 |
nuclear@13 | 44 std::string path = fname; |
nuclear@13 | 45 if(path.empty()) { |
nuclear@13 | 46 fprintf(stderr, "can't find data file: %s\n", name); |
nuclear@13 | 47 return 0; |
nuclear@13 | 48 } |
nuclear@13 | 49 |
nuclear@13 | 50 T res = load(path.c_str()); |
nuclear@13 | 51 if(res) { |
nuclear@13 | 52 data[name] = res; |
nuclear@13 | 53 } |
nuclear@13 | 54 return res; |
nuclear@13 | 55 } |