cameleon_genapi/
integer.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
5use super::{
6    elem_type::{ImmOrPNode, IntegerRepresentation, ValueKind},
7    interface::{IInteger, INode, ISelector, IncrementMode},
8    ivalue::IValue,
9    node_base::{NodeAttributeBase, NodeBase, NodeElementBase},
10    store::{CacheStore, IntegerId, NodeId, NodeStore, ValueStore},
11    Device, GenApiResult, ValueCtxt,
12};
13
14#[derive(Debug, Clone)]
15pub struct IntegerNode {
16    pub(crate) attr_base: NodeAttributeBase,
17    pub(crate) elem_base: NodeElementBase,
18
19    pub(crate) streamable: bool,
20    pub(crate) value_kind: ValueKind<IntegerId>,
21    pub(crate) min: ImmOrPNode<IntegerId>,
22    pub(crate) max: ImmOrPNode<IntegerId>,
23    pub(crate) inc: ImmOrPNode<i64>,
24    pub(crate) unit: Option<String>,
25    pub(crate) representation: IntegerRepresentation,
26    pub(crate) p_selected: Vec<NodeId>,
27}
28
29impl IntegerNode {
30    #[must_use]
31    pub fn value_kind(&self) -> &ValueKind<IntegerId> {
32        &self.value_kind
33    }
34
35    #[must_use]
36    pub fn min_elem(&self) -> ImmOrPNode<IntegerId> {
37        self.min
38    }
39
40    #[must_use]
41    pub fn max_elem(&self) -> ImmOrPNode<IntegerId> {
42        self.max
43    }
44
45    #[must_use]
46    pub fn inc_elem(&self) -> ImmOrPNode<i64> {
47        self.inc
48    }
49
50    #[must_use]
51    pub fn unit_elem(&self) -> Option<&str> {
52        self.unit.as_deref()
53    }
54
55    #[must_use]
56    pub fn representation_elem(&self) -> IntegerRepresentation {
57        self.representation
58    }
59
60    #[must_use]
61    pub fn p_selected(&self) -> &[NodeId] {
62        &self.p_selected
63    }
64}
65
66impl INode for IntegerNode {
67    fn node_base(&self) -> NodeBase {
68        NodeBase::new(&self.attr_base, &self.elem_base)
69    }
70
71    fn streamable(&self) -> bool {
72        self.streamable
73    }
74}
75
76impl IInteger for IntegerNode {
77    #[tracing::instrument(skip(self, device, store, cx),
78                          level = "trace",
79                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
80    fn value<T: ValueStore, U: CacheStore>(
81        &self,
82        device: &mut impl Device,
83        store: &impl NodeStore,
84        cx: &mut ValueCtxt<T, U>,
85    ) -> GenApiResult<i64> {
86        self.value_kind().value(device, store, cx)
87    }
88
89    #[tracing::instrument(skip(self, device, store, cx),
90                          level = "trace",
91                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
92    fn set_value<T: ValueStore, U: CacheStore>(
93        &self,
94        value: i64,
95        device: &mut impl Device,
96        store: &impl NodeStore,
97        cx: &mut ValueCtxt<T, U>,
98    ) -> GenApiResult<()> {
99        cx.invalidate_cache_by(self.node_base().id());
100        self.value_kind().set_value(value, device, store, cx)
101    }
102
103    #[tracing::instrument(skip(self, device, store, cx),
104                          level = "trace",
105                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
106    fn min<T: ValueStore, U: CacheStore>(
107        &self,
108        device: &mut impl Device,
109        store: &impl NodeStore,
110        cx: &mut ValueCtxt<T, U>,
111    ) -> GenApiResult<i64> {
112        self.min.value(device, store, cx)
113    }
114
115    #[tracing::instrument(skip(self, device, store, cx),
116                          level = "trace",
117                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
118    fn max<T: ValueStore, U: CacheStore>(
119        &self,
120        device: &mut impl Device,
121        store: &impl NodeStore,
122        cx: &mut ValueCtxt<T, U>,
123    ) -> GenApiResult<i64> {
124        self.max.value(device, store, cx)
125    }
126
127    fn inc_mode(&self, _: &impl NodeStore) -> Option<IncrementMode> {
128        Some(IncrementMode::FixedIncrement)
129    }
130
131    #[tracing::instrument(skip(self, device, store, cx),
132                          level = "trace",
133                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
134    fn inc<T: ValueStore, U: CacheStore>(
135        &self,
136        device: &mut impl Device,
137        store: &impl NodeStore,
138        cx: &mut ValueCtxt<T, U>,
139    ) -> GenApiResult<Option<i64>> {
140        Some(self.inc.value(device, store, cx)).transpose()
141    }
142
143    fn valid_value_set(&self, _: &impl NodeStore) -> &[i64] {
144        &[]
145    }
146
147    fn representation(&self, _: &impl NodeStore) -> IntegerRepresentation {
148        self.representation_elem()
149    }
150
151    fn unit(&self, _: &impl NodeStore) -> Option<&str> {
152        self.unit_elem()
153    }
154
155    #[tracing::instrument(skip(self, device, store, cx),
156                          level = "trace",
157                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
158    fn set_min<T: ValueStore, U: CacheStore>(
159        &self,
160        value: i64,
161        device: &mut impl Device,
162        store: &impl NodeStore,
163        cx: &mut ValueCtxt<T, U>,
164    ) -> GenApiResult<()> {
165        self.min.set_value(value, device, store, cx)
166    }
167
168    #[tracing::instrument(skip(self, device, store, cx),
169                          level = "trace",
170                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
171    fn set_max<T: ValueStore, U: CacheStore>(
172        &self,
173        value: i64,
174        device: &mut impl Device,
175        store: &impl NodeStore,
176        cx: &mut ValueCtxt<T, U>,
177    ) -> GenApiResult<()> {
178        self.max.set_value(value, device, store, cx)
179    }
180
181    #[tracing::instrument(skip(self, device, store, cx),
182                          level = "trace",
183                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
184    fn is_readable<T: ValueStore, U: CacheStore>(
185        &self,
186        device: &mut impl Device,
187        store: &impl NodeStore,
188        cx: &mut ValueCtxt<T, U>,
189    ) -> GenApiResult<bool> {
190        Ok(self.elem_base.is_readable(device, store, cx)?
191            && IValue::<i64>::is_readable(&self.value_kind, device, store, cx)?)
192    }
193
194    #[tracing::instrument(skip(self, device, store, cx),
195                          level = "trace",
196                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
197    fn is_writable<T: ValueStore, U: CacheStore>(
198        &self,
199        device: &mut impl Device,
200        store: &impl NodeStore,
201        cx: &mut ValueCtxt<T, U>,
202    ) -> GenApiResult<bool> {
203        Ok(self.elem_base.is_writable(device, store, cx)?
204            && IValue::<i64>::is_writable(&self.value_kind, device, store, cx)?)
205    }
206}
207
208impl ISelector for IntegerNode {
209    fn selecting_nodes(&self, _: &impl NodeStore) -> GenApiResult<&[NodeId]> {
210        Ok(self.p_selected())
211    }
212}