File: TypeDefinition.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (60 lines) | stat: -rw-r--r-- 1,914 bytes parent folder | download | duplicates (2)
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

#include "TypeDefinition.h"

namespace actions {
namespace expression {

// Integers can be safely converted to floats (for the most part)
TypeDefinition TypeDefinition::s_integer("Integer", {ValueType::Float});
// No implicit conversions here
TypeDefinition TypeDefinition::s_float("Float", {});
TypeDefinition TypeDefinition::s_vector("Vector", {});
// Identifiers are a technicality needed for making non-math values work but they have no further use other than their
// string content
TypeDefinition TypeDefinition::s_identifier("Identifier", {});

TypeDefinition::TypeDefinition(SCP_string name, SCP_vector<actions::expression::ValueType> allowedImplicitConversion)
	: m_name(std::move(name)), m_allowedImplicitConversions(std::move(allowedImplicitConversion))
{
}

const SCP_string& TypeDefinition::getName() const
{
	return m_name;
}
const SCP_vector<actions::expression::ValueType>& TypeDefinition::getAllowedImplicitConversions() const
{
	return m_allowedImplicitConversions;
}
const TypeDefinition& TypeDefinition::forValueType(ValueType type)
{
	switch (type) {
	case ValueType::Integer:
		return s_integer;
	case ValueType::Float:
		return s_float;
	case ValueType::Vector:
		return s_vector;
	case ValueType::String:
		return s_identifier;
	default:
		UNREACHABLE("Invalid value type!");
		return s_integer; // Make compiler happy
	}
}

bool checkTypeWithImplicitConversion(ValueType currentType, ValueType expectedType)
{
	if (currentType == expectedType) {
		// No need to consider implicit conversions;
		return true;
	}

	const auto& currentTypeDef = TypeDefinition::forValueType(currentType);
	const auto& allowedConversions = currentTypeDef.getAllowedImplicitConversions();

	// For now, no transitiv conversions are allowed
	return std::find(allowedConversions.cbegin(), allowedConversions.cend(), expectedType) != allowedConversions.cend();
}
} // namespace expression
} // namespace actions