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, AtomicU64, 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: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize;
18}
19
20pub trait LiveAtomic {
21 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, 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: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
60 let mut value = T::new(cx);
61 let index = value.apply(cx, apply, 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, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
70 self.apply_atomic(cx, apply, 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, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
104 self.apply_atomic(cx, apply, 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: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
153 let mut val = 0.0f32;
154 let index = val.apply(cx, apply, 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, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
163 self.apply_atomic(cx, apply, 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
196pub struct f64a(AtomicU64);
200
201impl Clone for f64a {
202 fn clone(&self)->Self{
203 f64a(AtomicU64::new(self.get().to_bits()))
204 }
205}
206
207impl AtomicGetSet<f64> for f64a {
208 fn get(&self) -> f64 {
209 f64::from_bits(self.0.load(Ordering::Relaxed))
210 }
211 fn set(&self, val: f64) {
212 self.0.store(val.to_bits(), Ordering::Relaxed);
213 }
214}
215
216impl LiveAtomic for f64a {
217 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
218 let mut val = 0.0f64;
219 let index = val.apply(cx, apply, index, nodes);
220 self.set(val);
221 index
222 }
223}
224
225impl LiveHook for f64a {}
226impl LiveApply for f64a {
227 fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
228 self.apply_atomic(cx, apply, index, nodes)
229 }
230}
231
232impl Debug for f64a{
233 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
234 self.get().fmt(f)
235 }
236}
237
238impl LiveRead for f64a{
239 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
240 self.get().live_read_to(id, out);
241 }
242}
243
244impl Into<f64a> for f64 {
245 fn into(self) -> f64a {
246 f64a(AtomicU64::new(self.to_bits()))
247 }
248}
249
250impl LiveNew for f64a {
251 fn new(_cx: &mut Cx) -> Self {
252 Self (AtomicU64::new(0.0f64.to_bits()))
253 }
254
255 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
256 f32::live_type_info(_cx)
257 }
258}
259
260
261
262pub struct u32a(AtomicU32);
266
267impl Clone for u32a {
268 fn clone(&self)->Self{
269 u32a(AtomicU32::new(self.get()))
270 }
271}
272
273
274impl AtomicGetSet<u32> for u32a {
275 fn get(&self) -> u32 {
276 self.0.load(Ordering::Relaxed)
277 }
278 fn set(&self, val: u32) {
279 self.0.store(val, Ordering::Relaxed);
280 }
281}
282
283impl LiveAtomic for u32a {
284 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
285 let mut val = 0u32;
286 let index = val.apply(cx, apply, index, nodes);
287 self.0.store(val, Ordering::Relaxed);
288 index
289 }
290}
291
292impl LiveHook for u32a {}
293impl LiveApply for u32a {
294 fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
295 self.apply_atomic(cx, apply, index, nodes)
296 }
297}
298
299impl Debug for u32a{
300 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
301 self.get().fmt(f)
302 }
303}
304
305impl Into<u32a> for u32 {
306 fn into(self) -> u32a {
307 u32a(AtomicU32::new(self))
308 }
309}
310
311impl LiveNew for u32a {
312 fn new(_cx: &mut Cx) -> Self {
313 Self (AtomicU32::new(0))
314 }
315
316 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
317 u32::live_type_info(_cx)
318 }
319}
320
321impl LiveRead for u32a{
322 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
323 self.get().live_read_to(id, out);
324 }
325}
326
327pub struct i64a(AtomicI64);
331
332impl Clone for i64a {
333 fn clone(&self)->Self{
334 i64a(AtomicI64::new(self.get()))
335 }
336}
337
338
339impl AtomicGetSet<i64> for i64a {
340 fn get(&self) -> i64 {
341 self.0.load(Ordering::Relaxed)
342 }
343 fn set(&self, val: i64) {
344 self.0.store(val, Ordering::Relaxed);
345 }
346}
347
348impl LiveAtomic for i64a {
349 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
350 let mut val = 0i64;
351 let index = val.apply(cx, apply, index, nodes);
352 self.0.store(val, Ordering::Relaxed);
353 index
354 }
355}
356
357impl<T, const N:usize> LiveAtomic for [T;N] where T: LiveAtomic {
358 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
359 if nodes[index].is_array(){
361 let mut index = index + 1;
362 let mut count = 0;
363 loop{
364 if nodes[index].is_close(){
365 index += 1;
366 break;
367 }
368 if count < self.len(){
369 index = self[count].apply_atomic(cx, apply, index, nodes);
370 count += 1;
371 }
372 else{
373 index = nodes.skip_node(index)
374 }
375 }
376 index
377 }
378 else{
379 cx.apply_error_expected_array(live_error_origin!(), index, nodes);
380 nodes.skip_node(index)
381 }
382 }
383}
384
385
386impl LiveHook for i64a {}
387impl LiveApply for i64a {
388 fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
389 self.apply_atomic(cx, apply, index, nodes)
390 }
391}
392
393impl Debug for i64a{
394 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
395 self.get().fmt(f)
396 }
397}
398
399impl Into<i64a> for i64 {
400 fn into(self) -> i64a {
401 i64a(AtomicI64::new(self))
402 }
403}
404
405impl LiveNew for i64a {
406 fn new(_cx: &mut Cx) -> Self {
407 Self (AtomicI64::new(0))
408 }
409
410 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
411 u32::live_type_info(_cx)
412 }
413}
414
415impl LiveRead for i64a{
416 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
417 self.get().live_read_to(id, out);
418 }
419}
420
421
422
423
424pub struct i32a(AtomicI32);
428
429impl Clone for i32a {
430 fn clone(&self)->Self{
431 i32a(AtomicI32::new(self.get()))
432 }
433}
434
435
436impl AtomicGetSet<i32> for i32a {
437 fn get(&self) -> i32 {
438 self.0.load(Ordering::Relaxed)
439 }
440 fn set(&self, val: i32) {
441 self.0.store(val, Ordering::Relaxed);
442 }
443}
444
445impl LiveAtomic for i32a {
446 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
447 let mut val = 0i32;
448 let index = val.apply(cx, apply, index, nodes);
449 self.0.store(val, Ordering::Relaxed);
450 index
451 }
452}
453
454impl LiveHook for i32a {}
455impl LiveApply for i32a {
456 fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
457 self.apply_atomic(cx, apply, index, nodes)
458 }
459}
460
461impl Debug for i32a{
462 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
463 self.get().fmt(f)
464 }
465}
466
467impl Into<i32a> for i32 {
468 fn into(self) -> i32a {
469 i32a(AtomicI32::new(self))
470 }
471}
472
473impl LiveNew for i32a {
474 fn new(_cx: &mut Cx) -> Self {
475 Self (AtomicI32::new(0))
476 }
477
478 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
479 u32::live_type_info(_cx)
480 }
481}
482
483impl LiveRead for i32a{
484 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
485 self.get().live_read_to(id, out);
486 }
487}
488
489
490
491
492
493
494pub struct boola(AtomicBool);
498
499impl Clone for boola {
500 fn clone(&self)->Self{
501 boola(AtomicBool::new(self.get()))
502 }
503}
504
505
506impl AtomicGetSet<bool> for boola {
507 fn get(&self) -> bool {
508 self.0.load(Ordering::Relaxed)
509 }
510 fn set(&self, val: bool) {
511 self.0.store(val, Ordering::Relaxed);
512 }
513}
514
515impl LiveAtomic for boola {
516 fn apply_atomic(&self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
517 let mut val = false;
518 let index = val.apply(cx, apply, index, nodes);
519 self.0.store(val, Ordering::Relaxed);
520 index
521 }
522}
523
524impl LiveHook for boola {}
525impl LiveApply for boola {
526 fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
527 self.apply_atomic(cx, apply, index, nodes)
528 }
529}
530
531impl Debug for boola{
532 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>{
533 self.get().fmt(f)
534 }
535}
536
537impl Into<boola> for bool {
538 fn into(self) -> boola {
539 boola(AtomicBool::new(self))
540 }
541}
542
543impl LiveNew for boola {
544 fn new(_cx: &mut Cx) -> Self {
545 Self (AtomicBool::new(false))
546 }
547
548 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
549 bool::live_type_info(_cx)
550 }
551}
552
553impl LiveRead for boola{
554 fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
555 self.get().live_read_to(id, out);
556 }
557}