Jump to content

  • Log In with Google      Sign In   
  • Create Account

Avoid the abstract templates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 CombatWombat   Members   -  Reputation: 485

Like
0Likes
Like

Posted 12 September 2012 - 05:08 PM

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?

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13913

Like
0Likes
Like

Posted 12 September 2012 - 05:22 PM

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.

#3 CombatWombat   Members   -  Reputation: 485

Like
0Likes
Like

Posted 12 September 2012 - 05:30 PM

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.

#4 Álvaro   Crossbones+   -  Reputation: 13913

Like
1Likes
Like

Posted 12 September 2012 - 05:46 PM

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) {
  ...
}

Edited by alvaro, 12 September 2012 - 05:47 PM.


#5 CombatWombat   Members   -  Reputation: 485

Like
0Likes
Like

Posted 12 September 2012 - 05:58 PM

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.

#6 Codarki   Members   -  Reputation: 462

Like
0Likes
Like

Posted 13 September 2012 - 04:39 AM

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;
}

Edited by Codarki, 13 September 2012 - 04:40 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS