Filter:   InfoImg
download cache.h
Language: C++
LOC: 59
Project Info
Ultima Online Project(uop)
Server: SourceForge
Type: cvs
SourceForge\u\uop\uop\uop\
   acinclude.m4
   aclocal.m4
   action.cc
   action.h
   animation.cc
   animation.h
   artexport.cc
   artfactory.cc
   artfactory.h
   audio.cc
   audio.h
   btree.cc
   btree.h
   cache.h
   cliloc.cc
   cliloc.h
   config.h.in
   configure.in
   cvs2cl.pl
   entity.cc
   entity.h
   gumps.cc
   gumps.h
   interface.cc
   interface.h
   interfacegumps.cc
   interfacegumps.h
   interfacehandler.cc
   interfacehandler.h
   intrface.def
   Makefile.am
   Makefile.in
   message.cc
   message.h
   musicmap.def
   network.cc
   network.h
   patchhandler.cc
   patchhandler.h
   readable.pl
   serverhandler.cc
   serverhandler.h
   sprite.cc
   sprite.h
   stamp-h.in
   texthandler.cc
   texthandler.h
   tiledata.cc
   tiledata.h
   unreadable.pl
   uop.cc
   uop.cfg
   uop.dox
   uop.h
   uopconfig.cc
   uopconfig.h
   uosprite.h
   uostub.pl
   world.cc
   world.h

#ifndef __UOPCACHE_H__
#define __UOPCACHE_H__

#include <map>
#include <list>

template <class kT, class vT, class factoryT, class cacheT=map<kT,vT> >
class Cache {

public:
	Cache(factoryT &f, int i=256) : factory(f) {max=i;}
	vT &operator[](const kT &k)
	{
		typename cacheT::iterator iter=cache.find(k);
		if (iter!=cache.end())
			return iter->second;
		vT &v=cache[k];
		factory.produce(k,v);
		queue.push_front(k);
		if (queue.size()>=max)
		{
			kT victim=queue.back();
			factory.recycle(cache[victim]);
			cache.erase(victim);
			queue.pop_back();
		}
		return v;
	}
private:
	unsigned int max;
	cacheT cache;
	list<kT> queue;
	factoryT &factory;
};

template <class kT, class vT, class factoryT, bool tdmap, class cacheT=map<kT,vT> >
class MapCache {

public:
	MapCache(int i=256) : max(i) {}
	vT &operator[](const kT &k)
	{
		typename cacheT::iterator iter=cache.find(k);
		if (iter!=cache.end())
			return iter->second;
		vT &v=cache[k];
		factory.produce(k,tdmap,v);
		queue.push_front(k);
		if (queue.size()>=max)
		{
			kT victim=queue.back();
			factory.recycle(cache[victim]);
			cache.erase(victim);
			queue.pop_back();
		}
		return v;
	}
private:
	unsigned int max;
	cacheT cache;
	list<kT> queue;
	factoryT factory;
};

#endif