Thread: Templated Generic Resource Manager, WIP..

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

    Templated Generic Resource Manager, WIP..

    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...
    Last edited by Shamino; 02-07-2006 at 03:00 PM.
    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
    instead of your resource handle you could use a boost.shared_ptr in the map and use weak_ptr's to 'observe' the resource. that way all your clients will be aware of a deleted pointer, because it will throw a boost::bad_weak_ptr when they try to use it.

    http://www.boost.org/libs/smart_ptr/weak_ptr.htm

    It will also ease your memory management in the Resource_Manager class (you won't have to manually delete your resources).

    I know I tend to suggest either boost or the standard library in nearly every post on this board, but there's a reason for that... they pay me to do it! just kidding

    One of the biggest problem with C++ is the tendency of it's users to reinvent the wheel (ala your resource_handle class, which is basically an auto_ptr). Boost and the standard library don't solve this (C++ still has no standard GUI, XML or networking libraries), but they go a long way.
    "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
    968
    Actually chaos.

    EVERYWHERE I go, EVERYONE I ask just about, suggests boost to me...

    But I don't want to use it blindly, I'd like to FULLY understand it before even beginning to implement...

    I guess a weak_ptr + smart_ptr combo actually IS the only way to go for this one..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    As long as you realise, you don't need to understand how it works, just how to use it correctly.
    "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?

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Question, Would I still wrap the weak pointer in a proxy object like handle??

    I am to understand that the clients will use the weak ptr, and the manager will own the smart ptr, is this correct?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    OMG ARE YOU KIDDING ME?!

    How in the world do you install boost? Why isn't there a freakin single executable?

    Why are the help docs written in a foreign language?! (not really, but its all SO arbitrary and incoherant!!)

    How in the world do I do this?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Many of the boost libraries can be used without "installing" boost. Just download it and add the proper directory to the include path for your project or IDE or makefile or whatever (e.g. C:\Boost\include\boost-1_33).

    I have never actually built boost, but I've used the shared_ptrs and other goodies without problem.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Oh I see, I can just link to the lib files and include the headers in my program?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. Although I'm not even sure you need to link to the lib for some things. A few of them are just headers that you can include and use.

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Shamino
    OMG ARE YOU KIDDING ME?!

    How in the world do you install boost? Why isn't there a freakin single executable?

    Why are the help docs written in a foreign language?! (not really, but its all SO arbitrary and incoherant!!)

    How in the world do I do this?
    It's daunting at first, but it's really not that hard.

    download the boost source and extract to a folder (let's say d:\boost for example)

    download bjam and extract to the same folder.
    copy bjam.exe to d:\boost\boost_1_33_1

    if you have visual studio, open the visual studio command prompt (same as normal dos window, but has the include dirs and tools defined in the path)

    type at cmd prompt
    d:\>cd d:\boost\boost_1_33_1
    d:\boost\boost_1_33_1>bjam "-sTools=vc-7_1" install

    if you don't have vs.net 2003 change the 'vc-7_1' to whatever compiler you use

    this will install boost to c:\boost
    then just include the files you need.

    the thing with the boost docs is that they're written from the point of view of being eventually included in the C++ standard so (some of them) tend to be a bit verbose and language lawyer-y. But most have an excellent tutorial section which should get you on your way.
    "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?

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Thanks Chaos, installer is running now, that was alot easier than expected..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    What do I need to include to use shared pointers and smart pointers and things like that?

    When I include the headers, All the headers do is include more headers, which can't be found, shrug..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then your include directories are not set up correctly. In the Visual Studio options, you need to go to C/C++->Directories and edit the include path to include the base of your Boost installation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Actually I kinda broke the whole build process, I typed in the wrong toolset .

    We'll try again when its done..
    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, 10:27 PM
  4. Generic Resource Manager, 90% done! Boost enabled!
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2006, 07:37 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