1#![allow(non_camel_case_types)]
2use {
3 std::fmt::{Formatter,Debug, Error},
4 std::marker::PhantomData,
5 std::{
6 sync::Arc,
7 sync::atomic::{AtomicU32, AtomicI32, AtomicI64, Ordering, AtomicBool},
8 },
9 crate::{
10 live_traits::*,
11 makepad_live_compiler::*,
12 cx::Cx,
13 }
14};
15
16pub trait LiveAtomicValue {
17 fn apply_value_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize;
18}
19
20pub trait LiveAtomic {
21 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize;
22}
23
24pub trait LiveAtomicU32Enum {
25 fn as_u32(&self) -> u32;
26 fn from_u32(val: u32) -> Self;
27}
28
29pub struct U32A<T>(AtomicU32, PhantomData<T>) where T: LiveAtomicU32Enum;
34
35impl <T> U32A<T> where T: LiveAtomicU32Enum {
36 pub fn set(&self, val: T) {
37 self.0.store(val.as_u32(), Ordering::Relaxed)
38 }
39
40 pub fn get(&self) -> T {
41 T::from_u32(self.0.load(Ordering::Relaxed))
42 }
43}
44
45impl <T> Clone for U32A<T> where T: LiveAtomicU32Enum {
46 fn clone(&self)->Self{
47 let t = self.get();
48 U32A(AtomicU32::new(t.as_u32()), PhantomData)
49 }
50}
51
52impl<T> Debug for U32A<T> where T: LiveAtomicU32Enum + Debug{
53 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
54 self.get().fmt(f)
55 }
56}
57
58impl<T> LiveAtomic for U32A<T> where T: LiveApply + LiveNew + 'static + LiveAtomicU32Enum {
59 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
60 let mut value = T::new(cx);
61 let index = value.apply(cx, apply_from, index, nodes);
62 self.set(value);
63 index
64 }
65}
66
67impl<T> LiveHook for U32A<T> where T: LiveApply + LiveNew + 'static + LiveAtomicU32Enum {}
68impl<T> LiveApply for U32A<T> where T: LiveApply + LiveNew + 'static + LiveAtomicU32Enum {
69 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
70 self.apply_atomic(cx, from, index, nodes)
71 }
72}
73
74impl<T> LiveNew for U32A<T> where T: LiveApply + LiveNew + 'static + LiveAtomicU32Enum {
75 fn new(cx: &mut Cx) -> Self {
76 Self (AtomicU32::new(T::new(cx).as_u32()), PhantomData)
77 }
78
79 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
80 T::live_type_info(_cx)
81 }
82}
83
84impl<T> LiveRead for U32A<T> where T:LiveRead + LiveAtomicU32Enum{
85 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
86 self.get().live_read_to(id, out);
87 }
88}
89impl<T> LiveHook for Arc<T> where T: LiveApply + LiveNew + 'static + LiveAtomic {}
102impl<T> LiveApply for Arc<T> where T: LiveApply + LiveNew + 'static + LiveAtomic {
103 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
104 self.apply_atomic(cx, from, index, nodes)
105 }
106}
107
108impl<T> LiveNew for Arc<T> where T: LiveApply + LiveNew + 'static + LiveAtomic {
109 fn new(cx: &mut Cx) -> Self {
110 Arc::new(T::new(cx))
111 }
112
113 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
114 T::live_type_info(_cx)
115 }
116}
117
118impl<T> LiveRead for Arc<T> where T:LiveRead{
119 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
120 (self as &T).live_read_to(id, out);
121 }
122}
123
124pub trait AtomicGetSet<T> {
125 fn get(&self) -> T;
126 fn set(&self, val: T);
127}
128
129
130
131pub struct f32a(AtomicU32);
135
136impl Clone for f32a {
137 fn clone(&self)->Self{
138 f32a(AtomicU32::new(self.get().to_bits()))
139 }
140}
141
142impl AtomicGetSet<f32> for f32a {
143 fn get(&self) -> f32 {
144 f32::from_bits(self.0.load(Ordering::Relaxed))
145 }
146 fn set(&self, val: f32) {
147 self.0.store(val.to_bits(), Ordering::Relaxed);
148 }
149}
150
151impl LiveAtomic for f32a {
152 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
153 let mut val = 0.0f32;
154 let index = val.apply(cx, apply_from, index, nodes);
155 self.set(val);
156 index
157 }
158}
159
160impl LiveHook for f32a {}
161impl LiveApply for f32a {
162 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
163 self.apply_atomic(cx, from, index, nodes)
164 }
165}
166
167impl Debug for f32a{
168 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
169 self.get().fmt(f)
170 }
171}
172
173impl LiveRead for f32a{
174 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
175 self.get().live_read_to(id, out);
176 }
177}
178
179impl Into<f32a> for f32 {
180 fn into(self) -> f32a {
181 f32a(AtomicU32::new(self.to_bits()))
182 }
183}
184
185impl LiveNew for f32a {
186 fn new(_cx: &mut Cx) -> Self {
187 Self (AtomicU32::new(0.0f32.to_bits()))
188 }
189
190 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
191 f32::live_type_info(_cx)
192 }
193}
194
195
196
197pub struct u32a(AtomicU32);
201
202impl Clone for u32a {
203 fn clone(&self)->Self{
204 u32a(AtomicU32::new(self.get()))
205 }
206}
207
208
209impl AtomicGetSet<u32> for u32a {
210 fn get(&self) -> u32 {
211 self.0.load(Ordering::Relaxed)
212 }
213 fn set(&self, val: u32) {
214 self.0.store(val, Ordering::Relaxed);
215 }
216}
217
218impl LiveAtomic for u32a {
219 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
220 let mut val = 0u32;
221 let index = val.apply(cx, apply_from, index, nodes);
222 self.0.store(val, Ordering::Relaxed);
223 index
224 }
225}
226
227impl LiveHook for u32a {}
228impl LiveApply for u32a {
229 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
230 self.apply_atomic(cx, from, index, nodes)
231 }
232}
233
234impl Debug for u32a{
235 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
236 self.get().fmt(f)
237 }
238}
239
240impl Into<u32a> for u32 {
241 fn into(self) -> u32a {
242 u32a(AtomicU32::new(self))
243 }
244}
245
246impl LiveNew for u32a {
247 fn new(_cx: &mut Cx) -> Self {
248 Self (AtomicU32::new(0))
249 }
250
251 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
252 u32::live_type_info(_cx)
253 }
254}
255
256impl LiveRead for u32a{
257 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
258 self.get().live_read_to(id, out);
259 }
260}
261
262pub struct i64a(AtomicI64);
266
267impl Clone for i64a {
268 fn clone(&self)->Self{
269 i64a(AtomicI64::new(self.get()))
270 }
271}
272
273
274impl AtomicGetSet<i64> for i64a {
275 fn get(&self) -> i64 {
276 self.0.load(Ordering::Relaxed)
277 }
278 fn set(&self, val: i64) {
279 self.0.store(val, Ordering::Relaxed);
280 }
281}
282
283impl LiveAtomic for i64a {
284 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
285 let mut val = 0i64;
286 let index = val.apply(cx, apply_from, index, nodes);
287 self.0.store(val, Ordering::Relaxed);
288 index
289 }
290}
291
292impl<T, const N:usize> LiveAtomic for [T;N] where T: LiveAtomic {
293 fn apply_atomic(&self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
294 if nodes[index].is_array(){
296 let mut index = index + 1;
297 let mut count = 0;
298 loop{
299 if nodes[index].is_close(){
300 index += 1;
301 break;
302 }
303 if count < self.len(){
304 index = self[count].apply_atomic(cx, from, index, nodes);
305 count += 1;
306 }
307 else{
308 index = nodes.skip_node(index)
309 }
310 }
311 index
312 }
313 else{
314 cx.apply_error_expected_array(live_error_origin!(), index, nodes);
315 nodes.skip_node(index)
316 }
317 }
318}
319
320
321impl LiveHook for i64a {}
322impl LiveApply for i64a {
323 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
324 self.apply_atomic(cx, from, index, nodes)
325 }
326}
327
328impl Debug for i64a{
329 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
330 self.get().fmt(f)
331 }
332}
333
334impl Into<i64a> for i64 {
335 fn into(self) -> i64a {
336 i64a(AtomicI64::new(self))
337 }
338}
339
340impl LiveNew for i64a {
341 fn new(_cx: &mut Cx) -> Self {
342 Self (AtomicI64::new(0))
343 }
344
345 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
346 u32::live_type_info(_cx)
347 }
348}
349
350impl LiveRead for i64a{
351 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
352 self.get().live_read_to(id, out);
353 }
354}
355
356
357
358
359pub struct i32a(AtomicI32);
363
364impl Clone for i32a {
365 fn clone(&self)->Self{
366 i32a(AtomicI32::new(self.get()))
367 }
368}
369
370
371impl AtomicGetSet<i32> for i32a {
372 fn get(&self) -> i32 {
373 self.0.load(Ordering::Relaxed)
374 }
375 fn set(&self, val: i32) {
376 self.0.store(val, Ordering::Relaxed);
377 }
378}
379
380impl LiveAtomic for i32a {
381 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
382 let mut val = 0i32;
383 let index = val.apply(cx, apply_from, index, nodes);
384 self.0.store(val, Ordering::Relaxed);
385 index
386 }
387}
388
389impl LiveHook for i32a {}
390impl LiveApply for i32a {
391 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
392 self.apply_atomic(cx, from, index, nodes)
393 }
394}
395
396impl Debug for i32a{
397 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
398 self.get().fmt(f)
399 }
400}
401
402impl Into<i32a> for i32 {
403 fn into(self) -> i32a {
404 i32a(AtomicI32::new(self))
405 }
406}
407
408impl LiveNew for i32a {
409 fn new(_cx: &mut Cx) -> Self {
410 Self (AtomicI32::new(0))
411 }
412
413 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
414 u32::live_type_info(_cx)
415 }
416}
417
418impl LiveRead for i32a{
419 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
420 self.get().live_read_to(id, out);
421 }
422}
423
424
425
426
427
428
429pub struct boola(AtomicBool);
433
434impl Clone for boola {
435 fn clone(&self)->Self{
436 boola(AtomicBool::new(self.get()))
437 }
438}
439
440
441impl AtomicGetSet<bool> for boola {
442 fn get(&self) -> bool {
443 self.0.load(Ordering::Relaxed)
444 }
445 fn set(&self, val: bool) {
446 self.0.store(val, Ordering::Relaxed);
447 }
448}
449
450impl LiveAtomic for boola {
451 fn apply_atomic(&self, cx: &mut Cx, apply_from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
452 let mut val = false;
453 let index = val.apply(cx, apply_from, index, nodes);
454 self.0.store(val, Ordering::Relaxed);
455 index
456 }
457}
458
459impl LiveHook for boola {}
460impl LiveApply for boola {
461 fn apply(&mut self, cx: &mut Cx, from: ApplyFrom, index: usize, nodes: &[LiveNode]) -> usize {
462 self.apply_atomic(cx, from, index, nodes)
463 }
464}
465
466impl Debug for boola{
467 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
468 self.get().fmt(f)
469 }
470}
471
472impl Into<boola> for bool {
473 fn into(self) -> boola {
474 boola(AtomicBool::new(self))
475 }
476}
477
478impl LiveNew for boola {
479 fn new(_cx: &mut Cx) -> Self {
480 Self (AtomicBool::new(false))
481 }
482
483 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
484 bool::live_type_info(_cx)
485 }
486}
487
488impl LiveRead for boola{
489 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
490 self.get().live_read_to(id, out);
491 }
492}