ResourceManager

License of the project : CC-BY-SA

March 2009 v00 I start the project
April 2009 v01 First release

[Visit my wikisite : hiura.tuxfamily.org]

This tools is a simple class. It can manage all kind of resources. You have only to inherit from this class to make your own specific manager.

Developing

I made an UML scheme before coding anything. Here it is with a little example of inheritance :
uml scheme

Some explanation

There are four template arguments :

Template Role Value in the example
ResourceType Represents the type of the resource. sf::Image
ResourceInfo Represents the type of the data needed to load the resource — it can be the same type as ResourceType if there is no loading to be perform. std::string
ResourceID Represents the type of the data that make the link in the std::map with ResourceType. std::string
ResourcePr Represents the predicate needed by std::map — its default value is std::less<ResourceID>. its default value — std::less<std::string>

I use a std::map to manage easily the link between a resource and its ID.

The two functions 'Load' and 'Destroy' are virtual. If an error occurs in these functions, a std::runtime_error is thrown.

Load function is a pure virtual one ; each implementation of ResourceManager has to define this function. Destroy function can be implemented in another way than the default one to provide some operations before removing the element from the map.

This last function is called for each element of the map when the resource manager is destroyed.

Add function also throw an exception (std::invalid_argument) if the given ID already exists. Remove function throws the same exception if the ID doesn't exist.

If the key passed to the operator[] doesn't exist an exception is thrown. Its type is std::invalid_argument. This function also exists as a const member function.

Example

This sample is available in the download archive.
Here I show you how to implement an ImageManager, and how to use it.

One comment on the below sample : the two pictures and ”../src/ResourceManager.hpp” are available in the archive.

First, the ImageManager

/*
  resourcemanager
  Copyright (C) 2009 Marco Antognini (hiura@romandie.com)
  License : CC-BY-SA 3.0
  	You can find the full legal code at 
  	http://creativecommons.org/licenses/by-sa/3.0/
  	or in the local file cc-by-sa-3.0-legalcode.html .
  	Here is only an abstract :
 
  You are free :
  	to Share — to copy, distribute and transmit the work
	to Remix — to adapt the work
 
  Under the following conditions :
  	Attribution. You must attribute the work in the manner 
		specified by the author or licensor (but not 
		in any way that suggests that they endorse you
		or your use of the work).
	Share Alike. If you alter, transform, or build upon this 
		work, you may distribute the resulting work only 
		under the same, similar or a compatible license.
 
  For any reuse or distribution, you must make clear to others 
  	the license terms of this work. The best way to do this 
	is with a link to this
       	(http://creativecommons.org/licenses/by-sa/3.0/) web page.
 
  Any of the above conditions can be waived if you get 
  	permission from the copyright holder.
 
  Nothing in this license impairs or restricts the author's 
  	moral rights.
 
*/
 
/*!
 \file ImageManager.hpp
 \brief Definition and implementation of ImageManager class.
*/
 
#ifndef IMAGEMANAGER_HPP
#define IMAGEMANAGER_HPP
 
#include "../src/ResourceManager.hpp"
#include <string>
#include <SFML/Graphics.hpp>
 
/*!
 \namespace sftools
 \brief namespace containing tools using SFML library.
*/
namespace sftools {
 
/*!
 \class ImageManager
 \brief Manage Image -- little sample.
*/
class ImageManager
: public ResourceManager<sf::Image, std::string, std::string> {
	public :
		/*!
		 \brief Load a resource before adding it.
		 \details Use ResourceManager::Add in its definition,
		 	and may throw a std::runtime_error if an
			error occurs.
		 \note This function doesn't catch a 
		 	possible std::invalid_argument thrown 
			by ResourceManager::Add.
		 \param info : The data needed to build the ResourceType.
		*/
		const std::string& Load(const std::string& info) {
			// Load in a temporary object the image, and
			// then "send" it to this.
			sf::Image i;
			if (!i.LoadFromFile(info))
				throw std::runtime_error(
					"Unable to load the file " + info);
 
			Add(info, i);
 
			return info;
		}
 
}; // ImageManager
 
} // namespace sftools
 
#endif

As you can see it's very easy. Here we do not need to implement Destroy : the default version does exactly what we need. We are also using the default value of ResourcePr.

Second, the sample

/*
  resourcemanager
  Copyright (C) 2009 Marco Antognini (hiura@romandie.com)
  License : CC-BY-SA 3.0
  	You can find the full legal code at 
  	http://creativecommons.org/licenses/by-sa/3.0/
  	or in the local file cc-by-sa-3.0-legalcode.html .
  	Here is only an abstract :
 
  You are free :
  	to Share — to copy, distribute and transmit the work
	to Remix — to adapt the work
 
  Under the following conditions :
  	Attribution. You must attribute the work in the manner 
		specified by the author or licensor (but not 
		in any way that suggests that they endorse you
		or your use of the work).
	Share Alike. If you alter, transform, or build upon this 
		work, you may distribute the resulting work only 
		under the same, similar or a compatible license.
 
  For any reuse or distribution, you must make clear to others 
  	the license terms of this work. The best way to do this 
	is with a link to this
       	(http://creativecommons.org/licenses/by-sa/3.0/) web page.
 
  Any of the above conditions can be waived if you get 
  	permission from the copyright holder.
 
  Nothing in this license impairs or restricts the author's 
  	moral rights.
 
*/
 
#include "ImageManager.hpp"
#include <algorithm>
 
using namespace sftools;
 
/// Example of ResourceManager usage.
int main(int, char**) {
	const std::string& S1("pict1.png");
	const std::string& S2("pict2.png");
 
	ImageManager im;
 
	im.Load(S1);
	im.Load(S2);
 
	const sf::Image& i1 = im[S1];
	const sf::Image& i2 = im[S2];
 
	sf::Sprite s1(i1);
	sf::Sprite s2(i2, sf::Vector2f(i1.GetWidth(), 0));
 
	const unsigned int width(i1.GetWidth() + i2.GetWidth());
	const unsigned int height(std::max(i1.GetHeight(), i2.GetHeight()));
 
	sf::RenderWindow w(sf::VideoMode(width, height, 32), "ImageManager");
 
	while (w.IsOpened()) {
		sf::Event e;
		while (w.GetEvent(e)) {
			if (e.Type == sf::Event::Closed)
				w.Close();
		}
 
		w.Clear();
		w.Draw(s1);
		w.Draw(s2);
		w.Display();
	}
 
	return 0;
}

Note : we do not need to call Destroy two times like this :

    } // end of while
 
    im.Destroy(S1);
    im.Destroy(S2);
 
    return 0;
}

Documentation

The documentation is there.

Download

Here you can download everything (source, sample, scheme, and doc) : resourcemanager.tar.gz

 
en/sources/resource_manager_hiura.txt · Last modified: 2009/06/18 11:59 by Hiura
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki