Generic Resource Manager, 90% done! Boost enabled!

This is a discussion on Generic Resource Manager, 90% done! Boost enabled! within the C++ Programming forums, part of the General Programming Boards category; Just posting for constructive nitpicking Code: #include <vector> #include <map> #include <boost\shared_ptr.hpp> #include <boost\weak_ptr.hpp> template< typename T_ > class Resource_Manager ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Generic Resource Manager, 90% done! Boost enabled!

    Just posting for constructive nitpicking

    Code:
    #include <vector>
    #include <map>
    #include <boost\shared_ptr.hpp>
    #include <boost\weak_ptr.hpp>
    
    template< typename T_ >
    class Resource_Manager
    {  
    
    public:
    	Resource_Manager<T_>() {};
    	~Resource_Manager<T_>() {};
    
    	boost::weak_ptr<T_> Request_Resource(const std::string &name)
        {
    		std::map< std::string, boost::shared_ptr<T_> >::iterator  it = mResources.find(name);
    
          if(it == mResources.end())
    	  {
    
    		  boost::shared_ptr<T_> Raw_Resource(new T_);
    		  Raw_Resource->Load_Resource(name);
    		  mResources.insert(std::make_pair(name, Raw_Resource));
    		  boost::weak_ptr<T_> Resource(Raw_Resource);
    		  return Resource;
    	  }
    	  else
    	  {
    		  boost::weak_ptr<T_> Resource(it->second);
    		  return Resource;
    	  }
        }
    
    	void Request_Resource_Removal(const std::string &name)
    	{
    		std::map<std::string, boost::shared_ptr<T_> >::iterator it = mResources.find(name);
    
    		if (it != mResources.end())
    		{
    			// how do I remove an element of the map without breaking anything?
    		}
    	}
    
    private:
    	std::map< std::string, boost::shared_ptr<T_> >  mResources;
    };
    One question, It won't let me do a delete it->second in the remove resource function, it is having an issue with boost::shared_ptr...

    TODO:

    Count references to each shared pointer, release resource when reference count gets to zero.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597

    Thumbs up

    Code:
    #include <vector>
    #include <map>
    #include <boost\shared_ptr.hpp>
    #include <boost\weak_ptr.hpp>
    
    template< typename T_ >
    class Resource_Manager
    {  
    
    public:
    
    	typedef T_ value_type; // std library convention 
    
    	typedef boost::shared_ptr<T_> Resource_Ptr;
    	typedef boost::weak_ptr<T_> Resource_Observer;
    	typedef std::map< std::string, Resource_Ptr > Resource_Map;
    
    	Resource_Manager<T_>() {};
    	~Resource_Manager<T_>() {};
    
    	Resource_Observer Request_Resource(const std::string &name)
    	{
    		Resource_Map::iterator  it = mResources.find(name);
    
    		if (it == mResources.end())
    		{
    			Resource_Ptr Raw_Resource(new T_);
    			Raw_Resource->Load_Resource(name);
    			mResources.insert(std::make_pair(name, Raw_Resource));
    			Resource_Observer Resource(Raw_Resource);
    			return Resource;
    		}
    		else
    		{
    			return Resource_Observer(it->second);
    		}
    	}
    
    	void Request_Resource_Removal(const std::string &name)
    	{
    		Resource_Map::iterator it = mResources.find(name);
    
    		if (it != mResources.end())
    		{
    			mResources.erase(it);
    		}
    	}
    
    private:
    	Resource_Map  mResources;
    };
    Quote Originally Posted by Shamino

    One question, It won't let me do a delete it->second in the remove resource function, it is having an issue with boost::shared_ptr...

    TODO:

    Count references to each shared pointer, release resource when reference count gets to zero.
    I'm not sure you're understanding what a shared_ptr does? you don't need to do reference counting or deleting it->second, shared_ptr takes care of that internally. for the most part, you can happily ignore the fact that it's dynamic memory and shared_ptr will clean up for you. gotta love that!

    not sure about the naming of Request_Resource, either. It's creating a resource if the one you request doesn't exist. I think the interface would be clearer if Request_Resource returned existing resources and informed you of an error if you requested a non-existent resource. I'd also add an Add_Resource method that allows you to add an existing object to the resource manager (what if T_ requires arguements to it's constructor?)

    something like
    Code:
    void Add_Resource(const std::string &name, const Resource_Ptr& resource)
    {
    	mResources.insert(std::make_pair(name, resource));
    }
    
    // then Request_Resource becomes
    Resource_Observer Request_Resource(const std::string &name)
    {
    	Resource_Map::iterator  it = mResources.find(name);
    
    	if (it == mResources.end())
    	{
    		throw Bad_Resource; // or return a null 
    		// up to you how you want to indicate error
    	}
    	else
    	{
    		return Resource_Observer(it->second);
    	}
    }
    To be honest, the more you refine this class the more it becomes a very simple wrapper around map, with a few convenience methods. This is a good thing, as it minimises the amount of code you have to maintain.
    Last edited by ChaosEngine; 02-20-2006 at 03:26 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    About making the interface clearer, you might be right, though.

    I figure the system doing the resource gathering doesn't care if it is there or not, it just wants a freakin resource, the logic engine wants to create a geometry node out of a raw resource and a transformation matrix. If resources aren't getting dynamically allocated by the logic engine, what manages what resources the game has loaded?

    Seeing as how there might be no way to tell what resources I need before I start my game, I think this is the way to go.

    The logic engine doesn't care if the resource is loaded, he wants it, simple as that.

    But yeah, you're probably right, I really don't fully understand what a shared pointer
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting out a resource manager
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 11-10-2008, 07:12 PM
  2. Resource manager
    By beene in forum Game Programming
    Replies: 3
    Last Post: 03-04-2008, 09:50 PM
  3. Resource manager tree
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 09-07-2007, 11:27 PM
  4. Templated Generic Resource Manager, WIP..
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 02-19-2006, 06:29 PM
  5. Generic Resource_Manager WIP with lots TODO
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2006, 01:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21