1use std::{
47 any::Any,
48 sync::{Arc, RwLock, Weak},
49 hash::Hash, fmt::{Debug, Display}, error::Error,
50};
51
52use by_address::ByAddress;
53
54type Component = Arc<dyn Any + Send + Sync>;
55
56#[doc(hidden)]
57pub use oop_inheritance_proc::class_extends;
58
59pub use oop_inheritance_proc::class;
60
61pub mod util;
62
63use self::util::VectorExtensions;
64
65fn default<T: Default>() -> T {
66 T::default()
67}
68
69pub struct Node {
71 inner: Arc<NodeInner>,
72}
73
74impl Debug for Node {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 write!(f, "Node")
77 }
78}
79
80impl Hash for Node {
81 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
83 ByAddress(Arc::clone(&self.inner)).hash(state)
84 }
85}
86
87impl PartialEq for Node {
88 fn eq(&self, other: &Self) -> bool {
91 Arc::ptr_eq(&self.inner, &other.inner)
92 }
93}
94
95impl Eq for Node {}
96
97impl Clone for Node {
98 fn clone(&self) -> Self {
101 Self { inner: Arc::clone(&self.inner) }
102 }
103}
104
105impl AsRef<Node> for Node {
106 fn as_ref(&self) -> &Node {
107 self
108 }
109}
110
111impl Node {
112 pub fn new() -> Node {
113 Self {
114 inner: Arc::new(NodeInner {
115 name: RwLock::new(None),
116 parent: RwLock::new(default()),
117 components: RwLock::new(vec![]),
118 children: RwLock::new(vec![]),
119 })
120 }
121 }
122
123 pub fn downgrade_ref(&self) -> WeakNodeRef {
125 WeakNodeRef(Arc::downgrade(&self.inner))
126 }
127
128 pub fn has<T>(&self) -> bool
130 where T: Any + Send + Sync
131 {
132 self.get::<T>().is_some()
133 }
134
135 pub fn get<T>(&self) -> Option<Arc<T>>
137 where T: Any + Send + Sync
138 {
139 for component in self.inner.components.read().unwrap().iter() {
140 if let Ok(c) = Arc::downcast::<T>(Arc::clone(component)) {
141 return Some(c);
142 }
143 }
144 None
145 }
146
147 pub fn set<T>(&self, value: T) -> Self
149 where T: Any + Send + Sync
150 {
151 self.delete::<T>();
152 self.inner.components.write().unwrap().push(Arc::new(value));
153 self.clone()
154 }
155
156 pub fn delete<T>(&self) -> bool
160 where T: Any + Send + Sync
161 {
162 let mut i = 0;
163 let mut components = vec![];
164 for component in self.inner.components.read().unwrap().iter() {
165 components.push(Arc::clone(component));
166 }
167 for component in components {
168 if Arc::downcast::<T>(Arc::clone(&component)).is_ok() {
169 self.inner.components.write().unwrap().remove(i);
170 return true;
171 }
172 i += 1;
173 }
174 false
175 }
176
177 pub fn parent(&self) -> Option<Node> {
178 self.inner.parent.read().unwrap().upgrade()
179 }
180
181 pub fn children(&self) -> Vec<Node> {
182 let mut c = vec![];
183 for child in self.inner.children.read().unwrap().iter() {
184 c.push(child.clone());
185 }
186 c
187 }
188
189 pub fn child_at(&self, index: usize) -> Option<Node> {
190 if index < self.num_children() { Some(self.inner.children.read().unwrap()[index].clone()) } else { None }
191 }
192
193 pub fn num_children(&self) -> usize {
195 self.inner.children.read().unwrap().len()
196 }
197
198 fn is_child_of(&self, child: &Node) -> bool {
199 if let Some(p) = self.parent() {
200 if &p == child {
201 return true;
202 }
203 }
204 for i in 0..child.num_children() {
205 let child = child.child_at(i).unwrap();
206 if self.is_child_of(&child) {
207 return true;
208 }
209 }
210 false
211 }
212
213 pub fn add_child(&self, child: impl AsRef<Node>) {
217 let child = child.as_ref();
218 child.remove_from_parent();
219
220 assert!(!self.is_child_of(child), "Adding circular child.");
222
223 *child.inner.parent.write().unwrap() = self.downgrade_ref();
224 self.inner.children.write().unwrap().push(child.clone());
225 }
226
227 pub fn add_child_at(&self, index: usize, child: impl AsRef<Node>) {
235 let child = child.as_ref();
236 child.remove_from_parent();
237 assert!(index < self.num_children(), "Specified index is out of bounds.");
238
239 assert!(!self.is_child_of(child), "Adding circular child.");
241
242 *child.inner.parent.write().unwrap() = self.downgrade_ref();
243 self.inner.children.write().unwrap().insert(index, child.clone());
244 }
245
246 pub fn add_children(&self, children: impl IntoIterator<Item = impl AsRef<Node>>) {
250 for child in children.into_iter() {
251 self.add_child(child.as_ref());
252 }
253 }
254
255 pub fn swap_children(&self, child_1: impl AsRef<Node>, child_2: impl AsRef<Node>) {
261 let child_1 = child_1.as_ref();
262 let child_2 = child_2.as_ref();
263 let indices = [self.inner.children.read().unwrap().index_of(child_1), self.inner.children.read().unwrap().index_of(child_2)];
264 assert!(indices.iter().all(|i| i.is_some()), "Some of the specified indices are out of bounds.");
265 self.inner.children.write().unwrap().swap(indices[0].unwrap(), indices[1].unwrap());
266 }
267
268 pub fn swap_children_by_indices(&self, child_1: usize, child_2: usize) {
274 assert!([child_1, child_2].iter().all(|&i| i < self.num_children()), "Some of the specified indices are out of bounds.");
275 self.inner.children.write().unwrap().swap(child_1, child_2);
276 }
277
278 pub fn remove_child(&self, child: impl AsRef<Node>) -> bool {
280 let child = child.as_ref();
281 let i = self.inner.children.read().unwrap().index_of(child);
282 if let Some(i) = i {
283 self.inner.children.write().unwrap().remove(i);
284 *child.inner.parent.write().unwrap() = default();
285 true
286 } else {
287 false
288 }
289 }
290
291 pub fn remove_children(&self) {
293 for child in self.children() {
294 *child.inner.parent.write().unwrap() = default();
295 }
296 self.inner.children.write().unwrap().clear();
297 }
298
299 pub fn remove_from_parent(&self) -> bool {
301 if let Some(p) = self.parent() { p.remove_child(self) } else { false }
302 }
303
304 pub fn name(&self) -> Option<String> {
306 self.inner.name.read().unwrap().clone()
307 }
308
309 pub fn set_name(&self, name: Option<String>) {
311 *self.inner.name.write().unwrap() = name;
312 }
313
314 pub fn resolve_path(&self, path: &str) -> Option<Node> {
327 let segments = path.split('/');
328 let mut r: Option<Node> = Some(self.clone());
329 for s in segments {
330 if r.is_none() {
331 break;
332 }
333 match s {
334 ".first" => {
335 r = r.unwrap().children().first().map(|c| c.clone());
336 },
337 ".last" => {
338 r = r.unwrap().children().last().map(|c| c.clone());
339 },
340 ".." => {
341 r = r.unwrap().parent();
342 },
343 "" => {
344 },
346 _ => {
347 r = r.unwrap().children().iter().find(|c| c.name().as_ref().map(|cn| cn.as_ref()) == Some(s)).map(|c| c.clone());
348 },
349 }
350 }
351 r
352 }
353
354 pub fn is<T: TryFrom<Self, Error = ClassError>>(&self) -> bool {
356 T::try_from(self.clone()).is_ok()
357 }
358
359 pub fn to<T: TryFrom<Self, Error = ClassError>>(&self) -> Result<T, ClassError> {
361 T::try_from(self.clone())
362 }
363}
364
365struct NodeInner {
366 name: RwLock<Option<String>>,
367 parent: RwLock<WeakNodeRef>,
368 components: RwLock<Vec<Component>>,
369 children: RwLock<Vec<Node>>,
370}
371
372pub struct WeakNodeRef(Weak<NodeInner>);
374
375impl WeakNodeRef {
376 pub fn empty() -> Self {
379 Self(Weak::new())
380 }
381
382 pub fn upgrade(&self) -> Option<Node> {
384 if let Some(r) = self.0.upgrade() { Some(Node { inner: r }) } else { None }
385 }
386}
387
388impl Debug for WeakNodeRef {
389 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
390 write!(f, "WeakNodeRef")
391 }
392}
393
394impl Default for WeakNodeRef {
395 fn default() -> Self {
396 Self::empty()
397 }
398}
399
400impl PartialEq for WeakNodeRef {
401 fn eq(&self, other: &Self) -> bool {
403 Weak::ptr_eq(&self.0, &other.0)
404 }
405}
406
407impl Eq for WeakNodeRef {}
408
409impl Clone for WeakNodeRef {
410 fn clone(&self) -> Self {
411 Self(self.0.clone())
412 }
413}
414
415pub struct ClassError {
418 message: String,
419}
420
421impl ClassError {
422 pub fn new(message: &str) -> Self {
423 Self { message: message.into() }
424 }
425}
426
427impl Display for ClassError {
428 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
429 write!(f, "{}", self.message)
430 }
431}
432
433impl Debug for ClassError {
434 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
435 <Self as Display>::fmt(self, f)
436 }
437}
438
439impl Error for ClassError {}
440
441#[cfg(test)]
442mod tests {
443 use crate::*;
444
445 #[test]
446 fn test_components() {
447 let node = Node::new();
448 node.set(10.0);
449 assert_eq!(10.0, *node.get::<f64>().unwrap());
450
451 node.delete::<f64>();
452 assert!(node.get::<f64>().is_none());
453 }
454
455 #[test]
456 fn test_hierarchy() {
457 let topmost = Node::new();
458 let child_1 = Node::new();
459 child_1.set_name(Some("child1".into()));
460 topmost.add_child(&child_1);
461 assert_eq!("child1".to_owned(), topmost.resolve_path(".last").unwrap().name().unwrap());
462 assert_eq!(topmost.resolve_path(".last").unwrap(), child_1);
463 }
464
465 #[test]
466 fn test_class_extends() {
467 struct A(Node);
468
469 class_extends!(A < Node, use AComponent, crate);
470
471 impl A {
472 fn new() -> Self {
473 Self(Node::new().set(AComponent))
474 }
475 }
476
477 struct AComponent;
478
479 struct B(A);
480
481 class_extends!(B < A < Node, use BComponent, crate);
482
483 impl B {
484 fn new() -> Self {
485 Self(A::new().set(BComponent).try_into().unwrap())
486 }
487 }
488
489 struct BComponent;
490
491 let r = B::new();
492 let r_e: Node = r.clone().into();
493 let _: A = r.into();
494 assert!(r_e.is::<B>());
495
496 let r = Node::new();
497 assert!(!r.is::<A>());
498 }
499
500 #[test]
501 fn test_class() {
502 class! {
503 use oop_inheritance = crate;
504 struct A: Node {
505 x: f64 = 0.0,
506 }
507 fn constructor(x: f64) {
508 super();
509 this.set_x(x);
510 }
511 }
512
513 let o = A::new(10.0);
514 assert_eq!(o.x(), 10.0);
515
516 class! {
517 use oop_inheritance = crate;
518 struct B: A < Node {
519 y: A = A::new(15.0),
520 ref z: f64 = 0.0,
521 }
522 fn constructor() {
523 super(0.0);
524 }
525 }
526
527 let o = B::new();
528 assert_eq!(o.y().x(), 15.0);
529 }
530}