29ae114f9d4daa4cc1b5e0cd14d900d7611963fa
[chaz/yoink] / src / moof / resource.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_RESOURCE_HH_
13 #define _MOOF_RESOURCE_HH_
14
15 /**
16 * \file resource.hh
17 * Interface for textures, sounds, and other types of resources.
18 */
19
20 #include "config.h"
21
22 #include <cstdio>
23 #include <map>
24 #include <stdexcept>
25 #include <string>
26 #include <vector>
27
28 #include <boost/shared_ptr.hpp>
29 #include <boost/function.hpp>
30
31 #include <moof/debug.hh>
32
33
34 namespace moof {
35
36
37 class resource;
38 typedef boost::shared_ptr<resource> resource_ptr;
39
40
41 /**
42 * Generic resource class capable of containing any type of resource,
43 * providing a type-safe interface.
44 */
45 class resource
46 {
47 public:
48
49 // FIXME: this won't be necessary once the existing code is modified to
50 // use the resource handles
51 resource() {}
52
53 /**
54 * Add a directory to search when looking for resource files.
55 * \param paths A colon-separated list of directory paths.
56 */
57 static void add_search_paths(const std::string& paths);
58
59
60 /**
61 * Get the path to a resource of a given name.
62 * \param path The name of the resource to find. Upon successful
63 * return, this is changed to an absolute path to the resource.
64 * \return True if a path to a resource was found, false otherwise.
65 */
66 static bool find(const std::string& file);
67
68 static std::string find_file(const std::string& name);
69
70 /**
71 * Get the path to a resource of a given name and open it if a resource
72 * was found.
73 * \param path The name of the resource to find. Upon successful
74 * return, this is changed to an absolute path to the resource.
75 * \param mode The open mode.
76 * \return The FILE* if the resource was found, 0 otherwise.
77 */
78 static FILE* open_file(const std::string& path,
79 const std::string& mode = "rb");
80
81
82 /**
83 * Register a type with the extension of files which this type can
84 * load. If any type has already been registered with the given file
85 * extension, it will be replaced.
86 * \param extension The file extension.
87 */
88 template <class T>
89 static void register_type(const std::string& extension)
90 {
91 if (!type_lookup_) type_lookup_ = type_lookup_ptr(new type_lookup);
92 loader_ptr loader(new specific_loader<T>);
93 (*type_lookup_)[extension] = loader;
94 }
95
96 /**
97 * Unregister the type associated with a file extension. Resources of
98 * this type will no longer be loadable, although resources which are
99 * already loaded will remain loaded.
100 * \param extension The file extension
101 */
102 static void unregister_type(const std::string& extension)
103 {
104 type_lookup_->erase(extension);
105 }
106
107
108 static resource_ptr load(const std::string& path);
109
110 static resource_ptr reload(std::string& path);
111
112
113 /**
114 * Construct a resource container.
115 * \param ptr A pointer to the underlying resource data.
116 */
117 template <class T>
118 explicit resource(T* ptr) :
119 resource_(ptr),
120 typeinfo_(const_cast<std::type_info*>(&typeid(T))),
121 unloader_(new specific_unloader<T>(ptr)) {}
122
123 /**
124 * Deconstruct a resource container.
125 */
126 virtual ~resource();
127
128
129 /**
130 * Reload the resource data. This will cause the resource file to be
131 * reread, and the underlying resource data will change.
132 */
133 void reload();
134
135
136 /**
137 * Get whether or not the type of the underlying resource data matches
138 * an expected type.
139 * \return True if the types match, false otherwise.
140 */
141 template <class T>
142 bool check() const
143 {
144 return *typeinfo_ == typeid(T);
145 }
146
147 /**
148 * Get a pointer to the underlying resource data as long as the type of
149 * the resource data matches the expected type.
150 * \return The resource data, or null if there is a type mismatch.
151 */
152 template <class T>
153 T* get() const
154 {
155 if (check<T>()) return (T*)resource_;
156 return 0;
157 }
158
159
160 /**
161 * Reloads some resources which have been modified on disk since they
162 * were loaded. Hotloading must have been enabled at compile-time.
163 * \return The number of resources reloaded.
164 */
165 static int reload_as_needed();
166
167
168 private:
169
170 class loader
171 {
172 public:
173
174 virtual ~loader() {}
175
176 virtual resource* load(const std::string& path)
177 {
178 return 0;
179 }
180 };
181
182 typedef boost::shared_ptr<loader> loader_ptr;
183
184 template <class T>
185 class specific_loader : public loader
186 {
187 public:
188
189 virtual resource* load(const std::string& path)
190 {
191 return new resource(new T(path));
192 }
193 };
194
195
196 class unloader
197 {
198 public:
199
200 virtual ~unloader() {};
201 };
202
203 typedef boost::shared_ptr<unloader> unloader_ptr;
204
205 template <class T>
206 class specific_unloader : public unloader
207 {
208 public:
209
210 specific_unloader(T* object = 0) :
211 object_(object) {}
212
213 virtual ~specific_unloader()
214 {
215 log_warning("unloading resource of type ", typeid(T).name());
216 delete object_;
217 }
218
219
220 private:
221
222 T* object_;
223 };
224
225
226 void set_loader(const std::string& path, loader_ptr loader)
227 {
228 path_ = path;
229 loader_ = loader;
230 }
231
232
233 void* resource_;
234 std::type_info* typeinfo_;
235 unloader_ptr unloader_;
236
237 std::string path_;
238 loader_ptr loader_;
239
240 typedef std::map<std::string,loader_ptr> type_lookup;
241 typedef boost::shared_ptr<type_lookup> type_lookup_ptr;
242 static type_lookup_ptr type_lookup_;
243
244 #ifdef USE_HOTLOADING
245 int wd_;
246
247 void set_watch_descriptor(int wd)
248 {
249 wd_ = wd;
250 }
251 #endif
252 };
253
254
255 /**
256 * The resource handle class provides a nicer way to work with resources.
257 * It allows you to work with a resource pointer as if you already know the
258 * type of the resource.
259 */
260 template <class T>
261 class resource_handle
262 {
263 public:
264
265 /**
266 * Construct a null resource handle.
267 */
268 resource_handle() {}
269
270 /**
271 * Construct a resource handle.
272 * \param ptr The resource pointer to reference.
273 */
274 resource_handle(resource_ptr ptr) :
275 resource_(ptr) {}
276
277
278 /**
279 * Get whether or not the handle is dereferenceable to the type of this
280 * handle. A resource handle is dereferenceable if it is not a null
281 * handle and if its underlying resource is in fact the same type as is
282 * expected by the handle.
283 * \return True if the handle is dereferenceable, false otherwise.
284 */
285 operator bool () const
286 {
287 if (!resource_) return false;
288 return resource_->check<T>();
289 }
290
291
292 /**
293 * Get a pointer to the underlying resource.
294 * \return The pointer, or null if this handle is not dereferenceable.
295 */
296 T* get() const
297 {
298 if (!*this) return 0;
299 return resource_->get<T>();
300 }
301
302 /**
303 * Dereference the handle all the way to the underlying resource.
304 * \return A reference to the resource.
305 * \throws std::runtime_error If this is a null handle.
306 */
307 T& get_reference() const
308 {
309 if (!*this) throw std::runtime_error("dereference null handle");
310 return *(resource_->get<T>());
311 }
312
313
314 /**
315 * Same as get() for getting a pointer to the underlying resources.
316 * \return The pointer, or null if this handle is not dereferenceable.
317 */
318 T* operator -> () const
319 {
320 return get();
321 }
322
323 /**
324 * Same a get_reference() for dereferencing the handle.
325 * \return A reference to the resource.
326 * \throws std::runtime_error If this is a null handle.
327 */
328 T& operator * () const
329 {
330 return get_reference();
331 }
332
333
334 private:
335
336 resource_ptr resource_;
337 };
338
339
340 } // namespace moof
341
342 #endif // _MOOF_RESOURCE_HH_
343
This page took 0.042249 seconds and 3 git commands to generate.