Saturday, 7 September 2013

Assign CRTP static members at runtime

Assign CRTP static members at runtime

I have a set of classes which I am extending using the CRTP to do a
mixin-style so that each type can have separate static member variables.
All of the classes inherit from GdsDataObject. There are 36 separate
classes that do this, and they do not have uniform class mixins. This is
an example of one of the classes, names GdsAircraft.
template <typename T>
class GdsMixinLOD
{
protected:
static unsigned int min_lod;
static unsigned int max_lod;
}
template <typename T> unsigned int GdsMixinLOD<T>::min_lod = 10;
template <typename T> unsigned int GdsMixinLOD<T>::max_lod = 1000;
class GdsAircraft :
public GdsDataObject,
public GdsMixinLOD<GdsAircraft>,
public GdsMixinShared<GdsAircraft> // Mixin shared by all classes
{
...
}
At runtime I load configuration options from a file and set the static
member variables using those values. Right now I use a loop to detect the
data type the option is meant for and then call one or more template
functions to assign the values. I have to manually call additional
functions for classes that have additional mixins. I would like to do all
this with a single template function and somehow have that function only
set static variables for classes that actually have those variables.
template<typename GdsType_T>
void setGdsStaticConfigLODValues(const OptionTokens &options)
{
for(auto &data_type_option : options) {
if(data_type_option.first == "MIN_LOD") {
GdsType_T::min_lod = boost::lexical_cast<unsigned int>(options.second);
} else if(data_type_option.first == "MIN_LOD") {
GdsType_T::min_lod = boost::lexical_cast<unsigned int>(options.second);
}
}
}
template<typename GdsType_T>
void setGdsStaticConfigSharedValues(const OptionTokens &options)
{
for(auto &data_type_option : options) {
// Loop just like in setGdsStaticConfigLODValues function
}
}

No comments:

Post a Comment