Skip to content

Commit

Permalink
codegen: Move enum->string header code to Khronos
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkY-LunarG authored and jzulauf-lunarg committed Jan 22, 2025
1 parent b2e83e3 commit a8acb05
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python3 -i
#
# Copyright (c) 2021 LunarG, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import os, re, sys, inspect
from khronos_base_generator import write


# KhronosEnumToStringHeaderGenerator
# Generates C++ functions for stringifying Khronos API enums.
class KhronosEnumToStringHeaderGenerator():
"""Generate C++ functions for Khronos ToString() functions"""

def write_enum_to_string_header(self):
for enum in sorted(self.enum_names):
if not enum in self.enumAliases:
if self.is_flags_enum_64bit(enum):
body = 'std::string {0}ToString(const {0} value);'
body += '\nstd::string {1}ToString(VkFlags64 vkFlags);'
else:
body = 'template <> std::string ToString<{0}>(const {0}& value, ToStringFlags toStringFlags, uint32_t tabCount, uint32_t tabSize);'
if 'Bits' in enum:
body += '\ntemplate <> std::string ToString<{0}>(VkFlags vkFlags, ToStringFlags toStringFlags, uint32_t tabCount, uint32_t tabSize);'
write(
body.format(enum, self.get_flags_type_from_enum(enum)),
file=self.outFile
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import os, re, sys, inspect
from base_generator import *
from khronos_enum_to_string_header_generator import KhronosEnumToStringHeaderGenerator


class VulkanEnumToStringHeaderGeneratorOptions(BaseGeneratorOptions):
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(

# VulkanEnumToStringHeaderGenerator - subclass of BaseGenerator.
# Generates C++ functions for stringifying Vulkan API enums.
class VulkanEnumToStringHeaderGenerator(BaseGenerator):
class VulkanEnumToStringHeaderGenerator(BaseGenerator, KhronosEnumToStringHeaderGenerator):
"""Generate C++ functions for Vulkan ToString() functions"""

def __init__(
Expand Down Expand Up @@ -90,7 +91,7 @@ def beginFile(self, genOpts):
# Method override
# yapf: disable
def endFile(self):
self.write_enum_to_string_header()
KhronosEnumToStringHeaderGenerator.write_enum_to_string_header(self)

body = inspect.cleandoc('''
GFXRECON_END_NAMESPACE(util)
Expand All @@ -108,18 +109,3 @@ def need_feature_generation(self):
if self.feature_struct_members:
return True
return False

#
# Performs C++ code generation for the enum to string header.
def write_enum_to_string_header(self):
for enum in sorted(self.enum_names):
if not enum in self.enumAliases:
if self.is_flags_enum_64bit(enum):
body = 'std::string {0}ToString(const {0} value);'
body += '\nstd::string {1}ToString(VkFlags64 vkFlags);'
else:
body = 'template <> std::string ToString<{0}>(const {0}& value, ToStringFlags toStringFlags, uint32_t tabCount, uint32_t tabSize);'
if 'Bits' in enum:
body += '\ntemplate <> std::string ToString<{0}>(VkFlags vkFlags, ToStringFlags toStringFlags, uint32_t tabCount, uint32_t tabSize);'
write(body.format(enum, self.get_flags_type_from_enum(enum)),
file=self.outFile)

0 comments on commit a8acb05

Please sign in to comment.