]>
Dogcows Code - chaz/yoink/blob - src/moof/resource.hh
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
10 #ifndef _MOOF_RESOURCE_HH_
11 #define _MOOF_RESOURCE_HH_
15 * Interface for textures, sounds, and other types of resources.
23 #include <boost/shared_ptr.hpp>
24 #include <boost/function.hpp>
26 #include <moof/debug.hh>
33 typedef boost::shared_ptr
<resource
> resource_ptr
;
36 * Generic resource class capable of containing any type of resource,
37 * providing a type-safe interface.
44 * Add a directory to search when looking for resource files.
45 * \param paths A colon-separated list of directory paths.
47 static void set_search_paths(const std::string
& paths
);
50 * Get the path to a resource of a given name. This uses the search
51 * path(s) and resources prefixes to locate resource files.
52 * \param name The name or partial path of the resource to find.
53 * \return The full path of the resource.
55 static std::string
find_file(const std::string
& name
);
58 * Get the path to a resource of a given name and explicit type. This
59 * uses the search path(s) and resources prefixes to locate resource
61 * \param name The name or partial path of the resource to find.
62 * \param ext The extension is appended to the name if the same
63 * extension is not already a part of name.
64 * \return The full path of the resource.
67 find_file(const std::string
& name
, const std::string
& ext
);
70 * Register a type with the extension of files which this type can
71 * load. If any type has already been registered with the given file
72 * extension, it will be replaced.
73 * \param extension The file extension.
76 static void register_type(const std::string
& extension
,
77 const std::string
& prefix
= "")
79 loader_ptr
loader(new specific_loader
<T
>(prefix
));
80 call_registry(extension
, loader
, set
);
84 * Unregister the type associated with a file extension. Resources of
85 * this type will no longer be loadable, although resources which are
86 * already loaded will remain loaded.
87 * \param extension The file extension.
89 static void unregister_type(const std::string
& extension
)
92 call_registry(extension
, loader
, set
);
96 * Find and load a resource by name or path.
97 * \param name The name or partial path of the resource. This should
98 * include the extension so that the correct loader can be chosen.
99 * \return The resource.
101 static resource_ptr
load(const std::string
& name
);
104 * Find and load a resource by name or path.
105 * \param name The name or partial path of the resource. This should
106 * include the extension so that the correct loader can be chosen.
108 * \return The resource.
111 load(const std::string
& name
, const std::string
& ext
);
114 * Reload the resource data. This will cause the resource file to be
115 * reread, and the underlying resource data will change.
120 * Get the path of file from which this resource was loaded.
123 std::string
path() const
129 * Reloads some resources which have been modified on disk since they
130 * were loaded. Hotloading must have been enabled at compile-time.
131 * \return The number of resources reloaded.
133 static int reload_as_needed();
136 * Get whether or not the type of the underlying resource data matches
138 * \return True if the types match, false otherwise.
143 return *typeinfo_
== typeid(T
);
147 * Get a pointer to the underlying resource data as long as the type of
148 * the resource data matches the expected type.
149 * \return The resource data, or null if there is a type mismatch.
154 if (check
<T
>()) return (T
*)resource_
;
159 * Deconstruct a resource container.
166 explicit resource(T
* ptr
) :
168 typeinfo_(const_cast<std::type_info
*>(&typeid(T
))),
169 unloader_(new specific_unloader
<T
>(ptr
)),
173 load_with_path(const std::string
& path
, const std::string
& extension
);
179 loader(const std::string
& prefix
) :
184 virtual resource
* load(const std::string
& path
)
189 const std::string
& prefix() const
199 typedef boost::shared_ptr
<loader
> loader_ptr
;
202 class specific_loader
: public loader
206 specific_loader(const std::string
& prefix
) :
209 virtual resource
* load(const std::string
& path
)
211 return new resource(new T(path
));
219 virtual ~unloader() {};
222 typedef boost::shared_ptr
<unloader
> unloader_ptr
;
225 class specific_unloader
: public unloader
229 specific_unloader(T
* object
= 0) :
232 virtual ~specific_unloader()
248 static bool call_registry(const std::string
& extension
,
249 loader_ptr
& loader
, registry_action action
);
252 std::type_info
* typeinfo_
;
253 unloader_ptr unloader_
;
261 * The resource handle class provides a nicer way to work with resources.
262 * It allows you to work with a resource pointer as if you already know the
263 * type of the resource.
266 class resource_handle
271 * Construct a null resource handle.
276 * Construct a resource handle.
277 * \param ptr The resource pointer to reference.
279 resource_handle(resource_ptr ptr
) :
282 explicit resource_handle(const std::string
& name
) :
283 resource_(resource::load(name
)) {}
285 resource_handle(const std::string
& name
, const std::string
& ext
) :
286 resource_(resource::load(name
, ext
)) {}
289 * Get whether or not the handle is dereferenceable to the type of this
290 * handle. A resource handle is dereferenceable if it is not a null
291 * handle and if its underlying resource is in fact the same type as is
292 * expected by the handle.
293 * \return True if the handle is dereferenceable, false otherwise.
295 operator bool () const
297 if (!resource_
) return false;
298 return resource_
->check
<T
>();
302 * Get a pointer to the underlying resource.
303 * \return The pointer, or null if this handle is not dereferenceable.
307 if (!*this) return 0;
308 return resource_
->get
<T
>();
312 * Dereference the handle all the way to the underlying resource.
313 * \return A reference to the resource.
314 * \throws std::runtime_error If this is a null handle.
316 T
& get_reference() const
318 if (!*this) throw std::runtime_error("dereference null handle");
319 return *(resource_
->get
<T
>());
323 * Same as get() for getting a pointer to the underlying resources.
324 * \return The pointer, or null if this handle is not dereferenceable.
326 T
* operator -> () const
332 * Same a get_reference() for dereferencing the handle.
333 * \return A reference to the resource.
334 * \throws std::runtime_error If this is a null handle.
336 T
& operator * () const
338 return get_reference();
342 * Unload the resource associated with this handle.
346 resource_
= resource_ptr();
351 resource_ptr resource_
;
355 * This macro easily registers types to act as resources. It should be
356 * used in a module file in global scope.
357 * \param TYPE The type (class), qualified as needed for the scope.
358 * \param EXT The file extension the resource uses.
359 * \param PREFIX The path prefix where a resource of this type could be.
361 #define MOOF_REGISTER_RESOURCE(TYPE, EXT, PREFIX) \
364 EXT() { moof::resource::register_type<TYPE>(#EXT, #PREFIX); } \
365 ~EXT() { moof::resource::unregister_type(#EXT); } \
373 #endif // _MOOF_RESOURCE_HH_
This page took 0.044243 seconds and 4 git commands to generate.