In my project I am generating template classes based on an input description. When working with integer types, for instance, I have to run through a list of accepted types in one function (while verifying input and collecting the full list of types being generated)) and then I run through a similar list in a different function when generating the types. I would like to use a common class to handle the checking to reduce maintenance.
The problem is, when I generate the types, I need to pass in a type returned from the common class. I'm looking at traits and decltype and don't know if I can use them or not.
I am trying to do something like this. Notice the problem in line 41.
class TypeChecker
{
public:
// !!! Need a type argument rather than a string argument here
bool isAllowed(const std::string& input, std::string& typeName);
};
TypeChecker::isAllowed(const std::string& input, std::string& typeName)
{
bool ok = true;
if (input == "integer")
{
typeName = "int32_t";
}
elseif (input == "short")
{
typeName = "uint16_t";
}
// else if (...) ...
else
{
ok = false;
}
return ok;
}
class Type {};
template <typename T>
class IntegerType : public Type {};
void generateTypes(std::list<Type*> typeList)
{
// while reading input file
{
Type* ptr = NULL;
// read inputName
std::string typeName;
if (isAllowed(inputName, typeName) == true)
{
// This won't work because typeName is a string, not a type.
ptr = new IntegerType<typeName>;
}
elseif (...)
{
}
if (ptr != NULL)
{
typeList.push_back(ptr);
}
}
}