Skip to content

Commit b4adf7c

Browse files
committed
make some files follow nullability
1 parent 19ad0a0 commit b4adf7c

8 files changed

Lines changed: 83 additions & 64 deletions

File tree

Arrays.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ public DebugView(Array<T> arr)
2424

2525
protected List<T> Inner;
2626

27-
public virtual AttributeList Owner
27+
public virtual AttributeList? Owner
2828
{
2929
get => _Owner;
3030
internal set
3131
{
3232
_Owner = value;
3333
}
3434
}
35-
AttributeList _Owner;
35+
AttributeList? _Owner;
3636

37-
protected Datamodel OwnerDatamodel => Owner?.Owner;
37+
protected Datamodel? OwnerDatamodel => Owner?.Owner;
3838

3939
internal Array()
4040
{
@@ -94,40 +94,52 @@ public void CopyTo(T[] array, int offset)
9494

9595
public object SyncRoot => throw new NotImplementedException();
9696

97-
object IList.this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
97+
object? IList.this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
9898

9999
public bool Remove(T item) => Inner.Remove(item);
100100

101101
public IEnumerator<T> GetEnumerator() => Inner.GetEnumerator();
102102
IEnumerator IEnumerable.GetEnumerator() => Inner.GetEnumerator();
103103

104104
#region IList
105-
int IList.Add(object value)
105+
int IList.Add(object? value)
106106
{
107-
Add((T)value);
107+
if (value is not null)
108+
Add((T)value);
108109
return Count;
109110
}
110111

111-
bool IList.Contains(object value)
112+
bool IList.Contains(object? value)
112113
{
114+
if (value is null)
115+
return false;
113116
return Contains((T)value);
114117
}
115118

116-
int IList.IndexOf(object value)
119+
int IList.IndexOf(object? value)
117120
{
121+
if (value is null)
122+
throw new InvalidOperationException("Trying to get the index of a null object");
123+
118124
return IndexOf((T)value);
119125
}
120126

121-
void IList.Insert(int index, object value)
127+
void IList.Insert(int index, object? value)
122128
{
129+
if (value is null)
130+
throw new InvalidOperationException("Trying to insert a null object");
131+
123132
Insert(index, (T)value);
124133
}
125134

126135
bool IList.IsFixedSize { get { return false; } }
127136
bool IList.IsReadOnly { get { return false; } }
128137

129-
void IList.Remove(object value)
138+
void IList.Remove(object? value)
130139
{
140+
if (value is null)
141+
throw new InvalidOperationException("Trying to remove a null object");
142+
131143
Remove((T)value);
132144
}
133145

@@ -156,7 +168,7 @@ public ElementArray(int capacity)
156168
/// </summary>
157169
internal IEnumerable<Element> RawList { get { foreach (var elem in Inner) yield return elem; } }
158170

159-
public override AttributeList Owner
171+
public override AttributeList? Owner
160172
{
161173
get => base.Owner;
162174
internal set
@@ -191,7 +203,7 @@ protected override void Insert_Internal(int index, Element item)
191203
throw new ElementOwnershipException();
192204
}
193205

194-
base.Insert_Internal(index, item);
206+
base.Insert_Internal(index, item!);
195207
}
196208

197209
public override Element this[int index]
@@ -210,6 +222,12 @@ public override Element this[int index]
210222
throw new DestubException(this, index, err);
211223
}
212224
}
225+
226+
if (elem is null)
227+
{
228+
throw new InvalidOperationException("Element at specified index is null");
229+
}
230+
213231
return elem;
214232
}
215233
set => base[index] = value;

Attribute.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Numerics;
44

5-
using AttrKVP = System.Collections.Generic.KeyValuePair<string, object>;
5+
using AttrKVP = System.Collections.Generic.KeyValuePair<string, object?>;
66

77
namespace Datamodel
88
{
@@ -16,7 +16,7 @@ class Attribute
1616
/// </summary>
1717
/// <param name="name">The name of the Attribute, which must be unique to its owner.</param>
1818
/// <param name="value">The value of the Attribute, which must be of a supported Datamodel type.</param>
19-
public Attribute(string name, AttributeList owner, object value)
19+
public Attribute(string name, AttributeList owner, object? value)
2020
{
2121
ArgumentNullException.ThrowIfNull(name);
2222

@@ -48,7 +48,7 @@ public Attribute(string name, AttributeList owner, long defer_offset)
4848
/// <summary>
4949
/// Gets the Type of this Attribute's Value.
5050
/// </summary>
51-
public Type ValueType { get; private set; }
51+
public Type ValueType { get; private set; } = typeof(Element);
5252

5353
/// <summary>
5454
/// Gets or sets the OverrideType of this Attributes.
@@ -97,7 +97,7 @@ internal set
9797
}
9898
AttributeList _Owner;
9999

100-
Datamodel OwnerDatamodel { get { return Owner?.Owner; } }
100+
Datamodel? OwnerDatamodel { get { return Owner.Owner; } }
101101

102102
/// <summary>
103103
/// Gets whether the value of this Attribute has yet to be decoded.
@@ -138,7 +138,7 @@ public void DeferredLoad()
138138
/// </summary>
139139
/// <exception cref="CodecException">Thrown when deferred value loading fails.</exception>
140140
/// <exception cref="DestubException">Thrown when Element destubbing fails.</exception>
141-
public object Value
141+
public object? Value
142142
{
143143
get
144144
{
@@ -197,12 +197,12 @@ public object Value
197197
Offset = 0;
198198
}
199199
}
200-
object _Value;
200+
object? _Value = null;
201201

202202
/// <summary>
203203
/// Gets the Attribute's Value without attempting deferred loading or destubbing.
204204
/// </summary>
205-
public object RawValue { get { return _Value; } }
205+
public object? RawValue { get { return _Value; } }
206206

207207
#endregion
208208

AttributeList.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public enum OverrideType
8383
Binary,
8484
}
8585

86-
public AttributeList(Datamodel owner)
86+
public AttributeList(Datamodel? owner)
8787
{
8888
var propertyAttributes = GetPropertyDerivedAttributeList();
8989
PropertyInfos = new OrderedDictionary(propertyAttributes?.Count ?? 0);
@@ -102,7 +102,7 @@ public AttributeList(Datamodel owner)
102102
/// <summary>
103103
/// Gets the <see cref="Datamodel"/> that this AttributeList is owned by.
104104
/// </summary>
105-
public virtual Datamodel Owner { get; internal set; }
105+
public virtual Datamodel? Owner { get; internal set; }
106106

107107
/// <summary>
108108
/// Adds a new attribute to this AttributeList.
@@ -115,7 +115,7 @@ public void Add(string key, object value)
115115
}
116116

117117

118-
protected virtual ICollection<(string Name, PropertyInfo Property)> GetPropertyDerivedAttributeList()
118+
protected virtual ICollection<(string Name, PropertyInfo Property)>? GetPropertyDerivedAttributeList()
119119
{
120120
return null;
121121
}

Codecs/Binary.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ float[] ReadVector(int dim)
267267
return output;
268268
}
269269

270-
object ReadValue(Datamodel dm, Type type, bool raw_string)
270+
object? ReadValue(Datamodel dm, Type type, bool raw_string)
271271
{
272272
if (type == typeof(Element))
273273
{
@@ -450,13 +450,13 @@ public Datamodel Decode(string encoding, int encoding_version, string format, in
450450

451451
int EncodingVersion;
452452

453-
public object DeferredDecodeAttribute(Datamodel dm, long offset)
453+
public object? DeferredDecodeAttribute(Datamodel dm, long offset)
454454
{
455455
Reader.BaseStream.Seek(offset, SeekOrigin.Begin);
456456
return DecodeAttribute(dm, false);
457457
}
458458

459-
object DecodeAttribute(Datamodel dm, bool prefix)
459+
object? DecodeAttribute(Datamodel dm, bool prefix)
460460
{
461461
var types = IdToType(Reader.ReadByte());
462462

Datamodel.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<RootNamespace>Datamodel</RootNamespace>
66
<PackageId>KeyValues2</PackageId>
77
<Version>0.7</Version>
8+
<Nullable>enable</Nullable>
89
<PackageLicenseFile>COPYING</PackageLicenseFile>
910
<PackageReadmeFile>README.md</PackageReadmeFile>
1011
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>

Datamodel.NET.sln

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Datamodel.NET", "Datamodel.
66
EndProject
77
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{4C928D60-5E48-4C0D-9C7E-C75D9734CD58}"
88
EndProject
9-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "..\ConsoleApp1\ConsoleApp1.csproj", "{B4982001-13F5-4CD3-953E-F438FC42699E}"
10-
ProjectSection(ProjectDependencies) = postProject
11-
{075743A9-B292-410C-B68F-6E6CF588D60A} = {075743A9-B292-410C-B68F-6E6CF588D60A}
12-
EndProjectSection
13-
EndProject
149
Global
1510
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1611
Debug|Any CPU = Debug|Any CPU
@@ -30,12 +25,6 @@ Global
3025
{4C928D60-5E48-4C0D-9C7E-C75D9734CD58}.Documentation|Any CPU.Build.0 = Debug|Any CPU
3126
{4C928D60-5E48-4C0D-9C7E-C75D9734CD58}.Release|Any CPU.ActiveCfg = Release|Any CPU
3227
{4C928D60-5E48-4C0D-9C7E-C75D9734CD58}.Release|Any CPU.Build.0 = Release|Any CPU
33-
{B4982001-13F5-4CD3-953E-F438FC42699E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34-
{B4982001-13F5-4CD3-953E-F438FC42699E}.Debug|Any CPU.Build.0 = Debug|Any CPU
35-
{B4982001-13F5-4CD3-953E-F438FC42699E}.Documentation|Any CPU.ActiveCfg = Release|Any CPU
36-
{B4982001-13F5-4CD3-953E-F438FC42699E}.Documentation|Any CPU.Build.0 = Release|Any CPU
37-
{B4982001-13F5-4CD3-953E-F438FC42699E}.Release|Any CPU.ActiveCfg = Release|Any CPU
38-
{B4982001-13F5-4CD3-953E-F438FC42699E}.Release|Any CPU.Build.0 = Release|Any CPU
3928
EndGlobalSection
4029
GlobalSection(SolutionProperties) = preSolution
4130
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)