1 # -*- coding: utf-8 -*-
3 from itertools import chain
4 from format import indentLines
6 def isValueDefined (definitions, value):
7 return value in definitions
9 def allValuesUndefined (definitions, values):
11 if isValueDefined(definitions, value):
15 def anyValueDefined (definitions, values):
16 return not allValuesUndefined(definitions, values)
18 def makeDefSet (iface):
19 return set(enum.name for enum in iface.enums) | \
20 set(enum.alias for enum in iface.enums if enum.alias != None)
22 def genStrUtilProtos (iface, enumGroups, bitfieldGroups):
23 definitions = makeDefSet(iface)
26 for groupName, values in enumGroups:
27 if anyValueDefined(definitions, values):
28 yield "const char*\tget%sName\t(int value);" % groupName
30 print "Warning: Empty value set for %s, skipping" % groupName
32 def genBitfieldProtos ():
33 for groupName, values in bitfieldGroups:
34 if anyValueDefined(definitions, values):
35 yield "tcu::Format::Bitfield<16>\tget%sStr\t(int value);" % groupName
37 print "Warning: Empty value set for %s, skipping" % groupName
40 for groupName, values in enumGroups:
41 if anyValueDefined(definitions, values):
42 yield "inline tcu::Format::Enum<int, 2>\tget%(name)sStr\t(int value)\t{ return tcu::Format::Enum<int, 2>(get%(name)sName, value); }" % {"name": groupName}
44 # booleans can be stored in enums or in byte-sized arrays. For clarity add special case
45 if anyValueDefined(definitions, ["GL_TRUE", "GL_FALSE"]):
46 yield "inline tcu::Format::Enum<int, 1>\tgetBooleanStr\t(deUint8 value)\t{ return tcu::Format::Enum<int, 1>(getBooleanName, (int)value); }"
48 return chain(genNameProtos(), genBitfieldProtos(), genStrImpl())
50 def genEnumStrImpl (groupName, values, definitions):
51 if allValuesUndefined(definitions, values):
55 yield "const char* get%sName (int value)" % groupName
57 yield "\tswitch (value)"
62 if isValueDefined(definitions, value):
63 yield "case %s:\treturn \"%s\";" % (value, value)
65 print "Warning: %s not defined, skipping" % value
66 yield "default:\treturn DE_NULL;"
68 for caseLine in indentLines(genCases()):
69 yield "\t\t" + caseLine
74 def genBitfieldStrImpl (groupName, values, definitions):
75 if allValuesUndefined(definitions, values):
79 yield "tcu::Format::Bitfield<16> get%sStr (int value)" % groupName
81 yield "\tstatic const tcu::Format::BitDesc s_desc[] ="
86 if isValueDefined(definitions, value):
87 yield "tcu::Format::BitDesc(%s,\t\"%s\")," % (value, value)
89 print "Warning: %s not defined, skipping" % value
91 for fieldLine in indentLines(genFields()):
92 yield "\t\t" + fieldLine
95 yield "\treturn tcu::Format::Bitfield<16>(value, &s_desc[0], &s_desc[DE_LENGTH_OF_ARRAY(s_desc)]);"
98 def genStrUtilImpls (iface, enumGroups, bitfieldGroups):
99 definitions = makeDefSet(iface)
101 for groupName, values in enumGroups:
102 for line in genEnumStrImpl(groupName, values, definitions):
104 for groupName, values in bitfieldGroups:
105 for line in genBitfieldStrImpl(groupName, values, definitions):
108 def genEnumUtilImpl (groupName, groupQueries, allEnums):
110 yield "int get%sQueryNumArgsOut (int pname)" % groupName
112 yield "\tswitch(pname)"
116 for enumName, enumQueryNumOutputs in groupQueries:
117 if enumName in allEnums:
118 yield "case %s:\treturn %s;" % (enumName, enumQueryNumOutputs)
120 print "Warning: %s not defined, skipping" % enumName
121 yield "default:\treturn 1;"
123 for caseLine in indentLines(genCases()):
124 yield "\t\t" + caseLine
129 def genEnumUtilImpls (iface, queryGroups):
130 allEnums = makeDefSet(iface)
132 for groupName, groupQueries in queryGroups:
133 for line in genEnumUtilImpl(groupName, groupQueries, allEnums):
136 def addValuePrefix (groups, prefix):
137 return [(groupName, [prefix + value for value in values]) for groupName, values in groups]