-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMutableDargonNodeImpl.cs
58 lines (45 loc) · 2.14 KB
/
MutableDargonNodeImpl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using ItzWarty;
using ItzWarty.Comparers;
using System;
using System.Collections.Generic;
namespace Dargon.IO {
public class MutableDargonNodeImpl : WritableDargonNode {
private readonly IDictionary<string, WritableDargonNode> childrenByName = new Dictionary<string, WritableDargonNode>(new CaseInsensitiveStringEqualityComparer());
private readonly Dictionary<Type, object> componentsByType = new Dictionary<Type, object>();
private WritableDargonNode parent;
public MutableDargonNodeImpl(string name) {
this.Name = name;
}
public string Name { get; set; }
ReadableDargonNode ReadableDargonNode.Parent => parent;
public WritableDargonNode Parent { get { return parent; } set { SetParent(value); } }
IReadOnlyCollection<ReadableDargonNode> ReadableDargonNode.Children => Children;
public IReadOnlyCollection<WritableDargonNode> Children => (IReadOnlyCollection<WritableDargonNode>)childrenByName.Values;
public void AddComponent<TComponent>(TComponent instance) => componentsByType.Add(typeof(TComponent), instance);
public virtual T GetComponentOrNull<T>() => (T)componentsByType.GetValueOrDefault(typeof(T));
public bool AddChild(WritableDargonNode node) {
if (childrenByName.ContainsKey(node.Name)) {
return false;
} else {
childrenByName.Add(node.Name, node);
node.Parent = this;
return true;
}
}
public bool RemoveChild(WritableDargonNode node) => childrenByName.Remove(node.Name.PairValue(node));
public bool TryGetChild(string name, out WritableDargonNode child) => childrenByName.TryGetValue(name, out child);
public bool TryGetChild(string name, out ReadableDargonNode child) {
WritableDargonNode node;
var succeeded = TryGetChild(name, out node);
child = node;
return succeeded;
}
private void SetParent(WritableDargonNode newParent) {
var oldParent = parent;
if (newParent == oldParent) return;
parent = newParent;
oldParent?.RemoveChild(this);
newParent?.AddChild(this);
}
}
}