+ template <class T>
+ explicit resource(T* ptr) :
+ resource_(ptr),
+ typeinfo_(const_cast<std::type_info*>(&typeid(T))),
+ unloader_(new specific_unloader<T>(ptr)),
+ wd_(-1) {}
+
+ static resource_ptr
+ load_with_path(const std::string& path, const std::string& extension);
+
+ class loader
+ {
+ public:
+
+ loader(const std::string& prefix) :
+ prefix_(prefix) {}
+
+ virtual ~loader() {}
+
+ virtual resource* load(const std::string& path)
+ {
+ return 0;
+ }
+
+ const std::string& prefix() const
+ {
+ return prefix_;
+ }
+
+ private:
+
+ std::string prefix_;
+ };
+
+ typedef boost::shared_ptr<loader> loader_ptr;
+
+ template <class T>
+ class specific_loader : public loader
+ {
+ public:
+
+ specific_loader(const std::string& prefix) :
+ loader(prefix) {}
+
+ virtual resource* load(const std::string& path)
+ {
+ return new resource(new T(path));
+ }
+ };
+
+ class unloader
+ {
+ public:
+
+ virtual ~unloader() {};
+ };
+
+ typedef boost::shared_ptr<unloader> unloader_ptr;
+
+ template <class T>
+ class specific_unloader : public unloader
+ {
+ public:
+
+ specific_unloader(T* object = 0) :
+ object_(object) {}
+
+ virtual ~specific_unloader()
+ {
+ delete object_;
+ }
+
+ private:
+
+ T* object_;
+ };
+
+ enum registry_action
+ {
+ lookup,
+ set
+ };
+
+ static bool call_registry(const std::string& extension,
+ loader_ptr& loader, registry_action action);
+
+ void* resource_;
+ std::type_info* typeinfo_;
+ unloader_ptr unloader_;
+ int wd_;
+ std::string path_;
+ std::string type_;
+};
+
+
+/**
+ * The resource handle class provides a nicer way to work with resources.
+ * It allows you to work with a resource pointer as if you already know the
+ * type of the resource.
+ */
+template <class T>
+class resource_handle
+{
+public:
+
+ /**
+ * Construct a null resource handle.
+ */
+ resource_handle() {}
+
+ /**
+ * Construct a resource handle.
+ * \param ptr The resource pointer to reference.
+ */
+ resource_handle(resource_ptr ptr) :
+ resource_(ptr) {}
+
+ explicit resource_handle(const std::string& name) :
+ resource_(resource::load(name)) {}
+
+ 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
+ * handle and if its underlying resource is in fact the same type as is
+ * expected by the handle.
+ * \return True if the handle is dereferenceable, false otherwise.
+ */
+ operator bool () const
+ {
+ if (!resource_) return false;
+ return resource_->check<T>();
+ }
+
+ /**
+ * Get a pointer to the underlying resource.
+ * \return The pointer, or null if this handle is not dereferenceable.
+ */
+ T* get() const
+ {
+ if (!*this) return 0;
+ return resource_->get<T>();
+ }
+
+ /**
+ * Dereference the handle all the way to the underlying resource.
+ * \return A reference to the resource.
+ * \throws std::runtime_error If this is a null handle.
+ */
+ T& get_reference() const
+ {
+ if (!*this) throw std::runtime_error("dereference null handle");
+ 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.
+ */
+ T* operator -> () const
+ {
+ return get();
+ }
+
+ /**
+ * Same a get_reference() for dereferencing the handle.
+ * \return A reference to the resource.
+ * \throws std::runtime_error If this is a null handle.
+ */
+ T& operator * () const
+ {
+ return get_reference();
+ }
+
+ /**
+ * Unload the resource associated with this handle.
+ */
+ void unload()
+ {
+ resource_ = resource_ptr();
+ }
+
+private:
+
+ resource_ptr resource_;