]> Dogcows Code - chaz/yoink/blobdiff - src/moof/resource.hh
fixed some resource management bugs
[chaz/yoink] / src / moof / resource.hh
index 5f1a71ab41e366596ef890f26806fd9f4d097173..eba4b3f2871d59e55a81b661e98776a947b97183 100644 (file)
@@ -46,7 +46,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() {}
 
@@ -62,11 +62,15 @@ public:
         * \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.
+        * XXX this is legacy
         */
        static bool find(const std::string& file);
 
        static std::string find_file(const std::string& name);
 
+       static std::string find_file(const std::string& name,
+                                                                const std::string& ext);
+
        /**
         * Get the path to a resource of a given name and open it if a resource
         * was found.
@@ -74,6 +78,7 @@ public:
         * 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.
+        * XXX deprecated
         */
        static FILE* open_file(const std::string& path,
                                                   const std::string& mode = "rb");
@@ -86,13 +91,12 @@ 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;
-               //type_lookup_[extension] = loader;
-               manage_loader(extension, loader, true);
+               loader_ptr loader(new specific_loader<T>(prefix));
+               printf("registered type with prefix %s", loader->prefix().c_str());
+               call_registry(extension, loader, set);
        }
 
        /**
@@ -103,14 +107,14 @@ public:
         */
        static void unregister_type(const std::string& extension)
        {
-               //type_lookup_.erase(extension);
-               //type_lookup_->erase(extension);
                loader_ptr loader;
-               manage_loader(extension, loader, true);
+               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);
 
@@ -169,21 +173,35 @@ public:
         */
        static int reload_as_needed();
 
-       static void print_types();
-
 
 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;
@@ -193,6 +211,10 @@ 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());
@@ -249,8 +271,16 @@ private:
        //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 void manage_loader(const std::string& extension, loader_ptr& loader, bool set = false);
+       static bool call_registry(const std::string& extension,
+                                                         loader_ptr& loader,
+                                                         registry_action action);
 
 #ifdef USE_HOTLOADING
        int wd_;
@@ -285,8 +315,12 @@ public:
        resource_handle(resource_ptr ptr) :
                resource_(ptr) {}
 
-       explicit resource_handle(const std::string& path) :
-               resource_(resource::load(path)) {}
+       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)) {}
+
 
 
        /**
@@ -345,6 +379,15 @@ public:
        }
 
 
+       /**
+        * Unload the resource associated with this handle.
+        */
+       void unload()
+       {
+               resource_ = resource_ptr();
+       }
+
+
 private:
 
        resource_ptr resource_;
This page took 0.024582 seconds and 4 git commands to generate.