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