cameleon_genapi/
elem_type.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5#![allow(clippy::upper_case_acronyms)]
6use std::marker::PhantomData;
7
8use super::{
9    ivalue::IValue,
10    store::{CacheStore, NodeId, NodeStore, ValueStore},
11    Device, GenApiResult, ValueCtxt,
12};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum NameSpace {
16    Standard,
17    Custom,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Visibility {
22    Beginner,
23    Expert,
24    Guru,
25    Invisible,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum MergePriority {
30    High,
31    Mid,
32    Low,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum AccessMode {
37    RO,
38    WO,
39    RW,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum ImmOrPNode<T> {
44    Imm(T),
45    PNode(NodeId),
46}
47
48impl<T> ImmOrPNode<T> {
49    pub fn imm(self) -> Option<T> {
50        match self {
51            Self::Imm(value) => Some(value),
52            _ => None,
53        }
54    }
55
56    pub fn pnode(self) -> Option<NodeId> {
57        match self {
58            Self::PNode(node) => Some(node),
59            _ => None,
60        }
61    }
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum IntegerRepresentation {
66    Linear,
67    Logarithmic,
68    Boolean,
69    PureNumber,
70    HexNumber,
71    IpV4Address,
72    MacAddress,
73}
74
75impl IntegerRepresentation {
76    /// Deduce defalut value of min element.
77    pub(super) fn deduce_min(self) -> i64 {
78        use IntegerRepresentation::{
79            Boolean, HexNumber, IpV4Address, Linear, Logarithmic, MacAddress, PureNumber,
80        };
81        match self {
82            Linear | Logarithmic | Boolean | PureNumber | HexNumber => i64::MIN,
83            IpV4Address | MacAddress => 0,
84        }
85    }
86
87    /// Deduce defalut value of max element.
88    pub(super) fn deduce_max(self) -> i64 {
89        use IntegerRepresentation::{
90            Boolean, HexNumber, IpV4Address, Linear, Logarithmic, MacAddress, PureNumber,
91        };
92        match self {
93            Linear | Logarithmic | Boolean | PureNumber | HexNumber => i64::MAX,
94            IpV4Address => 0xffff_ffff,
95            MacAddress => 0xffff_ffff_ffff,
96        }
97    }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq)]
101pub enum FloatRepresentation {
102    Linear,
103    Logarithmic,
104    PureNumber,
105}
106
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub enum Slope {
109    Increasing,
110    Decreasing,
111    Varying,
112    Automatic,
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub enum DisplayNotation {
117    Automatic,
118    Fixed,
119    Scientific,
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum StandardNameSpace {
124    None,
125    IIDC,
126    GEV,
127    CL,
128    USB,
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum CachingMode {
133    /// Allow to caching on read/write.
134    WriteThrough,
135    /// Allow to caching on read.
136    WriteAround,
137    /// Caching is not allowed.
138    NoCache,
139}
140
141#[derive(Debug, Clone, PartialEq, Eq)]
142pub struct NamedValue<T> {
143    pub(crate) name: String,
144    pub(crate) value: T,
145}
146
147impl<T> NamedValue<T> {
148    pub fn name(&self) -> &str {
149        &self.name
150    }
151
152    pub fn value(&self) -> T
153    where
154        T: Copy,
155    {
156        self.value
157    }
158
159    pub fn value_ref(&self) -> &T {
160        &self.value
161    }
162}
163
164#[derive(Debug, Clone)]
165pub enum ValueKind<T> {
166    Value(T),
167    PValue(PValue<T>),
168    PIndex(PIndex<T>),
169}
170
171impl<T> ValueKind<T> {
172    pub fn imm(&self) -> Option<T>
173    where
174        T: Copy,
175    {
176        if let Self::Value(v) = self {
177            Some(*v)
178        } else {
179            None
180        }
181    }
182
183    pub fn p_value(&self) -> Option<&PValue<T>> {
184        if let Self::PValue(v) = self {
185            Some(v)
186        } else {
187            None
188        }
189    }
190
191    pub fn p_index(&self) -> Option<&PIndex<T>> {
192        if let Self::PIndex(v) = self {
193            Some(v)
194        } else {
195            None
196        }
197    }
198}
199
200#[derive(Debug, Clone)]
201pub struct PValue<T> {
202    pub(crate) p_value: NodeId,
203    pub(crate) p_value_copies: Vec<NodeId>,
204    pub(crate) phantom: PhantomData<T>,
205}
206
207impl<T> PValue<T> {
208    #[must_use]
209    pub fn p_value(&self) -> NodeId {
210        self.p_value
211    }
212
213    #[must_use]
214    pub fn p_value_copies(&self) -> &[NodeId] {
215        &self.p_value_copies
216    }
217}
218
219#[derive(Debug, Clone)]
220pub struct PIndex<T> {
221    pub(crate) p_index: NodeId,
222    pub(crate) value_indexed: Vec<ValueIndexed<T>>,
223    pub(crate) value_default: ImmOrPNode<T>,
224}
225
226impl<T> PIndex<T> {
227    #[must_use]
228    pub fn p_index(&self) -> NodeId {
229        self.p_index
230    }
231
232    #[must_use]
233    pub fn value_indexed(&self) -> &[ValueIndexed<T>] {
234        &self.value_indexed
235    }
236
237    #[must_use]
238    pub fn value_default(&self) -> ImmOrPNode<T>
239    where
240        T: Copy,
241    {
242        self.value_default
243    }
244}
245
246#[derive(Debug, Clone)]
247pub struct ValueIndexed<T> {
248    pub(crate) index: i64,
249    pub(crate) indexed: ImmOrPNode<T>,
250}
251
252impl<T> ValueIndexed<T> {
253    #[must_use]
254    pub fn index(&self) -> i64 {
255        self.index
256    }
257
258    #[must_use]
259    pub fn indexed(&self) -> ImmOrPNode<T>
260    where
261        T: Copy,
262    {
263        self.indexed
264    }
265}
266
267#[derive(Debug, Clone)]
268pub enum AddressKind {
269    Address(ImmOrPNode<i64>),
270    IntSwissKnife(NodeId),
271    PIndex(RegPIndex),
272}
273
274impl AddressKind {
275    pub(super) fn value<T: ValueStore, U: CacheStore>(
276        &self,
277        device: &mut impl Device,
278        store: &impl NodeStore,
279        cx: &mut ValueCtxt<T, U>,
280    ) -> GenApiResult<i64> {
281        match self {
282            Self::Address(i) => i.value(device, store, cx),
283            Self::IntSwissKnife(nid) => nid.value(device, store, cx),
284            Self::PIndex(p_index) => p_index.value(device, store, cx),
285        }
286    }
287}
288
289#[derive(Debug, Clone)]
290pub struct RegPIndex {
291    pub(crate) offset: Option<ImmOrPNode<i64>>,
292    pub(crate) p_index: NodeId,
293}
294
295impl RegPIndex {
296    #[must_use]
297    pub fn offset(&self) -> Option<ImmOrPNode<i64>> {
298        self.offset
299    }
300
301    #[must_use]
302    pub fn p_index(&self) -> NodeId {
303        self.p_index
304    }
305
306    pub(super) fn value<T: ValueStore, U: CacheStore>(
307        &self,
308        device: &mut impl Device,
309        store: &impl NodeStore,
310        cx: &mut ValueCtxt<T, U>,
311    ) -> GenApiResult<i64> {
312        let base = self.p_index.value(device, store, cx)?;
313        if let Some(offset) = &self.offset {
314            let offset: i64 = offset.value(device, store, cx)?;
315            Ok(base * offset)
316        } else {
317            Ok(base)
318        }
319    }
320}
321
322#[derive(Debug, Clone, Copy, PartialEq, Eq)]
323pub enum Endianness {
324    LE,
325    BE,
326}
327
328#[derive(Debug, Clone, Copy, PartialEq, Eq)]
329pub enum Sign {
330    Signed,
331    Unsigned,
332}
333
334#[derive(Debug, Clone, Copy, PartialEq, Eq)]
335pub enum BitMask {
336    SingleBit(u64),
337    Range { lsb: u64, msb: u64 },
338}