Skip to content

Commit

Permalink
feat: add support of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Makcal committed Apr 21, 2024
1 parent eb6f7ce commit 1cd0c9d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
8 changes: 6 additions & 2 deletions cpp_to_plantuml/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from clang import cindex

from cpp_to_plantuml.objects import CppClass, CppMethod, CppField, CppVar, AccessSpecifier
from cpp_to_plantuml.objects import CppClass, CppMethod, CppField, CppVar, AccessSpecifier, CppEnum
from cpp_to_plantuml.writers import AbstractWriter, PlantUmlWriter


Expand All @@ -24,7 +24,8 @@ class Converter:
classes: dict[str, CppClass]

CLASS_KINDS: set[cindex.CursorKind] = \
{cindex.CursorKind.CLASS_DECL, cindex.CursorKind.STRUCT_DECL, cindex.CursorKind.CLASS_TEMPLATE}
{cindex.CursorKind.CLASS_DECL, cindex.CursorKind.STRUCT_DECL, cindex.CursorKind.CLASS_TEMPLATE,
cindex.CursorKind.ENUM_DECL}
METHOD_KINDS: set[cindex.CursorKind] = \
{cindex.CursorKind.CXX_METHOD, cindex.CursorKind.FUNCTION_TEMPLATE, cindex.CursorKind.CONSTRUCTOR,
cindex.CursorKind.FUNCTION_DECL}
Expand Down Expand Up @@ -61,6 +62,9 @@ def _parse_class(self, cursor: cindex.Cursor):
if cursor.kind not in self.CLASS_KINDS:
raise ValueError('Cursor is not a class')

if cursor.kind == cindex.CursorKind.ENUM_DECL:
self.classes[cursor.displayname] = CppEnum(cursor.displayname)
return
cls = CppClass(cursor.displayname)
self.classes[cursor.displayname] = cls

Expand Down
17 changes: 17 additions & 0 deletions cpp_to_plantuml/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class CppClass:
base_classes: list[str] = dataclasses.field(default_factory=list)
fields: list[CppField] = dataclasses.field(default_factory=list)
methods: list[CppMethod] = dataclasses.field(default_factory=list)
is_enum: bool = False

@cached_property
def is_abstract(self):
Expand All @@ -96,3 +97,19 @@ def __str__(self):
f'Fields: [{sep}{sep.join(map(str, self.fields))}\n],\n'
f'Methods: [{sep}{sep.join(map(str, self.methods))}\n],\n'
f'Base classes: {self.base_classes}')


class CppEnum(CppClass):
def __init__(self, name: str):
super().__init__(name, is_enum=True)

@property
def is_abstract(self):
return False

@property
def is_interface(self):
return False

def __str__(self):
return f'Enum: {self.name}'
3 changes: 3 additions & 0 deletions cpp_to_plantuml/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def var_to_string(self, var: CppVar) -> str:
return f'{var.name}: {var.type}' if self.postfix_style else f'{var.type} {var.name}'

def write(self, class_: CppClass) -> str:
if class_.is_enum:
return f"enum {class_.name}"

if class_.is_interface:
res = "interface %s {\n" % class_.name
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "CppToPlantUML"
version = "2.1.0"
version = "2.2.0"
description = "A simple script that generates a PlantUML class diagram from a C++ source file."
authors = ["Maxim Fomin <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 1cd0c9d

Please sign in to comment.