aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL/libghdl/_decorator.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyGHDL/libghdl/_decorator.py')
-rw-r--r--pyGHDL/libghdl/_decorator.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/pyGHDL/libghdl/_decorator.py b/pyGHDL/libghdl/_decorator.py
new file mode 100644
index 000000000..01863381a
--- /dev/null
+++ b/pyGHDL/libghdl/_decorator.py
@@ -0,0 +1,33 @@
+from functools import wraps
+from typing import Callable, List
+
+from pydecor import export
+
+
+@export
+def EnumLookupTable(cls) -> Callable:
+ """
+ Decorator to precalculate a enum lookup table (LUT) for enum position to
+ enum literal name.
+
+ .. todo:: Make compatible to chained decorators
+
+ :param cls: Enumerator class for which a LUT shall be pre-calculated.
+ """
+ def decorator(func) -> Callable:
+ def gen() -> List[str]:
+ d = [e for e in dir(cls) if e[0] != "_"]
+ res = [None] * len(d)
+ for e in d:
+ res[getattr(cls, e)] = e
+ return res
+
+ __lut = gen()
+
+ def wrapper(id: int) -> str:
+ # function that replaces the placeholder function
+ return __lut[id]
+
+ return wrapper
+
+ return decorator