This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Simple Resource Manager
  Submitted by

Here's a template for a simple resource manager. To use it, subclass and provide a Load function, then just call GetResource(filename) when you want a resource - it'll load it if it isn't in memory already, and return a reference to it. The resources are deleted when the resource manager's deconstructed.

Do what you want with the code so long as I don't get the blame! There's no doubt a few stylistic complaints to be made (the using namespace is an obvious one if you put this in a header), I'm sure they'll get picked out in the comments...


Resource Manager template

Inherited from for each resource type

Caches the resource in question and frees it when the manager is deleted (generally on program shutdown)


#include <map #include <string

#include <assert.h

using namespace std;

template <class R class resourcemanager { protected: map <string, R * resources;

public: R &GetResource (const string &filename) { if (resources.find(filename) == resources.end()) //need to load resource { R* a = Load (filename); assert(a != NULL); //alternatively, handle it here resources[filename] = a; } return *(resources[filename]); }

virtual ~resourcemanager() { //iterate along map for (map<string, R*::iterator i = resources.begin(); i != resources.end(); i++) delete i-second;

resources.clear(); }

protected: virtual R *Load(const string &filename)=0; };

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.