Design + policies + functors question

M

Matthias

Hello,

I basically want to implement a general resource manager which should have
a caching and a loading policy so that I can exchange those. Here's some
example code of how it should work:

ResourceManager< SimpleCachingPolicy<Image, SimpleImageLoader> > imageMgr;
ResourceManager< SimpleCachingPolicy<Sound, SimpleSoundLoader> > soundMgr;

imageMgr.Load(filename, 1234);
soundMgr.Load(filename, "foo", 5.37);

So we have two managers which load sound and images respectively and both
use the same caching policy. However, notice that the arguments of the
Load function are different.
Now I want to write a SimpleCachingPolicy that can take an arbitrary
object (or pointer to it). I tried like this:

template<typename ResourceType, typename LoadingPolicy>
class SimpleCachingPolicy
{
public:
ResourceType Load(... what to put here, the arguments into this func are
different for each ResourceType ...)
{
if ( IsCached(... same arguments as above ...) ) return obj;
obj=LoadingPolicy::LoadObject(... same args as above ...);
AddToCache(obj);
return obj;
}
};

The problem one can see here is that I would need to write the Load
function several times, one version for each parameter. I thought I could
apply functors here, but I am not sure how I can do this. Can you give me
any hints what I can do to solve this problem? Is there a way round
needing to make several versions of Load(...)?

Thanks a lot for your help!

-Matthias
 
V

Victor Bazarov

Matthias said:
I basically want to implement a general resource manager which should
have a caching and a loading policy so that I can exchange those. Here's
some example code of how it should work:

ResourceManager< SimpleCachingPolicy<Image, SimpleImageLoader> > imageMgr;
ResourceManager< SimpleCachingPolicy<Sound, SimpleSoundLoader> > soundMgr;

imageMgr.Load(filename, 1234);
soundMgr.Load(filename, "foo", 5.37);

So we have two managers which load sound and images respectively and
both use the same caching policy. However, notice that the arguments of
the Load function are different.
Now I want to write a SimpleCachingPolicy that can take an arbitrary
object (or pointer to it). I tried like this:

template<typename ResourceType, typename LoadingPolicy>
class SimpleCachingPolicy
{
public:
ResourceType Load(... what to put here, the arguments into this func
are different for each ResourceType ...)
{
if ( IsCached(... same arguments as above ...) ) return obj;
obj=LoadingPolicy::LoadObject(... same args as above ...);

So, it's apparent that your 'SimpleCachingPolicy' is not that simple,
is it? Essentially, on one hand, you're trying to keep the interface
the same since it's the caller that should define it, and OTOH you're
forced to provide several of those since it ultimately depends on the
type of the template argument...
AddToCache(obj);
return obj;
}
};

The problem one can see here is that I would need to write the Load
function several times, one version for each parameter. I thought I
could apply functors here, but I am not sure how I can do this. Can you
give me any hints what I can do to solve this problem? Is there a way
round needing to make several versions of Load(...)?

I don't think so. You can try to work around the necessity to have
several 'Load' members in the same class template by inheriting it from
different 'Loaders', which you will specialise on the type of Resource,
but it will only move the problem from one class into the other. You
still will have to "make several versions of Load".

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top