goat3dgfx
view src/dataset.inl @ 34:3eb6c8f89fe1
merge
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Mar 2014 17:41:10 +0200 |
parents | 7d6b667821cf |
children |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "datapath.h"
5 namespace goatgfx {
7 template <typename T>
8 DataSet<T>::DataSet(T (*create_func)(), bool (*load_func)(T, const char*), bool (*done_func)(T), void (*destr_func)(T))
9 {
10 create = create_func;
11 load = load_func;
12 done = done_func;
13 destroy = destr_func;
15 rman = resman_create();
16 resman_set_load_func(rman, dataset_load_func, this);
17 resman_set_done_func(rman, dataset_done_func, this);
18 resman_set_destroy_func(rman, dataset_destroy_func, this);
19 }
21 template <typename T>
22 DataSet<T>::~DataSet()
23 {
24 resman_free(rman);
25 }
27 template <typename T>
28 void DataSet<T>::clear()
29 {
30 resman_free(rman);
31 data.clear();
33 rman = resman_create();
34 }
36 template <typename T>
37 void DataSet<T>::update()
38 {
39 resman_poll(rman);
40 }
42 template <typename T>
43 T DataSet<T>::get(const char *name) const
44 {
45 typename std::map<std::string, T>::const_iterator iter = data.find(name);
46 if(iter != data.end()) {
47 return iter->second;
48 }
50 const char *fname, *slash;
51 if((slash = strrchr(name, '/'))) {
52 fname = slash + 1;
53 } else {
54 fname = name;
55 }
57 std::string path = datafile_path(fname);
58 if(path.empty()) {
59 fprintf(stderr, "can't find data file: %s\n", name);
60 return 0;
61 }
63 T res = create();
64 resman_lookup(rman, path.c_str(), res);
65 return res;
66 }
69 // --- static functions to pass as callback to resman ---
71 template <typename T>
72 int DataSet<T>::dataset_load_func(const char *fname, int id, void *cls)
73 {
74 DataSet<T> *dset = (DataSet<T>*)cls;
75 T data = (T)resman_get_res_data(dset->rman, id);
77 if(!dset->load(data, fname)) {
78 return -1;
79 }
80 return 0;
81 }
83 template <typename T>
84 int DataSet<T>::dataset_done_func(int id, void *cls)
85 {
86 DataSet<T> *dset = (DataSet<T>*)cls;
88 T data = (T)resman_get_res_data(dset->rman, id);
89 int load_res = resman_get_res_result(dset->rman, id);
91 if(load_res != 0) {
92 fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(dset->rman, id));
93 } else {
94 printf("done loading resource %d (%s)\n", id, resman_get_res_name(dset->rman, id));
95 }
97 if(dset->done) {
98 dset->done(data);
99 }
100 return 0;
101 }
103 template <typename T>
104 void DataSet<T>::dataset_destroy_func(int id, void *cls)
105 {
106 DataSet<T> *dset = (DataSet<T>*)cls;
107 T data = (T)resman_get_res_data(dset->rman, id);
109 if(dset->destroy) {
110 dset->destroy(data);
111 }
112 }
114 } // namespace goatgfx