Skip to content
Snippets Groups Projects

Improve versatility and robustness of `cpptype`, and document it in the user guide

Merged Frederik Hennig requested to merge fhennig/cpptypes into master
All threads resolved!
Viewing commit 7d4f72c5
Show latest version
2 files
+ 37
40
Preferences
Compare changes
Files
2
from __future__ import annotations
from __future__ import annotations
from typing import Any, Iterable, Sequence, Mapping
from typing import Any, Iterable, Sequence, Mapping, Callable
from abc import ABC
from abc import ABC
from dataclasses import dataclass
from dataclasses import dataclass
from itertools import chain
from itertools import chain
@@ -107,7 +107,7 @@ class CppType(PsCustomType, ABC):
@@ -107,7 +107,7 @@ class CppType(PsCustomType, ABC):
def cpptype(
def cpptype(
typestr: str, include: str | HeaderFile | Iterable[str | HeaderFile] = ()
typestr: str, include: str | HeaderFile | Iterable[str | HeaderFile] = ()
) -> type[CppType]:
) -> Callable[..., CppType | Ref]:
headers: list[str | HeaderFile]
headers: list[str | HeaderFile]
if isinstance(include, (str, HeaderFile)):
if isinstance(include, (str, HeaderFile)):
@@ -121,15 +121,14 @@ def cpptype(
@@ -121,15 +121,14 @@ def cpptype(
template_string = typestr
template_string = typestr
class_includes = frozenset(HeaderFile.parse(h) for h in headers)
class_includes = frozenset(HeaderFile.parse(h) for h in headers)
class TypeClassFactory(TypeClass):
def factory(*args, ref: bool = False, **kwargs):
def __new__(cls, *args, ref: bool = False, **kwargs):
obj = TypeClass(*args, **kwargs)
obj = TypeClass(*args, **kwargs)
if ref:
if ref:
return Ref(obj)
return Ref(obj)
else:
else:
return obj
return obj
return TypeClassFactory
return staticmethod(factory)
class Ref(PsType):
class Ref(PsType):