Skip to content

Commit 177c1fc

Browse files
committed
Print all names for an enum value
1 parent 12215fa commit 177c1fc

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

khrgenerator/cpp/CPPGenerator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,23 @@ def generate(cls, profile, api, binding):
212212
extensionsByFunction=cls.identifierPrefixGroupsDict(api, api.extensionsByFunction(), len(profile.lowercasePrefix))
213213
)
214214

215+
uniqueEnumTypes = []
215216
for type in [ api.typeByIdentifier(binding.extensionType), api.typeByIdentifier(binding.enumType), api.typeByIdentifier(binding.booleanType) ]:
216-
if type not in enumTypes and type is not None:
217-
enumTypes.append(type)
218-
217+
if type not in enumTypes and type not in uniqueEnumTypes and type is not None:
218+
uniqueEnumTypes.append(type)
219+
219220
cls.render(template_engine, "khrbinding-aux/RingBuffer.h", includedir_aux+"RingBuffer.h", api=api, profile=profile, binding=binding)
220221
cls.render(template_engine, "khrbinding-aux/RingBuffer.inl", includedir_aux+"RingBuffer.inl", api=api, profile=profile, binding=binding)
221222
cls.render(template_engine, "khrbinding-aux/types_to_string.h", includedir_aux+"types_to_string.h", api=api, profile=profile, binding=binding,
222223
enumerators=enumTypes,
224+
uniqueEnumerators=uniqueEnumTypes,
223225
bitfields=bitfieldTypes,
224226
cStringTypes=binding.cStringOutputTypes,
225227
)
226228
cls.render(template_engine, "khrbinding-aux/types_to_string.inl", includedir_aux+"types_to_string.inl", api=api, profile=profile, binding=binding)
227229
cls.render(template_engine, "khrbinding-aux/types_to_string.cpp", sourcedir_aux+"types_to_string.cpp", api=api, profile=profile, binding=binding,
228230
enumerators=enumTypes,
231+
uniqueEnumerators=uniqueEnumTypes,
229232
bitfields=bitfieldTypes,
230233
cStringTypes=binding.cStringOutputTypes,
231234
cPointerTypes=binding.cPointerTypes,

khrgenerator/cpp/templates/Meta.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class {{binding.auxApiExport}} Meta
103103
* @return
104104
* The list of all enums known by the {{profile.inputfile}}
105105
*/
106-
static std::vector<{{api.identifier}}::{{binding.enumType}}> enums();
106+
static std::set<{{api.identifier}}::{{binding.enumType}}> enums();
107107

108108
/**
109109
* @brief
@@ -273,9 +273,24 @@ class {{binding.auxApiExport}} Meta
273273
* The enum value
274274
*
275275
* @return
276-
* The string representation of the value
276+
* The shortest string representation of the value
277+
*
278+
* @remark
279+
* Beware, that some enums in the API have different symbol names but identical enum values and that this function cannot differentiate between them
277280
*/
278281
static const std::string & getString({{api.identifier}}::{{group.identifier}} {{api.identifier}}enum);
282+
283+
/**
284+
* @brief
285+
* Convert enum to symbol name string representation
286+
*
287+
* @param[in] {{api.identifierl}}enum
288+
* The enum value
289+
*
290+
* @return
291+
* All string representations of the value
292+
*/
293+
static std::vector<std::string> getStrings({{api.identifier}}::{{group.identifier}} {{api.identifier}}enum);
279294
{% endfor %}
280295

281296
private:

khrgenerator/cpp/templates/khrbinding-aux/types_to_string.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,27 @@ namespace {{api.identifier}}
1616

1717
{% for group in enumerators|sort(attribute='identifier') %}
1818
std::ostream & operator<<(std::ostream & stream, const {{group.identifier}} & value)
19+
{
20+
const auto strings = {{binding.bindingAuxNamespace}}::Meta::getStrings(value);
21+
22+
if (strings.size() == 0)
23+
{
24+
return stream;
25+
}
26+
27+
stream << strings[0];
28+
29+
for (auto i = static_cast<std::size_t>(1); i < strings.size(); ++i)
30+
stream << " | " << strings[i];
31+
32+
return stream;
33+
}
34+
{% endfor -%}
35+
{% for group in uniqueEnumerators|sort(attribute='identifier') %}
36+
std::ostream & operator<<(std::ostream & stream, const {{group.identifier}} & value)
1937
{
2038
stream << {{binding.bindingAuxNamespace}}::Meta::getString(value);
39+
2140
return stream;
2241
}
2342
{% endfor -%}
@@ -39,8 +58,7 @@ namespace {{binding.namespace}}
3958
template <>
4059
std::ostream & operator<<(std::ostream & stream, const Value<{{api.identifier}}::{{binding.enumType}}> & value)
4160
{
42-
const auto & name = {{binding.auxNamespace}}::Meta::getString(value.value());
43-
stream.write(name.c_str(), static_cast<std::streamsize>(name.size()));
61+
stream << value.value();
4462

4563
return stream;
4664
}

khrgenerator/cpp/templates/khrbinding-aux/types_to_string.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ namespace {{api.identifier}}
1919
{% for group in enumerators|sort(attribute='identifier') -%}
2020
{{binding.auxApiExport}} std::ostream & operator<<(std::ostream & stream, const {{group.identifier}} & value);
2121
{% endfor -%}
22+
{% for group in uniqueEnumerators|sort(attribute='identifier') -%}
23+
{{binding.auxApiExport}} std::ostream & operator<<(std::ostream & stream, const {{group.identifier}} & value);
24+
{% endfor -%}
2225
{% for group in bitfields|sort(attribute='identifier') -%}
2326
{{binding.auxApiExport}} std::ostream & operator<<(std::ostream & stream, const {{group.identifier}} & value);
2427
{% endfor %}

0 commit comments

Comments
 (0)