• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CombatWombat

Avoid the abstract templates

5 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:
[code]template <typename T> T generate(int sourceID);

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

template <> Character generate<Character>(int sourceID) {
...
}
[/code] Edited by alvaro
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
How about:

[code]
// 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;
}
[/code] Edited by Codarki
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0