Skip to content

Commit cfc8340

Browse files
committed
more
1 parent b4adf7c commit cfc8340

4 files changed

Lines changed: 28 additions & 20 deletions

File tree

AttributeList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ public ICollection<object> Values
215215
/// <exception cref="ElementOwnershipException">Thrown when an attempt is made to set the value of the attribute to an Element from a different <see cref="Datamodel"/>.</exception>
216216
/// <exception cref="AttributeTypeException">Thrown when an attempt is made to set a value that is not of a valid Datamodel attribute type.</exception>
217217
/// <exception cref="IndexOutOfRangeException">Thrown when the maximum number of Attributes allowed in an AttributeList has been reached.</exception>
218-
public virtual object this[string name]
218+
public virtual object? this[string name]
219219
{
220220
get
221221
{
222222
ArgumentNullException.ThrowIfNull(name);
223-
var attr = (Attribute)Inner[name];
223+
var attr = (Attribute?)Inner[name];
224224
if (attr == null)
225225
{
226226
var prop_attr = (PropertyInfo)PropertyInfos[name];

Datamodel.ElementList.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal void Add(Element item)
4545
ChangeLock.EnterUpgradeableReadLock();
4646
try
4747
{
48-
Element existing = (Element)store[item.ID];
48+
Element? existing = (Element?)store[item.ID];
4949
if (existing != null && !existing.Stub)
5050
{
5151
throw new ElementIdException($"Element ID {item.ID} already in use in this Datamodel.");
@@ -82,14 +82,14 @@ internal void Add(Element item)
8282
/// <remarks>The order of this list has no meaning to a Datamodel. This accessor is intended for <see cref="ICodec"/> implementers.</remarks>
8383
/// <param name="index">The index to look up.</param>
8484
/// <returns>The Element found at the index.</returns>
85-
public Element this[int index]
85+
public Element? this[int index]
8686
{
8787
get
8888
{
8989
ChangeLock.EnterReadLock();
9090
try
9191
{
92-
return (Element)store[index];
92+
return (Element?)store[index];
9393
}
9494
finally { ChangeLock.ExitReadLock(); }
9595
}
@@ -100,14 +100,14 @@ public Element this[int index]
100100
/// </summary>
101101
/// <param name="id">The ID to search for.</param>
102102
/// <returns>The Element with the given ID, or null if none is found.</returns>
103-
public Element this[Guid id]
103+
public Element? this[Guid id]
104104
{
105105
get
106106
{
107107
ChangeLock.EnterReadLock();
108108
try
109109
{
110-
return (Element)store[id];
110+
return (Element?)store[id];
111111
}
112112
finally { ChangeLock.ExitReadLock(); }
113113
}
@@ -168,7 +168,7 @@ public bool Remove(Element item, RemoveMode mode)
168168
try
169169
{
170170
store.Remove(item.ID);
171-
Element replacement = (mode == RemoveMode.MakeStubs) ? new Element(Owner, item.ID) : (Element)null;
171+
Element? replacement = (mode == RemoveMode.MakeStubs) ? new Element(Owner, item.ID) : null;
172172

173173
foreach (Element elem in store.Values)
174174
{
@@ -179,7 +179,7 @@ public bool Remove(Element item, RemoveMode mode)
179179
elem[attr.Key] = replacement;
180180
}
181181

182-
foreach (var array in elem.Select(a => a.Value).OfType<IList<Element>>())
182+
foreach (var array in elem.Select(a => a.Value).OfType<IList<Element?>>())
183183
for (int i = 0; i < array.Count; i++)
184184
if (array[i] == item)
185185
array[i] = replacement;
@@ -210,7 +210,7 @@ public void Trim()
210210
ChangeLock.EnterUpgradeableReadLock();
211211
try
212212
{
213-
var used = new HashSet<Element>();
213+
var used = new HashSet<Element?>();
214214
WalkElemTree(Owner.Root, used);
215215
if (used.Count == Count) return;
216216

@@ -219,8 +219,11 @@ public void Trim()
219219
{
220220
foreach (var elem in this.Except(used).ToArray())
221221
{
222-
store.Remove(elem.ID);
223-
elem.Owner = null;
222+
if(elem != null)
223+
{
224+
store.Remove(elem.ID);
225+
elem.Owner = null;
226+
}
224227
}
225228

226229
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (System.Collections.IList)used));
@@ -236,8 +239,13 @@ public void Trim()
236239
}
237240
}
238241

239-
protected void WalkElemTree(Element elem, HashSet<Element> found)
242+
protected void WalkElemTree(Element? elem, HashSet<Element?> found)
240243
{
244+
if(elem is null)
245+
{
246+
return;
247+
}
248+
241249
found.Add(elem);
242250
foreach (var value in elem.Inner.Values.Cast<Attribute>().Select(a => a.RawValue))
243251
{
@@ -279,7 +287,7 @@ public IEnumerator<Element> GetEnumerator()
279287
/// <summary>
280288
/// Raised when an <see cref="Element"/> is added, removed, or replaced.
281289
/// </summary>
282-
public event NotifyCollectionChangedEventHandler CollectionChanged;
290+
public event NotifyCollectionChangedEventHandler? CollectionChanged;
283291
#endregion
284292

285293
public void Dispose()

Datamodel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public int EncodingVersion
466466
/// Gets or sets the first Element of the Datamodel. Only Elements referenced by the Root element or one of its children are considered a part of the Datamodel.
467467
/// </summary>
468468
/// <exception cref="ElementOwnershipException">Thown when an attempt is made to assign an Element from another Datamodel to this property.</exception>
469-
public Element Root
469+
public Element? Root
470470
{
471471
get => _Root;
472472
set
@@ -482,7 +482,7 @@ public Element Root
482482
OnPropertyChanged();
483483
}
484484
}
485-
Element _Root;
485+
Element? _Root;
486486

487487
public AttributeList PrefixAttributes
488488
{

Element.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ internal set
207207
/// <exception cref="KeyNotFoundException">Thrown when an attempt is made to get a name that is not present on this Element.</exception>
208208
public T? Get<T>(string name)
209209
{
210-
object value = this[name];
210+
object? value = this[name];
211211

212212
if (value is not T && !(typeof(T) == typeof(Element) && value == null))
213-
throw new AttributeTypeException(string.Format("Attribute \"{0}\" ({1}) does not implement {2}.", name, value.GetType().Name, typeof(T).Name));
213+
throw new AttributeTypeException(string.Format("Attribute \"{0}\" ({1}) does not implement {2}.", name, value?.GetType().Name, typeof(T).Name));
214214

215215
return (T?)value;
216216
}
@@ -233,7 +233,7 @@ internal set
233233
}
234234
catch (AttributeTypeException)
235235
{
236-
throw new AttributeTypeException(string.Format("Attribute \"{0}\" ({1}) is not an array.", name, this[name].GetType().Name));
236+
throw new AttributeTypeException(string.Format("Attribute \"{0}\" ({1}) is not an array.", name, this[name]?.GetType().Name));
237237
}
238238

239239
}
@@ -249,7 +249,7 @@ internal set
249249
/// <exception cref="ElementOwnershipException">Thrown when an attempt is made to set the value of the attribute to an Element from a different <see cref="Datamodel"/>.</exception>
250250
/// <exception cref="AttributeTypeException">Thrown when an attempt is made to set a value that is not of a valid Datamodel attribute type.</exception>
251251
/// <exception cref="IndexOutOfRangeException">Thrown when the maximum number of Attributes allowed in an Element has been reached.</exception>
252-
public override object this[string name]
252+
public override object? this[string name]
253253
{
254254
get
255255
{

0 commit comments

Comments
 (0)