I must say, since I started programming a year ago, this hobby of mine has got to be the most thought intensive thing I've ever taken up. I literally perplex about programming day in and day out, it never leaves me. Finally now, I believe I'm onto something, something good. With that being said, I give you my resource manager.
Code:
#include <vector>
#include <map>
template< typename T_ >
class Resource_Handle
{
//template< typename T_ > friend class Manager;
public:
T_* operator-> (void) { return (mRawResource); }
public:
Resource_Handle (T_* raw) : mRawResource(raw) { }
Resource_Handle (const Resource_Handle<T_>& a) { this->mRawResource = a.mRawResource; }
T_* mRawResource;
};
template< typename T_ >
class Resource_Manager
{
public:
Resource_Handle< T_ > RequestResource(const std::string &name)
{
std::map< std::string,Resource_Handle<T_> >::iterator it = mResources.find(name);
if(it == mResources.end())
{
Resource_Handle<T_> handle(new T_);
handle->Load_Resource(name);
mResources.insert(std::make_pair(name, handle));
return handle;
}
else
{
return (it->second);
}
}
private:
std::map< std::string,Resource_Handle<T_> > mResources;
};
Unfortunately, I'm treading into very, very murky waters, I'm coding faster than my brain can comprehend what I am exactly doing. Although I do have a general idea of what I'm trying to accomplish, I'm really not sure if what I'm coding actually makes logical sense.
And I'm going to lay it out in plain order of operations.
1. Another system in the program calls the Request_Resource function when it needs something loaded into memory, it passes a string name, nothing more.
2. The resource manager processes the request by checking a list of already loaded resources, and doing the necessary steps to return a handle to whatever system has requested it.
3. Whatever system is doing the work now, has a whole bunch of resources it needs to do whatever with.
Three simple steps. Wow, I think I've outdone myself.
Problems:
1. I have two copies of the resource handle, I return it to the system as well as push it back into a map...
2. Deletion of the resource manager would result in the dangling of pointers held by clients.
Those are the problems I can see glaring at me at the moment..
My questions are simple, what exactly can I do to solve problem 1 and 2? Also, simply put, does this make sense? My brain is literally burning after a month of studying this thing...