vrshoot

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