]> Dogcows Code - chaz/yoink/blobdiff - src/moof/resource.hh
mesh and other random adjustments
[chaz/yoink] / src / moof / resource.hh
index 9d991053c21b841be3ebc164e6b538244c80bb82..094791c30f383b34538f6713227a190f69452ba3 100644 (file)
@@ -17,8 +17,6 @@
  * Interface for textures, sounds, and other types of resources.
  */
 
-#include "config.h"
-
 #include <map>
 #include <stdexcept>
 #include <string>
@@ -45,25 +43,30 @@ class resource
 {
 public:
 
-       // XXX: this won't be necessary once the existing code is modified to
-       // use the resource handles
-       resource() {}
-
        /**
         * Add a directory to search when looking for resource files.
         * \param paths A colon-separated list of directory paths.
         */
-       static void add_search_paths(const std::string& paths);
+       static void set_search_paths(const std::string& paths);
 
 
        /**
-        * Get the path to a resource of a given name.
-        * \param path The name of the resource to find.  Upon successful
-        * return, this is changed to an absolute path to the resource.
-        * \return True if a path to a resource was found, false otherwise.
+        * Get the path to a resource of a given name.  This uses the search
+        * path(s) and resources prefixes to locate resource files.
+        * \param name The name or partial path of the resource to find.
+        * \return The full path of the resource.
         */
        static std::string find_file(const std::string& name);
 
+       /**
+        * Get the path to a resource of a given name and explicit type.  This
+        * uses the search path(s) and resources prefixes to locate resource
+        * files.
+        * \param name The name or partial path of the resource to find.
+        * \param ext The extension is appended to the name if the same
+        * 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);
 
@@ -85,7 +88,7 @@ public:
         * Unregister the type associated with a file extension.  Resources of
         * this type will no longer be loadable, although resources which are
         * already loaded will remain loaded.
-        * \param extension The file extension
+        * \param extension The file extension.
         */
        static void unregister_type(const std::string& extension)
        {
@@ -94,34 +97,46 @@ public:
        }
 
 
+       /**
+        * Find and load a resource by name or path.
+        * \param name The name or partial path of the resource.  This should
+        * include the extension so that the correct loader can be chosen.
+        * \return The resource.
+        */
        static resource_ptr load(const std::string& name);
+
+       /**
+        * Find and load a resource by name or path.
+        * \param name The name or partial path of the resource.  This should
+        * include the extension so that the correct loader can be chosen.
+        * \param 
+        * \return The resource.
+        */
        static resource_ptr load(const std::string& name,
                                                         const std::string& ext);
 
-       static resource_ptr reload(std::string& path);
-
 
        /**
-        * Construct a resource container.
-        * \param ptr A pointer to the underlying resource data.
+        * Reload the resource data.  This will cause the resource file to be
+        * reread, and the underlying resource data will change.
         */
-       template <class T>
-       explicit resource(T* ptr) :
-               resource_(ptr),
-               typeinfo_(const_cast<std::type_info*>(&typeid(T))),
-               unloader_(new specific_unloader<T>(ptr)) {}
+       void reload();
 
        /**
-        * Deconstruct a resource container.
+        * Get the path of file from which this resource was loaded.
+        * \return The path.
         */
-       virtual ~resource();
-
+       std::string path() const
+       {
+               return path_;
+       }
 
        /**
-        * Reload the resource data.  This will cause the resource file to be
-        * reread, and the underlying resource data will change.
+        * Reloads some resources which have been modified on disk since they
+        * were loaded.  Hotloading must have been enabled at compile-time.
+        * \return The number of resources reloaded.
         */
-       void reload();
+       static int reload_as_needed();
 
 
        /**
@@ -149,22 +164,28 @@ public:
 
 
        /**
-        * Reloads some resources which have been modified on disk since they
-        * were loaded.  Hotloading must have been enabled at compile-time.
-        * \return The number of resources reloaded.
+        * Deconstruct a resource container.
         */
-       static int reload_as_needed();
+       virtual ~resource();
 
 
 private:
 
-       static resource_ptr load_with_path(const std::string& path);
+       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() {}
                loader(const std::string& prefix) :
                        prefix_(prefix) {}
 
@@ -193,13 +214,11 @@ private:
        {
        public:
 
-               //specific_loader() {}
                specific_loader(const std::string& prefix) :
                        loader(prefix) {}
 
                virtual resource* load(const std::string& path)
                {
-                       log_info("loading resource of type ", typeid(T).name());
                        return new resource(new T(path));
                }
        };
@@ -224,7 +243,6 @@ private:
 
                virtual ~specific_unloader()
                {
-                       log_info("unloading resource of type ", typeid(T).name());
                        delete object_;
                }
 
@@ -235,25 +253,6 @@ private:
        };
 
 
-       void set_loader(const std::string& path, loader_ptr loader)
-       {
-               path_ = path;
-               loader_ = loader;
-       }
-
-
-       void*                   resource_;
-       std::type_info* typeinfo_;
-       unloader_ptr    unloader_;
-
-       std::string             path_;
-       loader_ptr              loader_;
-
-       typedef std::map<std::string,loader_ptr> type_lookup;
-       //typedef boost::shared_ptr<type_lookup> type_lookup_ptr;
-       //static type_lookup_ptr type_lookup_;
-       //static type_lookup type_lookup_;
-       
        enum registry_action
        {
                lookup,
@@ -264,14 +263,13 @@ private:
                                                          loader_ptr& loader,
                                                          registry_action action);
 
-#ifdef USE_HOTLOADING
-       int wd_;
 
-       void set_watch_descriptor(int wd)
-       {
-               wd_ = wd;
-       }
-#endif
+       void*                   resource_;
+       std::type_info* typeinfo_;
+       unloader_ptr    unloader_;
+       int                             wd_;
+       std::string             path_;
+       std::string             type_;
 };
 
 
@@ -304,7 +302,6 @@ public:
                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
@@ -376,6 +373,23 @@ private:
 };
 
 
+/**
+ * This macro easily registers types to act as resources.  It should be
+ * used in a module file in global scope.
+ * \param TYPE The type (class), qualified as needed for the scope.
+ * \param EXT The file extension the resource uses.
+ * \param PREFIX The path prefix where a resource of this type could be.
+ */
+#define MOOF_REGISTER_RESOURCE(TYPE, EXT, PREFIX) \
+namespace { \
+       struct EXT { \
+               EXT()  { moof::resource::register_type<TYPE>(#EXT, #PREFIX); } \
+               ~EXT() { moof::resource::unregister_type(#EXT); } \
+       }; \
+       static EXT EXT; \
+}
+
+
 } // namespace moof
 
 #endif // _MOOF_RESOURCE_HH_
This page took 0.022929 seconds and 4 git commands to generate.