-/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+/*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#ifndef _MOOF_RESOURCE_HH_
#define _MOOF_RESOURCE_HH_
class resource;
typedef boost::shared_ptr<resource> resource_ptr;
-
/**
* Generic resource class capable of containing any type of resource,
* providing a type-safe interface.
*/
static void set_search_paths(const std::string& paths);
-
/**
* Get the path to a resource of a given name. This uses the search
* path(s) and resources prefixes to locate resource files.
* extension is not already a part of name.
* \return The full path of the resource.
*/
- static std::string find_file(const std::string& name,
- const std::string& ext);
+ static std::string
+ find_file(const std::string& name, const std::string& ext);
/**
* Register a type with the extension of files which this type can
*/
template <class T>
static void register_type(const std::string& extension,
- const std::string& prefix = "")
+ const std::string& prefix = "")
{
loader_ptr loader(new specific_loader<T>(prefix));
call_registry(extension, loader, set);
call_registry(extension, loader, set);
}
-
/**
* Find and load a resource by name or path.
* \param name The name or partial path of the resource. This should
* \param
* \return The resource.
*/
- static resource_ptr load(const std::string& name,
- const std::string& ext);
-
+ static resource_ptr
+ load(const std::string& name, const std::string& ext);
/**
* Reload the resource data. This will cause the resource file to be
*/
static int reload_as_needed();
-
/**
* Get whether or not the type of the underlying resource data matches
* an expected type.
return 0;
}
-
/**
* Deconstruct a resource container.
*/
virtual ~resource();
-
private:
template <class T>
unloader_(new specific_unloader<T>(ptr)),
wd_(-1) {}
- static resource_ptr load_with_path(const std::string& path,
- const std::string& extension);
-
+ static resource_ptr
+ load_with_path(const std::string& path, const std::string& extension);
class loader
{
return prefix_;
}
-
private:
std::string prefix_;
}
};
-
class unloader
{
public:
delete object_;
}
-
private:
T* object_;
};
-
enum registry_action
{
lookup,
};
static bool call_registry(const std::string& extension,
- loader_ptr& loader,
- registry_action action);
+ loader_ptr& loader, registry_action action);
-
- void* resource_;
+ void* resource_;
std::type_info* typeinfo_;
unloader_ptr unloader_;
- int wd_;
- std::string path_;
- std::string type_;
+ int wd_;
+ std::string path_;
+ std::string type_;
};
resource_handle(const std::string& name, const std::string& ext) :
resource_(resource::load(name, ext)) {}
-
/**
* Get whether or not the handle is dereferenceable to the type of this
* handle. A resource handle is dereferenceable if it is not a null
return resource_->check<T>();
}
-
/**
* Get a pointer to the underlying resource.
* \return The pointer, or null if this handle is not dereferenceable.
return *(resource_->get<T>());
}
-
/**
* Same as get() for getting a pointer to the underlying resources.
* \return The pointer, or null if this handle is not dereferenceable.
return get_reference();
}
-
/**
* Unload the resource associated with this handle.
*/
resource_ = resource_ptr();
}
-
private:
resource_ptr resource_;
};
-
/**
* This macro easily registers types to act as resources. It should be
* used in a module file in global scope.