]>
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_
18 #include <boost/shared_ptr.hpp>
19 #include <boost/function.hpp>
24 * Interface for textures, sounds, and other types of resources.
31 typedef boost::shared_ptr
<resource
> resource_ptr
;
34 * Generic resource class capable of containing any type of resource,
35 * providing a type-safe interface.
42 * Add a directory to search when looking for resource files.
43 * \param paths A colon-separated list of directory paths.
45 static void set_search_paths(const std::string
& paths
);
48 * Get the path to a resource of a given name. This uses the search
49 * path(s) and resources prefixes to locate resource files.
50 * \param name The name or partial path of the resource to find.
51 * \return The full path of the resource.
53 static std::string
find_file(const std::string
& name
);
56 * Get the path to a resource of a given name and explicit type. This
57 * uses the search path(s) and resources prefixes to locate resource
59 * \param name The name or partial path of the resource to find.
60 * \param ext The extension is appended to the name if the same
61 * extension is not already a part of name.
62 * \return The full path of the resource.
65 find_file(const std::string
& name
, const std::string
& ext
);
68 * Register a type with the extension of files which this type can
69 * load. If any type has already been registered with the given file
70 * extension, it will be replaced.
71 * \param extension The file extension.
74 static void register_type(const std::string
& extension
,
75 const std::string
& prefix
= "")
77 loader_ptr
loader(new specific_loader
<T
>(prefix
));
78 call_registry(extension
, loader
, set
);
82 * Unregister the type associated with a file extension. Resources of
83 * this type will no longer be loadable, although resources which are
84 * already loaded will remain loaded.
85 * \param extension The file extension.
87 static void unregister_type(const std::string
& extension
)
90 call_registry(extension
, loader
, set
);
94 * Find and load a resource by name or path.
95 * \param name The name or partial path of the resource. This should
96 * include the extension so that the correct loader can be chosen.
97 * \return The resource.
99 static resource_ptr
load(const std::string
& name
);
102 * Find and load a resource by name or path.
103 * \param name The name or partial path of the resource. This should
104 * include the extension so that the correct loader can be chosen.
106 * \return The resource.
109 load(const std::string
& name
, const std::string
& ext
);
112 * Reload the resource data. This will cause the resource file to be
113 * reread, and the underlying resource data will change.
118 * Get the path of file from which this resource was loaded.
121 std::string
path() const
127 * Reloads some resources which have been modified on disk since they
128 * were loaded. Hotloading must have been enabled at compile-time.
129 * \return The number of resources reloaded.
131 static int reload_as_needed();
134 * Get whether or not the type of the underlying resource data matches
136 * \return True if the types match, false otherwise.
141 return *typeinfo_
== typeid(T
);
145 * Get a pointer to the underlying resource data as long as the type of
146 * the resource data matches the expected type.
147 * \return The resource data, or null if there is a type mismatch.
152 if (check
<T
>()) return (T
*)resource_
;
157 * Deconstruct a resource container.
164 explicit resource(T
* ptr
) :
166 typeinfo_(const_cast<std::type_info
*>(&typeid(T
))),
167 unloader_(new specific_unloader
<T
>(ptr
)),
171 load_with_path(const std::string
& path
, const std::string
& extension
);
177 loader(const std::string
& prefix
) :
182 virtual resource
* load(const std::string
& path
)
187 const std::string
& prefix() const
197 typedef boost::shared_ptr
<loader
> loader_ptr
;
200 class specific_loader
: public loader
204 specific_loader(const std::string
& prefix
) :
207 virtual resource
* load(const std::string
& path
)
209 return new resource(new T(path
));
217 virtual ~unloader() {};
220 typedef boost::shared_ptr
<unloader
> unloader_ptr
;
223 class specific_unloader
: public unloader
227 specific_unloader(T
* object
= 0) :
230 virtual ~specific_unloader()
246 static bool call_registry(const std::string
& extension
,
247 loader_ptr
& loader
, registry_action action
);
250 std::type_info
* typeinfo_
;
251 unloader_ptr unloader_
;
259 * The resource handle class provides a nicer way to work with resources.
260 * It allows you to work with a resource pointer as if you already know the
261 * type of the resource.
264 class resource_handle
269 * Construct a null resource handle.
274 * Construct a resource handle.
275 * \param ptr The resource pointer to reference.
277 resource_handle(resource_ptr ptr
) :
280 explicit resource_handle(const std::string
& name
) :
281 resource_(resource::load(name
)) {}
283 resource_handle(const std::string
& name
, const std::string
& ext
) :
284 resource_(resource::load(name
, ext
)) {}
287 * Get whether or not the handle is dereferenceable to the type of this
288 * handle. A resource handle is dereferenceable if it is not a null
289 * handle and if its underlying resource is in fact the same type as is
290 * expected by the handle.
291 * \return True if the handle is dereferenceable, false otherwise.
293 operator bool () const
295 if (!resource_
) return false;
296 return resource_
->check
<T
>();
300 * Get a pointer to the underlying resource.
301 * \return The pointer, or null if this handle is not dereferenceable.
305 if (!*this) return 0;
306 return resource_
->get
<T
>();
310 * Dereference the handle all the way to the underlying resource.
311 * \return A reference to the resource.
312 * \throws std::runtime_error If this is a null handle.
314 T
& get_reference() const
316 if (!*this) throw std::runtime_error("dereference null handle");
317 return *(resource_
->get
<T
>());
321 * Same as get() for getting a pointer to the underlying resources.
322 * \return The pointer, or null if this handle is not dereferenceable.
324 T
* operator -> () const
330 * Same a get_reference() for dereferencing the handle.
331 * \return A reference to the resource.
332 * \throws std::runtime_error If this is a null handle.
334 T
& operator * () const
336 return get_reference();
340 * Unload the resource associated with this handle.
344 resource_
= resource_ptr();
349 resource_ptr resource_
;
353 * This macro easily registers types to act as resources. It should be
354 * used in a module file in global scope.
355 * \param TYPE The type (class), qualified as needed for the scope.
356 * \param EXT The file extension the resource uses.
357 * \param PREFIX The path prefix where a resource of this type could be.
359 #define MOOF_REGISTER_RESOURCE(TYPE, EXT, PREFIX) \
362 EXT() { moof::resource::register_type<TYPE>(#EXT, #PREFIX); } \
363 ~EXT() { moof::resource::unregister_type(#EXT); } \
371 #endif // _MOOF_RESOURCE_HH_
This page took 0.055657 seconds and 5 git commands to generate.