]> Dogcows Code - chaz/yoink/blobdiff - src/moof/resource.hh
remove some deprected stuff
[chaz/yoink] / src / moof / resource.hh
index 7616d69c280ecde861e9857c1a1138bd3127a4d8..9d991053c21b841be3ebc164e6b538244c80bb82 100644 (file)
@@ -17,7 +17,8 @@
  * Interface for textures, sounds, and other types of resources.
  */
 
-#include <cstdio>
+#include "config.h"
+
 #include <map>
 #include <stdexcept>
 #include <string>
 
 #include <moof/debug.hh>
 
-#if HAVE_CONFIG_H
-#include "../config.h"
-#endif
-
 
 namespace moof {
 
@@ -48,7 +45,7 @@ class resource
 {
 public:
 
-       // FIXME: this won't be necessary once the existing code is modified to
+       // XXX: this won't be necessary once the existing code is modified to
        // use the resource handles
        resource() {}
 
@@ -58,12 +55,6 @@ public:
         */
        static void add_search_paths(const std::string& paths);
 
-       /**
-        * Add directories to search when looking for resource files.
-        * \param pathList The list of directory paths.
-        */
-       static void add_search_paths(const std::vector<std::string>& pathList);
-
 
        /**
         * Get the path to a resource of a given name.
@@ -71,19 +62,10 @@ public:
         * return, this is changed to an absolute path to the resource.
         * \return True if a path to a resource was found, false otherwise.
         */
-       static bool find(const std::string& file);
-
-       /**
-        * Get the path to a resource of a given name and open it if a resource
-        * was found.
-        * \param path The name of the resource to find.  Upon successful
-        * return, this is changed to an absolute path to the resource.
-        * \param mode The open mode.
-        * \return The FILE* if the resource was found, 0 otherwise.
-        */
-       static FILE* open_file(const std::string& path,
-                                                  const std::string& mode = "rb");
+       static std::string find_file(const std::string& name);
 
+       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
@@ -92,11 +74,11 @@ public:
         * \param extension The file extension.
         */
        template <class T>
-       static void register_type(const std::string& extension)
+       static void register_type(const std::string& extension,
+                                                         const std::string& prefix = "")
        {
-               if (!type_lookup_) type_lookup_ = type_lookup_ptr(new type_lookup);
-               loader_ptr loader(new specific_loader<T>);
-               (*type_lookup_)[extension] = loader;
+               loader_ptr loader(new specific_loader<T>(prefix));
+               call_registry(extension, loader, set);
        }
 
        /**
@@ -107,11 +89,14 @@ public:
         */
        static void unregister_type(const std::string& extension)
        {
-               type_lookup_->erase(extension);
+               loader_ptr loader;
+               call_registry(extension, loader, set);
        }
 
 
-       static resource_ptr load(const std::string& path);
+       static resource_ptr load(const std::string& name);
+       static resource_ptr load(const std::string& name,
+                                                        const std::string& ext);
 
        static resource_ptr reload(std::string& path);
 
@@ -173,16 +158,32 @@ public:
 
 private:
 
+       static resource_ptr load_with_path(const std::string& path);
+
        class loader
        {
        public:
 
+               //loader() {}
+               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;
@@ -192,8 +193,13 @@ 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));
                }
        };
@@ -218,7 +224,7 @@ private:
 
                virtual ~specific_unloader()
                {
-                       log_warning("unloading resource of type ", typeid(T).name());
+                       log_info("unloading resource of type ", typeid(T).name());
                        delete object_;
                }
 
@@ -244,8 +250,19 @@ private:
        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_;
+       //typedef boost::shared_ptr<type_lookup> type_lookup_ptr;
+       //static type_lookup_ptr type_lookup_;
+       //static type_lookup type_lookup_;
+       
+       enum registry_action
+       {
+               lookup,
+               set
+       };
+
+       static bool call_registry(const std::string& extension,
+                                                         loader_ptr& loader,
+                                                         registry_action action);
 
 #ifdef USE_HOTLOADING
        int wd_;
@@ -280,6 +297,13 @@ public:
        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
@@ -337,6 +361,15 @@ public:
        }
 
 
+       /**
+        * Unload the resource associated with this handle.
+        */
+       void unload()
+       {
+               resource_ = resource_ptr();
+       }
+
+
 private:
 
        resource_ptr resource_;
This page took 0.027422 seconds and 4 git commands to generate.