forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyBag.cs
More file actions
348 lines (291 loc) · 13.3 KB
/
PropertyBag.cs
File metadata and controls
348 lines (291 loc) · 13.3 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
/// <summary>
/// Represents a scriptable collection of named properties.
/// </summary>
/// <remarks>
/// If an object that implements this interface is added to a script engine (see
/// <see cref="ScriptEngine.AddHostObject(string, object)">AddHostObject</see>), script code
/// will be able to access the properties stored in the collection as if they were members of
/// the object itself, using the script language's native syntax for member access. No
/// other members of the object will be accessible. This interface also allows objects to
/// implement dynamic properties for script languages that support them.
/// </remarks>
public interface IPropertyBag : IDictionary<string, object>
{
}
/// <summary>
/// Provides a default <see cref="IPropertyBag"/> implementation.
/// </summary>
public class PropertyBag : IPropertyBag, INotifyPropertyChanged, IScriptableObject
{
#region data
private readonly Dictionary<string, object> dictionary;
private readonly ICollection<KeyValuePair<string, object>> collection;
private readonly bool isReadOnly;
private readonly ConcurrentWeakSet<ScriptEngine> engineSet = new ConcurrentWeakSet<ScriptEngine>();
#endregion
#region constructors
/// <summary>
/// Initializes a new writable <see cref="PropertyBag"/> with the default property name comparer.
/// </summary>
public PropertyBag()
: this(false)
{
}
/// <summary>
/// Initializes a new <see cref="PropertyBag"/> with the default property name comparer.
/// </summary>
/// <param name="isReadOnly"><c>True</c> to make the <see cref="PropertyBag"/> read-only, <c>false</c> to make it writable.</param>
/// <remarks>
/// The host can modify a read-only <see cref="PropertyBag"/> by calling
/// <see cref="SetPropertyNoCheck">SetPropertyNoCheck</see>,
/// <see cref="RemovePropertyNoCheck">RemovePropertyNoCheck</see>, or
/// <see cref="ClearNoCheck">ClearNoCheck</see>.
/// </remarks>
public PropertyBag(bool isReadOnly)
: this(isReadOnly, null)
{
}
/// <summary>
/// Initializes a new writable <see cref="PropertyBag"/>.
/// </summary>
/// <param name="comparer">The comparer to use for property names, or <c>null</c> to use the default string comparer.</param>
public PropertyBag(IEqualityComparer<string> comparer)
: this(false, comparer)
{
}
/// <summary>
/// Initializes a new <see cref="PropertyBag"/>.
/// </summary>
/// <param name="isReadOnly"><c>True</c> to make the <see cref="PropertyBag"/> read-only, <c>false</c> to make it writable.</param>
/// <param name="comparer">The comparer to use for property names, or <c>null</c> to use the default string comparer.</param>
public PropertyBag(bool isReadOnly, IEqualityComparer<string> comparer)
{
dictionary = new Dictionary<string, object>(comparer);
collection = dictionary;
this.isReadOnly = isReadOnly;
}
#endregion
#region public members
/// <summary>
/// Gets the property name comparer for the <see cref="PropertyBag"/>.
/// </summary>
public IEqualityComparer<string> Comparer => dictionary.Comparer;
/// <summary>
/// Sets a property value without checking whether the <see cref="PropertyBag"/> is read-only.
/// </summary>
/// <param name="name">The name of the property to set.</param>
/// <param name="value">The property value.</param>
/// <remarks>
/// This operation is never exposed to script code.
/// </remarks>
public void SetPropertyNoCheck(string name, object value)
{
dictionary[name] = value;
NotifyPropertyChanged(name);
NotifyExposedToScriptCode(value);
}
/// <summary>
/// Removes a property without checking whether the <see cref="PropertyBag"/> is read-only.
/// </summary>
/// <param name="name">The name of the property to remove.</param>
/// <returns><c>True</c> if the property was found and removed, <c>false</c> otherwise.</returns>
/// <remarks>
/// This operation is never exposed to script code.
/// </remarks>
public bool RemovePropertyNoCheck(string name)
{
if (dictionary.Remove(name))
{
NotifyPropertyChanged(name);
return true;
}
return false;
}
/// <summary>
/// Removes all properties without checking whether the <see cref="PropertyBag"/> is read-only.
/// </summary>
/// <remarks>
/// This operation is never exposed to script code.
/// </remarks>
public void ClearNoCheck()
{
dictionary.Clear();
NotifyPropertyChanged(null);
}
#endregion
#region internal members
internal int EngineCount => engineSet.Count;
private void CheckReadOnly()
{
if (isReadOnly)
{
throw new UnauthorizedAccessException("The object is read-only");
}
}
private void AddPropertyNoCheck(string name, object value)
{
dictionary.Add(name, value);
NotifyPropertyChanged(name);
NotifyExposedToScriptCode(value);
}
private void NotifyPropertyChanged(string name)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(name));
}
private void NotifyExposedToScriptCode(object value)
{
if (value is IScriptableObject scriptableObject)
{
engineSet.ForEach(scriptableObject.OnExposedToScriptCode);
}
}
#endregion
#region IEnumerable implementation
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member requires explicit implementation to resolve ambiguity.")]
IEnumerator IEnumerable.GetEnumerator()
{
return dictionary.GetEnumerator();
}
#endregion
#region IEnumerable<KeyValuePair<string, object>> implementation
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member requires explicit implementation to resolve ambiguity.")]
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
return dictionary.GetEnumerator();
}
#endregion
#region ICollection<KeyValuePair<string, object>> implementation
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
{
CheckReadOnly();
SetPropertyNoCheck(item.Key, item.Value);
}
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
void ICollection<KeyValuePair<string, object>>.Clear()
{
CheckReadOnly();
ClearNoCheck();
}
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
{
return collection.Contains(item);
}
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
collection.CopyTo(array, arrayIndex);
}
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
{
CheckReadOnly();
if (collection.Remove(item))
{
NotifyPropertyChanged(item.Key);
return true;
}
return false;
}
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
int ICollection<KeyValuePair<string, object>>.Count => collection.Count;
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
bool ICollection<KeyValuePair<string, object>>.IsReadOnly => isReadOnly;
#endregion
#region IDictionary<string, object> implementation
/// <summary>
/// Determines whether the <see cref="PropertyBag"/> contains a property with the specified name.
/// </summary>
/// <param name="key">The name of the property to locate.</param>
/// <returns><c>True</c> if the <see cref="PropertyBag"/> contains a property with the specified name, <c>false</c> otherwise.</returns>
public bool ContainsKey(string key)
{
return dictionary.ContainsKey(key);
}
/// <summary>
/// Adds a property to the <see cref="PropertyBag"/>.
/// </summary>
/// <param name="key">The name of the property to add.</param>
/// <param name="value">The property value.</param>
public void Add(string key, object value)
{
CheckReadOnly();
AddPropertyNoCheck(key, value);
}
/// <summary>
/// Removes a property from the <see cref="PropertyBag"/>.
/// </summary>
/// <param name="key">The name of the property to remove.</param>
/// <returns><c>True</c> if the property was successfully found and removed, <c>false</c> otherwise.</returns>
public bool Remove(string key)
{
CheckReadOnly();
return RemovePropertyNoCheck(key);
}
/// <summary>
/// Looks up a property value in the <see cref="PropertyBag"/>.
/// </summary>
/// <param name="key">The name of the property to locate.</param>
/// <param name="value">The property value if the property was found, <c>null</c> otherwise.</param>
/// <returns><c>True</c> if the property was found, <c>false</c> otherwise.</returns>
public bool TryGetValue(string key, out object value)
{
return dictionary.TryGetValue(key, out value);
}
/// <summary>
/// Gets or sets a property value in the <see cref="PropertyBag"/>.
/// </summary>
/// <param name="key">The name of the property to get or set.</param>
/// <returns>The property value.</returns>
public object this[string key]
{
get => dictionary[key];
set
{
CheckReadOnly();
SetPropertyNoCheck(key, value);
}
}
/// <summary>
/// Gets a collection of property names from the <see cref="PropertyBag"/>.
/// </summary>
public ICollection<string> Keys => dictionary.Keys;
/// <summary>
/// Gets a collection of property values from the <see cref="PropertyBag"/>.
/// </summary>
public ICollection<object> Values => dictionary.Values;
#endregion
#region INotifyPropertyChanged implementation
/// <summary>
/// Occurs when a property is added or replaced, or when the collection is cleared.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region IScriptableObject implementation
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "This member is not expected to be re-implemented in derived classes.")]
void IScriptableObject.OnExposedToScriptCode(ScriptEngine engine)
{
if ((engine != null) && engineSet.TryAdd(engine))
{
foreach (var scriptableObject in Values.OfType<IScriptableObject>())
{
scriptableObject.OnExposedToScriptCode(engine);
}
}
}
#endregion
}
}