vrshoot

diff src/dataset.inl @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dataset.inl	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,56 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include "datapath.h"
     1.7 +
     1.8 +template <typename T>
     1.9 +DataSet<T>::DataSet(T (*load_func)(const char*), void (*destr_func)(T))
    1.10 +{
    1.11 +	load = load_func;
    1.12 +	destroy = destr_func;
    1.13 +}
    1.14 +
    1.15 +template <typename T>
    1.16 +DataSet<T>::~DataSet()
    1.17 +{
    1.18 +	clear();
    1.19 +}
    1.20 +
    1.21 +template <typename T>
    1.22 +void DataSet<T>::clear()
    1.23 +{
    1.24 +	if(destroy) {
    1.25 +		typename std::map<std::string, T>::iterator it = data.begin();
    1.26 +		while(it != data.end()) {
    1.27 +			destroy(it++->second);
    1.28 +		}
    1.29 +	}
    1.30 +	data.clear();
    1.31 +}
    1.32 +
    1.33 +template <typename T>
    1.34 +T DataSet<T>::get(const char *name) const
    1.35 +{
    1.36 +	typename std::map<std::string, T>::const_iterator iter = data.find(name);
    1.37 +	if(iter != data.end()) {
    1.38 +		return iter->second;
    1.39 +	}
    1.40 +
    1.41 +	const char *fname, *slash;
    1.42 +	if((slash = strrchr(name, '/'))) {
    1.43 +		fname = slash + 1;
    1.44 +	} else {
    1.45 +		fname = name;
    1.46 +	}
    1.47 +
    1.48 +	std::string path = datafile_path(fname);
    1.49 +	if(path.empty()) {
    1.50 +		fprintf(stderr, "can't find data file: %s\n", name);
    1.51 +		return 0;
    1.52 +	}
    1.53 +
    1.54 +	T res = load(path.c_str());
    1.55 +	if(res) {
    1.56 +		data[name] = res;
    1.57 +	}
    1.58 +	return res;
    1.59 +}