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@15
|
5 namespace goatgfx {
|
nuclear@15
|
6
|
nuclear@0
|
7 template <typename T>
|
nuclear@24
|
8 DataSet<T>::DataSet(T (*create_func)(), bool (*load_func)(T, const char*), bool (*done_func)(T), void (*destr_func)(T))
|
nuclear@0
|
9 {
|
nuclear@24
|
10 create = create_func;
|
nuclear@0
|
11 load = load_func;
|
nuclear@24
|
12 done = done_func;
|
nuclear@0
|
13 destroy = destr_func;
|
nuclear@24
|
14
|
nuclear@24
|
15 rman = resman_create();
|
nuclear@24
|
16 resman_set_load_func(rman, dataset_load_func, this);
|
nuclear@24
|
17 resman_set_done_func(rman, dataset_done_func, this);
|
nuclear@24
|
18 resman_set_destroy_func(rman, dataset_destroy_func, this);
|
nuclear@0
|
19 }
|
nuclear@0
|
20
|
nuclear@0
|
21 template <typename T>
|
nuclear@0
|
22 DataSet<T>::~DataSet()
|
nuclear@0
|
23 {
|
nuclear@24
|
24 resman_free(rman);
|
nuclear@0
|
25 }
|
nuclear@0
|
26
|
nuclear@0
|
27 template <typename T>
|
nuclear@0
|
28 void DataSet<T>::clear()
|
nuclear@0
|
29 {
|
nuclear@24
|
30 resman_free(rman);
|
nuclear@0
|
31 data.clear();
|
nuclear@24
|
32
|
nuclear@24
|
33 rman = resman_create();
|
nuclear@24
|
34 }
|
nuclear@24
|
35
|
nuclear@24
|
36 template <typename T>
|
nuclear@24
|
37 void DataSet<T>::update()
|
nuclear@24
|
38 {
|
nuclear@24
|
39 resman_poll(rman);
|
nuclear@0
|
40 }
|
nuclear@0
|
41
|
nuclear@0
|
42 template <typename T>
|
nuclear@0
|
43 T DataSet<T>::get(const char *name) const
|
nuclear@0
|
44 {
|
nuclear@0
|
45 typename std::map<std::string, T>::const_iterator iter = data.find(name);
|
nuclear@0
|
46 if(iter != data.end()) {
|
nuclear@0
|
47 return iter->second;
|
nuclear@0
|
48 }
|
nuclear@0
|
49
|
nuclear@24
|
50 const char *fname, *slash;
|
nuclear@24
|
51 if((slash = strrchr(name, '/'))) {
|
nuclear@24
|
52 fname = slash + 1;
|
nuclear@24
|
53 } else {
|
nuclear@24
|
54 fname = name;
|
nuclear@24
|
55 }
|
nuclear@24
|
56
|
nuclear@24
|
57 std::string path = datafile_path(fname);
|
nuclear@0
|
58 if(path.empty()) {
|
nuclear@0
|
59 fprintf(stderr, "can't find data file: %s\n", name);
|
nuclear@0
|
60 return 0;
|
nuclear@0
|
61 }
|
nuclear@0
|
62
|
nuclear@24
|
63 T res = create();
|
nuclear@24
|
64 resman_lookup(rman, path.c_str(), res);
|
nuclear@0
|
65 return res;
|
nuclear@0
|
66 }
|
nuclear@15
|
67
|
nuclear@24
|
68
|
nuclear@24
|
69 // --- static functions to pass as callback to resman ---
|
nuclear@24
|
70
|
nuclear@24
|
71 template <typename T>
|
nuclear@24
|
72 int DataSet<T>::dataset_load_func(const char *fname, int id, void *cls)
|
nuclear@24
|
73 {
|
nuclear@24
|
74 DataSet<T> *dset = (DataSet<T>*)cls;
|
nuclear@24
|
75 T data = (T)resman_get_res_data(dset->rman, id);
|
nuclear@24
|
76
|
nuclear@24
|
77 if(!dset->load(data, fname)) {
|
nuclear@24
|
78 return -1;
|
nuclear@24
|
79 }
|
nuclear@24
|
80 return 0;
|
nuclear@24
|
81 }
|
nuclear@24
|
82
|
nuclear@24
|
83 template <typename T>
|
nuclear@24
|
84 int DataSet<T>::dataset_done_func(int id, void *cls)
|
nuclear@24
|
85 {
|
nuclear@24
|
86 DataSet<T> *dset = (DataSet<T>*)cls;
|
nuclear@24
|
87
|
nuclear@24
|
88 T data = (T)resman_get_res_data(dset->rman, id);
|
nuclear@24
|
89 int load_res = resman_get_res_result(dset->rman, id);
|
nuclear@24
|
90
|
nuclear@24
|
91 if(load_res != 0) {
|
nuclear@24
|
92 fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(dset->rman, id));
|
nuclear@24
|
93 } else {
|
nuclear@24
|
94 printf("done loading resource %d (%s)\n", id, resman_get_res_name(dset->rman, id));
|
nuclear@24
|
95 }
|
nuclear@24
|
96
|
nuclear@24
|
97 if(dset->done) {
|
nuclear@24
|
98 dset->done(data);
|
nuclear@24
|
99 }
|
nuclear@24
|
100 return 0;
|
nuclear@24
|
101 }
|
nuclear@24
|
102
|
nuclear@24
|
103 template <typename T>
|
nuclear@24
|
104 void DataSet<T>::dataset_destroy_func(int id, void *cls)
|
nuclear@24
|
105 {
|
nuclear@24
|
106 DataSet<T> *dset = (DataSet<T>*)cls;
|
nuclear@24
|
107 T data = (T)resman_get_res_data(dset->rman, id);
|
nuclear@24
|
108
|
nuclear@24
|
109 if(dset->destroy) {
|
nuclear@24
|
110 dset->destroy(data);
|
nuclear@24
|
111 }
|
nuclear@24
|
112 }
|
nuclear@24
|
113
|
nuclear@15
|
114 } // namespace goatgfx
|