1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2020-2021 Thomas Kramer.
// SPDX-FileCopyrightText: 2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use super::hierarchy_reference_access::*;
use crate::prelude::{Geometry, LayerInfo, LayoutBase, Rect, SimpleTransform};

/// Trait that provides object-like read access to a layout structure and its elements.
pub trait LayoutReferenceAccess: LayoutBase {
    /// Get a cell object by its ID.
    fn shape_ref(&self, shape_id: &Self::ShapeId) -> ShapeRef<'_, Self> {
        ShapeRef {
            base: self,
            id: shape_id.clone(),
        }
    }

    /// Get a layer object by its ID.
    fn layer_ref(&self, layer_id: &Self::LayerId) -> LayerRef<'_, Self> {
        LayerRef {
            base: self,
            id: layer_id.clone(),
        }
    }

    /// Iterate over all layers defined in this layout.
    fn each_layer_ref(&self) -> Box<dyn Iterator<Item = LayerRef<'_, Self>> + '_> {
        Box::new(self.each_layer().map(move |id| LayerRef { id, base: self }))
    }

    /// Get a layer object by the layer name.
    fn layer_ref_by_name(&self, name: &str) -> Option<LayerRef<'_, Self>> {
        self.layer_by_name(name).map(|id| self.layer_ref(&id))
    }
}

impl<T: LayoutBase> LayoutReferenceAccess for T {}

impl<'a, L: LayoutBase> CellInstRef<'a, L> {
    /// Get the geometric transform that describes the location of a cell instance relative to its parent.
    pub fn get_transform(&self) -> SimpleTransform<L::Coord> {
        self.base().get_transform(&self.id)
    }
}

impl<'a, L: LayoutBase> CellRef<'a, L> {
    /// Iterate over all shapes on a layer.
    pub fn each_shape_per_layer(
        &self,
        layer_id: &L::LayerId,
    ) -> impl Iterator<Item = ShapeRef<L>> + '_ {
        self.base
            .each_shape_id(&self.id, layer_id)
            .map(move |id| ShapeRef {
                id,
                base: self.base,
            })
    }

    /// Iterate over all shapes defined in this cell.
    pub fn each_shape(&self) -> impl Iterator<Item = ShapeRef<L>> + '_ {
        self.base
            .each_layer()
            .flat_map(move |id| self.each_shape_per_layer(&id))
    }

    /// Get the bounding box of the shapes on a specific layer.
    pub fn bounding_box_per_layer(&self, layer_id: &L::LayerId) -> Option<Rect<L::Coord>> {
        self.base.bounding_box_per_layer(&self.id, layer_id)
    }

    /// Get the bounding box of the shapes on all layers.
    pub fn bounding_box(&self) -> Option<Rect<L::Coord>> {
        self.base.bounding_box(&self.id)
    }
}

/// Reference to a layer.
pub struct LayerRef<'a, L: LayoutBase + ?Sized> {
    /// Reference to the parent data structure.
    pub(super) base: &'a L,
    /// ID of the layer.
    pub(super) id: L::LayerId,
}

impl<'a, L: LayoutBase> LayerRef<'a, L> {
    /// Get the layer ID.
    pub fn id(&self) -> L::LayerId {
        self.id.clone()
    }

    /// Get the name of the layer.
    pub fn name(&self) -> Option<L::NameType> {
        self.layer_info().name.clone()
    }

    /// Get a reference to the layer-info structure.
    pub fn layer_info(&self) -> LayerInfo<L::NameType> {
        self.base.layer_info(&self.id)
    }
}

/// Reference to a shape.
pub struct ShapeRef<'a, L: LayoutBase + ?Sized> {
    /// Reference to the parent data structure.
    pub(super) base: &'a L,
    /// ID of the shape.
    pub(super) id: L::ShapeId,
}

impl<'a, L: LayoutBase> ShapeRef<'a, L> {
    /// Get the shape ID.
    pub fn id(&self) -> L::ShapeId {
        self.id.clone()
    }

    /// Get the cell where this shape lives.
    pub fn cell(&self) -> CellRef<L> {
        let id = self.base.parent_of_shape(&self.id).0;
        CellRef {
            id,
            base: self.base,
        }
    }

    /// Get the layer of the shape.
    pub fn layer(&self) -> LayerRef<L> {
        let id = self.base.parent_of_shape(&self.id).1;
        LayerRef {
            id,
            base: self.base,
        }
    }

    /// Get the cloned geometry struct representing this shape.
    pub fn geometry_cloned(&self) -> Geometry<L::Coord> {
        self.base.with_shape(&self.id, |_layer, geo| geo.clone())
    }
}