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