Avoid the abstract templates

Started by
4 comments, last by Codarki 11 years, 7 months ago
I'd like some feedback from more experienced folks on how they would implement the following (In C++)

I have a Least-Recently-Used Cache which is a template class. The data is type<T> and it is always keyed by unsigned int. It works as far as putting data in from outside, and retrieving it again (with a the proper bump in "used recently" status).

Now, when another service requests data from the cache, it is possible for the data to be missing. What should the cache do in this situation? It is not his responsibility to generate the data, only to store it. So the first option is the cache class can just throw some kind of NULL flag out instead of data and the outside world must take action to fill that data in. I really don't know how I would implement this solution. The second option is the cache should understand who to call upon to generate the data that is missing. This is the option that I started to implement, and then things went south.

I started by trying to do an abstract "DataGenerator" object, from which other specific generator objects would derive. This would let the cache keep one of these objects handy so it would know how to generate it's missing data.
So this DataGenerator would have some function like
[source lang="cpp"]typeOfDataToMake generate(int sourceID);[/source]
Ok, great. Now I want my derived classes to be required to implement this "generate" member to make their type. So there might be a MeshGenerator, a CharacterGenerator, and so on, each one required to generate his type of data.

So shouldnt the method in the base class be virtual to force implementation by the derived classes? But I cannot (and probably do not want) to make it both virtual and template, as that sounds a bit over my pay grade and/or not allowed at all. Yet, it must be a template, since the return type could be anything!

Am I way off base here?
Advertisement
Why wouldn't a template work for this part as well? For instance, you can have a template function generage<T>(int sourceID), and you don't need inheritance at all.
So you're saying?
template <typename T>
class CacheLRU
{
DataGenerator<T> iMakeMissingData;
DataHolder<T> iHoldTheCachedData
}
So the cache knows at compile-time to grab the right generator object since he does know what type of data he will hold already... I think, if I follow you correctly. That makes it simpler I suppose. Maybe more involved in the writing of the dataGenerator<T> class though, since wildly different types kinda get blobbed into the same place.
I wouldn't use a dataGenerator class, since a function is probably all that's needed. In any case, writing the dataGenerator<T> class is not hard, because it only needs to know how to generate a T, and nothing else. What you seem to be missing is that you can use template specialization to define the different generators independently of each other, even though they all have the name "dataGenerator".

The situation is similar if you use functions:
template <typename T> T generate(int sourceID);

template <> Mesh generate<Mesh>(int sourceID) {
...
}

template <> Character generate<Character>(int sourceID) {
...
}
Ok, I think I understand that bit.

I'm aware of template specialization, I'm just worried that header file will get to be ginormous if I have more than a trivial amount of specializations. I guess I'll burn that bridge when I get to it, because this method is still worlds simpler than what I was trying to do.

Thanks.
How about:


// Builder type must define:
// Builder::args type which wraps all possible input arguments.
// T const& Builder.create(int id, args source_arguments);
template<typename T, typename Builder>
class cached_builder
{
public:
T const& find_or_create(int sourceID, typename Builder::args const& source_arguments)
{
T const* existing_data = iHoldTheCachedData.find(sourceID);
if (existing_data)
return *existing_data;
T const& new_data = iHoldTheCachedData.insert(builder.create(sourceID, source_arguments));
return new_data;
}
private:
DataHolder<T> iHoldTheCachedData;
Builder builder;
}

This topic is closed to new replies.

Advertisement