forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyContainer+Path.cs
More file actions
111 lines (100 loc) · 4.65 KB
/
Copy pathPropertyContainer+Path.cs
File metadata and controls
111 lines (100 loc) · 4.65 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
namespace Unity.Properties
{
public static partial class PropertyContainer
{
class ValueAtPathVisitor : PathVisitor
{
public static readonly UnityEngine.Pool.ObjectPool<ValueAtPathVisitor> Pool = new UnityEngine.Pool.ObjectPool<ValueAtPathVisitor>(() => new ValueAtPathVisitor(), null, v => v.Reset());
public IPropertyVisitor Visitor;
public override void Reset()
{
base.Reset();
Visitor = default;
ReadonlyVisit = true;
}
protected override void VisitPath<TContainer, TValue>(Property<TContainer, TValue> property,
ref TContainer container, ref TValue value)
{
((IPropertyAccept<TContainer>) property).Accept(Visitor, ref container);
}
}
class ExistsAtPathVisitor : PathVisitor
{
public static readonly UnityEngine.Pool.ObjectPool<ExistsAtPathVisitor> Pool = new UnityEngine.Pool.ObjectPool<ExistsAtPathVisitor>(() => new ExistsAtPathVisitor(), null, v => v.Reset());
public bool Exists;
public override void Reset()
{
base.Reset();
Exists = default;
ReadonlyVisit = true;
}
protected override void VisitPath<TContainer, TValue>(Property<TContainer, TValue> property, ref TContainer container, ref TValue value)
{
Exists = true;
}
}
/// <summary>
/// Returns <see langword="true"/> if a property exists at the specified <see cref="PropertyPath"/>.
/// </summary>
/// <param name="container">The container tree to search.</param>
/// <param name="path">The property path to resolve.</param>
/// <typeparam name="TContainer">The container type.</typeparam>
/// <returns><see langword="true"/> if a property can be found at path.</returns>
public static bool IsPathValid<TContainer>(TContainer container, string path)
=> IsPathValid(ref container, new PropertyPath(path));
/// <summary>
/// Returns <see langword="true"/> if a property exists at the specified <see cref="PropertyPath"/>.
/// </summary>
/// <param name="container">The container tree to search.</param>
/// <param name="path">The property path to resolve.</param>
/// <typeparam name="TContainer">The container type.</typeparam>
/// <returns><see langword="true"/> if a property can be found at path.</returns>
public static bool IsPathValid<TContainer>(TContainer container, in PropertyPath path)
=> IsPathValid(ref container, path);
/// <summary>
/// Returns <see langword="true"/> if a property exists at the specified <see cref="PropertyPath"/>.
/// </summary>
/// <param name="container">The container tree to search.</param>
/// <param name="path">The property path to resolve.</param>
/// <typeparam name="TContainer">The container type.</typeparam>
/// <returns><see langword="true"/> if a property can be found at path.</returns>
public static bool IsPathValid<TContainer>(ref TContainer container, string path)
{
var visitor = ExistsAtPathVisitor.Pool.Get();
try
{
visitor.Path = new PropertyPath(path);
TryAccept(visitor, ref container);
return visitor.Exists;
}
finally
{
ExistsAtPathVisitor.Pool.Release(visitor);
}
}
/// <summary>
/// Returns <see langword="true"/> if a property exists at the specified <see cref="PropertyPath"/>.
/// </summary>
/// <param name="container">The container tree to search.</param>
/// <param name="path">The property path to resolve.</param>
/// <typeparam name="TContainer">The container type.</typeparam>
/// <returns><see langword="true"/> if a property can be found at path.</returns>
public static bool IsPathValid<TContainer>(ref TContainer container, in PropertyPath path)
{
var visitor = ExistsAtPathVisitor.Pool.Get();
try
{
visitor.Path = path;
TryAccept(visitor, ref container);
return visitor.Exists;
}
finally
{
ExistsAtPathVisitor.Pool.Release(visitor);
}
}
}
}