|
| 1 | +# |
| 2 | +# This file is part of libdestruct (https://github.com/mrindeciso/libdestruct). |
| 3 | +# Copyright (c) 2026 Roberto Alessandro Bertolini. All rights reserved. |
| 4 | +# Licensed under the MIT license. See LICENSE file in the project root for details. |
| 5 | +# |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import TYPE_CHECKING |
| 10 | + |
| 11 | +from libdestruct.common.obj import obj |
| 12 | + |
| 13 | +if TYPE_CHECKING: # pragma: no cover |
| 14 | + from libdestruct.backing.resolver import Resolver |
| 15 | + |
| 16 | + |
| 17 | +class union(obj): |
| 18 | + """A union value, supporting both tagged (single active variant) and plain (all variants overlaid) modes.""" |
| 19 | + |
| 20 | + _variant: obj | None |
| 21 | + """The single active variant (tagged union mode).""" |
| 22 | + |
| 23 | + _variants: dict[str, obj] |
| 24 | + """Named variants (plain union mode).""" |
| 25 | + |
| 26 | + _frozen_bytes: bytes | None |
| 27 | + """The frozen bytes of the full union region.""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self: union, |
| 31 | + resolver: Resolver | None, |
| 32 | + variant: obj | None, |
| 33 | + max_size: int, |
| 34 | + variants: dict[str, obj] | None = None, |
| 35 | + ) -> None: |
| 36 | + """Initialize the union. |
| 37 | +
|
| 38 | + Args: |
| 39 | + resolver: The backing resolver. |
| 40 | + variant: The single active variant (tagged union mode, None for plain unions). |
| 41 | + max_size: The size of the union (max of all variant sizes). |
| 42 | + variants: Named variants dict (plain union mode, None for tagged unions). |
| 43 | + """ |
| 44 | + super().__init__(resolver) |
| 45 | + self._variant = variant |
| 46 | + self._variants = variants or {} |
| 47 | + self.size = max_size |
| 48 | + self._frozen_bytes = None |
| 49 | + |
| 50 | + @property |
| 51 | + def variant(self: union) -> obj | None: |
| 52 | + """Return the active variant object (tagged union mode).""" |
| 53 | + return self._variant |
| 54 | + |
| 55 | + def get(self: union) -> object: |
| 56 | + """Return the value of the active variant.""" |
| 57 | + if self._variant is not None: |
| 58 | + return self._variant.get() |
| 59 | + if self._variants: |
| 60 | + return {name: v.get() for name, v in self._variants.items()} |
| 61 | + return None |
| 62 | + |
| 63 | + def _set(self: union, value: object) -> None: |
| 64 | + """Set the value of the active variant.""" |
| 65 | + if self._variant is None: |
| 66 | + raise RuntimeError("Cannot set the value of a union without an active variant.") |
| 67 | + self._variant._set(value) |
| 68 | + |
| 69 | + def to_bytes(self: union) -> bytes: |
| 70 | + """Return the full union-sized region as bytes.""" |
| 71 | + if self._frozen_bytes is not None: |
| 72 | + return self._frozen_bytes |
| 73 | + if self.resolver is None: |
| 74 | + return b"\x00" * self.size |
| 75 | + return self.resolver.resolve(self.size, 0) |
| 76 | + |
| 77 | + def freeze(self: union) -> None: |
| 78 | + """Freeze the union and all its variants.""" |
| 79 | + if self.resolver is not None: |
| 80 | + self._frozen_bytes = self.resolver.resolve(self.size, 0) |
| 81 | + else: |
| 82 | + self._frozen_bytes = b"\x00" * self.size |
| 83 | + if self._variant is not None: |
| 84 | + self._variant.freeze() |
| 85 | + for v in self._variants.values(): |
| 86 | + v.freeze() |
| 87 | + super().freeze() |
| 88 | + |
| 89 | + def to_str(self: union, indent: int = 0) -> str: |
| 90 | + """Return a string representation of the union.""" |
| 91 | + if self._variant is not None: |
| 92 | + return self._variant.to_str(indent) |
| 93 | + if self._variants: |
| 94 | + members = ", ".join(self._variants) |
| 95 | + return f"union({members})" |
| 96 | + return "union(empty)" |
| 97 | + |
| 98 | + def __getattr__(self: union, name: str) -> object: |
| 99 | + """Delegate attribute access to named variants or the active variant.""" |
| 100 | + variants = object.__getattribute__(self, "_variants") |
| 101 | + if name in variants: |
| 102 | + return variants[name] |
| 103 | + variant = object.__getattribute__(self, "_variant") |
| 104 | + if variant is not None: |
| 105 | + return getattr(variant, name) |
| 106 | + raise AttributeError(f"'{type(self).__name__}' has no attribute '{name}'") |
0 commit comments