Passing A Type

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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";
	}
	else if (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>;
		}
		else if (...)
		{
		}
		
		if (ptr != NULL)
		{
			typeList.push_back(ptr);
		}
	}
}
Last edited on
You must specify your types manually.
1
2
if(typeName == "int") ptr = new IntegerType<int>;
else if(typeName == "float") ptr = new IntegerType<float>;
Yeah, thinking it through, I realize that I need to know that template parameter type at compile time.

I think I might try adding a clone function. Then I can do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Type
{
	Type* clone() const = 0;
};

template <typename T>
class IntegerType : public Type
{
	Type* clone() const { return new IntegerType<T>; }
};

class TypeChecker
{
public:
	// I'm pretty sure I got the const correct on model
	bool isAllowed(const std::string& input, const Type*& model); 
};

TypeChecker::isAllowed(const std::string& input, const Type*& model)
{
	static IntegerType<int32_t> intType;
	static IntegerType<int16_t> shortType;

	bool ok = true;
	if (input == "integer")
	{
		model = &intType;
	}
	else if (input == "short")
	{
		model = &shortType;
	}
	// else if (...) ...
	else
	{
		ok = false;
	}
	return ok;
}




void generateTypes(std::list<Type*> typeList)
{
	// while reading input file
	{
		Type const* ptr = NULL;
		// read inputName
		Type* model;
		if (isAllowed(inputName, model) == true)
		{
			ptr = model->clone();
		}
		else if (...)
		{
		}
		
		if (ptr != NULL)
		{
			typeList.push_back(ptr);
		}
	}
}
Hi,

Just my two cents worth here :+)

Could you have a play with boost::mpl : A template meta-programming library.

I managed to easily create some types here:

http://www.cplusplus.com/forum/lounge/184419/#msg901920

In my code, Radius etcetera are all types, and can be use as such in templated code.

Hopefully you might be able to adapt that idea so it would be suitable for what you are doing.

Edit: Although this won't be much help at runtime.
Last edited on
Topic archived. No new replies allowed.