1
0

feat: update flag enum and enum underlying type decision rules for codegen

This commit is contained in:
2026-02-11 22:49:54 +08:00
parent 3310cac100
commit 4619cb5d1a
10 changed files with 211 additions and 64 deletions

View File

@@ -87,9 +87,9 @@ class BEnum:
"""The name of this enum."""
__enum_comment: str | None
"""The comment of this enum. None if no comment."""
__can_unsigned: bool
__is_unsigned: bool
"""True if this enum can use unsigned integer as its underlying type."""
__use_flags: bool
__is_flag: bool
"""True if this enum will use flags feature (supporting OR, AND, operators)."""
__entries: list[BEnumEntry]
"""The list to store entries of this enum."""
@@ -101,14 +101,14 @@ class BEnum:
self,
enum_name: str,
enum_comment: str | None,
can_unsigned: bool,
use_flags: bool,
is_unsigned: bool,
is_flag: bool,
entries: list[BEnumEntry],
):
self.__enum_name = enum_name
self.__enum_comment = enum_comment
self.__can_unsigned = can_unsigned
self.__use_flags = use_flags
self.__is_unsigned = is_unsigned
self.__is_flag = is_flag
self.__entries = entries
self.__entries_map = {e.get_entry_name(): e for e in entries}
@@ -120,13 +120,13 @@ class BEnum:
"""Get the comment of this enum. None if no comment."""
return self.__enum_comment
def get_can_unsigned(self) -> bool:
def is_unsigned(self) -> bool:
"""True if this enum can use unsigned integer as its underlying type."""
return self.__can_unsigned
return self.__is_unsigned
def get_use_flags(self) -> bool:
def is_flag(self) -> bool:
"""True if this enum will use flags feature (supporting OR, AND, operators)."""
return self.__use_flags
return self.__is_flag
def iter_entries(self) -> typing.Iterator[BEnumEntry]:
"""Get the iterator of entries of this enum."""
@@ -140,8 +140,8 @@ class BEnum:
return BEnum(
data["name"],
data.get("comment", None),
data["can_unsigned"],
data["use_flags"],
data["is_unsigned"],
data["is_flag"],
list(map(lambda i: BEnum.__create_entry_by_content(i), data["entries"])),
)

View File

@@ -11,6 +11,7 @@ def main():
render.render_cpp_enum("CKERROR.hpp", ckerror)
render.render_py_enum("CKERROR.py", ckerror)
render.render_cs_enum("CKERROR.cs", ckerror)
render.render_rs_enum("CKERROR.rs", ckerror)
render.render_cpp_ckerror_docstring("CKERROR.docstring.hpp", "CKERROR.docstring.cpp", ckerror)
render.render_py_enum_docstring("CKERROR.docstring.py", ckerror)
render.render_cs_enum_docstring("CKERROR.docstring.cs", ckerror)

View File

@@ -1,9 +1,9 @@
{%- for benum in payload.iter_enums() %}
{%- if benum.get_enum_comment() is not none %}
{{ benum.get_enum_comment() | block_comment('/// ') }}
{%- endif %} {%- if benum.get_use_flags() %}
{%- endif %} {%- if benum.is_flag() %}
[Flags]{%- endif %}
public enum {{ benum.get_enum_name() }} : {% if benum.get_can_unsigned() -%} uint {%- else -%} int {%- endif %} {
public enum {{ benum.get_enum_name() }} : {% if benum.is_unsigned() -%} uint {%- else -%} int {%- endif %} {
{%- for entry in benum.iter_entries() %}
{%- if entry.get_entry_comment() is not none %}
/// <summary>

View File

@@ -4,7 +4,7 @@
{{ benum.get_enum_comment() | block_comment(' * ') }}
*/
{%- endif %}
enum class {{ benum.get_enum_name() }} : {% if benum.get_can_unsigned() -%} CKDWORD {%- else -%} CKINT {%- endif %} {
enum class {{ benum.get_enum_name() }} : {% if benum.is_unsigned() -%} CKDWORD {%- else -%} CKINT {%- endif %} {
{%- for entry in benum.iter_entries() %}
{{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ entry.get_entry_value() }} {%- endif %}, {%- if entry.get_entry_comment() is not none %} /**< {{ entry.get_entry_comment() | line_comment }} */ {%- endif %}
{%- endfor %}

View File

@@ -2,9 +2,10 @@
{%- if benum.get_enum_comment() is not none %}
{{ benum.get_enum_comment() | block_comment('/// ') }}
{%- endif %}
{%- if not benum.get_use_flags() %}
{%- if not benum.is_flag() %}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr({% if benum.get_can_unsigned() -%} u32 {%- else -%} i32 {%- endif %})]
#[repr({% if benum.is_unsigned() -%} u32 {%- else -%} i32 {%- endif %})]
pub enum {{ benum.get_enum_name() }} {
{%- for entry in benum.iter_entries() %}
{%- if entry.get_entry_comment() is not none %}
@@ -16,10 +17,10 @@ pub enum {{ benum.get_enum_name() }} {
{%- else %}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct {{ benum.get_enum_name() }}({% if benum.get_can_unsigned() -%} u32 {%- else -%} i32 {%- endif %});
pub struct {{ benum.get_enum_name() }}({% if benum.is_unsigned() -%} u32 {%- else -%} i32 {%- endif %});
bitflags! {
impl {{ benum.get_enum_name() }}: {% if benum.get_can_unsigned() -%} u32 {%- else -%} i32 {%- endif %} {
impl {{ benum.get_enum_name() }}: {% if benum.is_unsigned() -%} u32 {%- else -%} i32 {%- endif %} {
{%- for entry in benum.iter_entries() %}
{%- if entry.get_entry_comment() is not none %}
/// {{ entry.get_entry_comment() | line_comment }}