]>
Dogcows Code - chaz/yoink/blob - src/moof/resource.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #ifndef _MOOF_RESOURCE_HH_
13 #define _MOOF_RESOURCE_HH_
17 * Interface for textures, sounds, and other types of resources.
25 #include <boost/shared_ptr.hpp>
26 #include <boost/function.hpp>
28 #include <moof/debug.hh>
35 typedef boost::shared_ptr
<resource
> resource_ptr
;
39 * Generic resource class capable of containing any type of resource,
40 * providing a type-safe interface.
47 * Add a directory to search when looking for resource files.
48 * \param paths A colon-separated list of directory paths.
50 static void set_search_paths(const std::string
& paths
);
54 * Get the path to a resource of a given name. This uses the search
55 * path(s) and resources prefixes to locate resource files.
56 * \param name The name or partial path of the resource to find.
57 * \return The full path of the resource.
59 static std::string
find_file(const std::string
& name
);
62 * Get the path to a resource of a given name and explicit type. This
63 * uses the search path(s) and resources prefixes to locate resource
65 * \param name The name or partial path of the resource to find.
66 * \param ext The extension is appended to the name if the same
67 * extension is not already a part of name.
68 * \return The full path of the resource.
70 static std::string
find_file(const std::string
& name
,
71 const std::string
& ext
);
74 * Register a type with the extension of files which this type can
75 * load. If any type has already been registered with the given file
76 * extension, it will be replaced.
77 * \param extension The file extension.
80 static void register_type(const std::string
& extension
,
81 const std::string
& prefix
= "")
83 loader_ptr
loader(new specific_loader
<T
>(prefix
));
84 call_registry(extension
, loader
, set
);
88 * Unregister the type associated with a file extension. Resources of
89 * this type will no longer be loadable, although resources which are
90 * already loaded will remain loaded.
91 * \param extension The file extension.
93 static void unregister_type(const std::string
& extension
)
96 call_registry(extension
, loader
, set
);
101 * Find and load a resource by name or path.
102 * \param name The name or partial path of the resource. This should
103 * include the extension so that the correct loader can be chosen.
104 * \return The resource.
106 static resource_ptr
load(const std::string
& name
);
109 * Find and load a resource by name or path.
110 * \param name The name or partial path of the resource. This should
111 * include the extension so that the correct loader can be chosen.
113 * \return The resource.
115 static resource_ptr
load(const std::string
& name
,
116 const std::string
& ext
);
120 * Reload the resource data. This will cause the resource file to be
121 * reread, and the underlying resource data will change.
126 * Get the path of file from which this resource was loaded.
129 std::string
path() const
135 * Reloads some resources which have been modified on disk since they
136 * were loaded. Hotloading must have been enabled at compile-time.
137 * \return The number of resources reloaded.
139 static int reload_as_needed();
143 * Get whether or not the type of the underlying resource data matches
145 * \return True if the types match, false otherwise.
150 return *typeinfo_
== typeid(T
);
154 * Get a pointer to the underlying resource data as long as the type of
155 * the resource data matches the expected type.
156 * \return The resource data, or null if there is a type mismatch.
161 if (check
<T
>()) return (T
*)resource_
;
167 * Deconstruct a resource container.
175 explicit resource(T
* ptr
) :
177 typeinfo_(const_cast<std::type_info
*>(&typeid(T
))),
178 unloader_(new specific_unloader
<T
>(ptr
)),
181 static resource_ptr
load_with_path(const std::string
& path
,
182 const std::string
& extension
);
189 loader(const std::string
& prefix
) :
194 virtual resource
* load(const std::string
& path
)
199 const std::string
& prefix() const
210 typedef boost::shared_ptr
<loader
> loader_ptr
;
213 class specific_loader
: public loader
217 specific_loader(const std::string
& prefix
) :
220 virtual resource
* load(const std::string
& path
)
222 return new resource(new T(path
));
231 virtual ~unloader() {};
234 typedef boost::shared_ptr
<unloader
> unloader_ptr
;
237 class specific_unloader
: public unloader
241 specific_unloader(T
* object
= 0) :
244 virtual ~specific_unloader()
262 static bool call_registry(const std::string
& extension
,
264 registry_action action
);
268 std::type_info
* typeinfo_
;
269 unloader_ptr unloader_
;
277 * The resource handle class provides a nicer way to work with resources.
278 * It allows you to work with a resource pointer as if you already know the
279 * type of the resource.
282 class resource_handle
287 * Construct a null resource handle.
292 * Construct a resource handle.
293 * \param ptr The resource pointer to reference.
295 resource_handle(resource_ptr ptr
) :
298 explicit resource_handle(const std::string
& name
) :
299 resource_(resource::load(name
)) {}
301 resource_handle(const std::string
& name
, const std::string
& ext
) :
302 resource_(resource::load(name
, ext
)) {}
306 * Get whether or not the handle is dereferenceable to the type of this
307 * handle. A resource handle is dereferenceable if it is not a null
308 * handle and if its underlying resource is in fact the same type as is
309 * expected by the handle.
310 * \return True if the handle is dereferenceable, false otherwise.
312 operator bool () const
314 if (!resource_
) return false;
315 return resource_
->check
<T
>();
320 * Get a pointer to the underlying resource.
321 * \return The pointer, or null if this handle is not dereferenceable.
325 if (!*this) return 0;
326 return resource_
->get
<T
>();
330 * Dereference the handle all the way to the underlying resource.
331 * \return A reference to the resource.
332 * \throws std::runtime_error If this is a null handle.
334 T
& get_reference() const
336 if (!*this) throw std::runtime_error("dereference null handle");
337 return *(resource_
->get
<T
>());
342 * Same as get() for getting a pointer to the underlying resources.
343 * \return The pointer, or null if this handle is not dereferenceable.
345 T
* operator -> () const
351 * Same a get_reference() for dereferencing the handle.
352 * \return A reference to the resource.
353 * \throws std::runtime_error If this is a null handle.
355 T
& operator * () const
357 return get_reference();
362 * Unload the resource associated with this handle.
366 resource_
= resource_ptr();
372 resource_ptr resource_
;
377 * This macro easily registers types to act as resources. It should be
378 * used in a module file in global scope.
379 * \param TYPE The type (class), qualified as needed for the scope.
380 * \param EXT The file extension the resource uses.
381 * \param PREFIX The path prefix where a resource of this type could be.
383 #define MOOF_REGISTER_RESOURCE(TYPE, EXT, PREFIX) \
386 EXT() { moof::resource::register_type<TYPE>(#EXT, #PREFIX); } \
387 ~EXT() { moof::resource::unregister_type(#EXT); } \
395 #endif // _MOOF_RESOURCE_HH_
This page took 0.054034 seconds and 4 git commands to generate.