Jump to content
  • Advertisement
Sign in to follow this  
Waterlimon

Generating many configurations of a class at compile time

This topic is 1080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically I have my game world class with its various components and whatnot.

 

Now, there are some features that cannot be cleanly separated into distinct classes without ridiculous amounts of boilerplate:

-Rendering

-Networking

 

(code snippets and additional state everywhere - with pure templates managing this would mean ridiculous amounts of extra classes/funcs wrapped in template magic)

 

While waiting for static_if, I found the following approach online:

 

object.h:

#once
template<bool FeatureA, bool FeatureB>
class Object;
{
}
//generate all specializations with preprocessor
#define F_A true
#define F_B true
#include "ObjectTemplate.h"
#define F_A false
#define F_B true
#include "ObjectTemplate.h"
#define F_A true
#define F_B false
#include "ObjectTemplate.h"
#define F_A false
#define F_B false
#include "ObjectTemplate.h"

ObjectTemplate.h:

template<>
class Object<F_A,F_B>
{
#if F_A
  int a;
#endif

#if F_B
  int b;
#endif

  int someFunc()
  {
#if F_A && F_B
    return a + b;
#elif F_A
    return a + 5;
#elif F_B
    return b;
#endif
    return 0;
  }
}

The result is a template (the preprocessor usage is encapsulated within this software component). So I dont get the issues with "globality" that would come with straight up using some COMPILING_SERVER define or such.

 

Anyone see any glaring flaws with this approach?

 

EDIT:

Intellisense seems to hate it which is to be expected I guess... Just me being silly and instantiating with params I didnt specialize for

Edited by Waterlimon

Share this post


Link to post
Share on other sites
Advertisement

There's nothing fundamentally wrong with this approach. Boost has a preprocessor library for supporting this kind of thing if you try to do anything more complex than your example. One important implementation note: if you have a header designed to be included multiple times, save yourself some maintenance headaches and stick a comment up top saying that the lack of header guards or pragma once is deliberate.

Share this post


Link to post
Share on other sites
The above approach works ok, but I would seriously try exhausting all other approaches first before trying it.

Having used such an approach in order to auto-generate a series of templates to fake vararg templates on a compiler that doesn't support it they are a nightmare to debug and maintain. I pretty much got it working and then shoved it off in it's own little capsule where it was needed, heavily wrapped to try to prevent misuse.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!