Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 73e015d

Browse files
Merge pull request #433 from xamarin/refactor-the-second
Refactor value types
2 parents b89a972 + 745ead6 commit 73e015d

16 files changed

+68
-223
lines changed

SwiftRuntimeLibrary.Mac/SwiftRuntimeLibrary.Mac.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@
283283
<Compile Include="..\SwiftRuntimeLibrary\SwiftNativeObject.cs">
284284
<Link>SwiftNativeObject.cs</Link>
285285
</Compile>
286+
<Compile Include="..\SwiftRuntimeLibrary\SwiftNativeValueType.cs">
287+
<Link>SwiftNativeValueType.cs</Link>
288+
</Compile>
286289
</ItemGroup>
287290
<ItemGroup>
288291
<Folder Include="SwiftMarshal\" />

SwiftRuntimeLibrary.iOS/SwiftRuntimeLibrary.iOS.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@
288288
<Compile Include="..\SwiftRuntimeLibrary\SwiftNativeInstance.cs">
289289
<Link>SwiftNativeInstance.cs</Link>
290290
</Compile>
291+
<Compile Include="..\SwiftRuntimeLibrary\SwiftNativeValueType.cs">
292+
<Link>SwiftNativeValueType.cs</Link>
293+
</Compile>
291294
</ItemGroup>
292295
<Target Name="GeneratedCSCode" BeforeTargets="CoreCompile" Inputs="$(MSBuildProjectFullPath)" Outputs="GeneratedCode\BindingMetadata.iOS.cs">
293296
<Exec Command="make --directory=../SwiftRuntimeLibrary create-pinvokes-ios configuration=$(Configuration)" />

SwiftRuntimeLibrary/SwiftArray.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
namespace SwiftRuntimeLibrary {
1313
[SwiftTypeName ("Swift.Array")]
1414
[SwiftStruct (SwiftCoreConstants.LibSwiftCore, SwiftCoreConstants.SwiftArray_NominalTypeDescriptor, "", "")]
15-
public sealed class SwiftArray<T> : ISwiftStruct, IList<T> {
16-
17-
public byte [] SwiftData { get; set; }
15+
public sealed class SwiftArray<T> : SwiftNativeValueType, ISwiftStruct, IList<T> {
1816

1917
byte [] CheckedSwiftData {
2018
get {
@@ -90,8 +88,9 @@ static int ElementSizeOf {
9088
}
9189

9290
internal SwiftArray (SwiftNominalCtorArgument unused)
91+
: base ()
9392
{
94-
StructMarshal.Marshaler.PrepareNominal (this);
93+
9594
}
9695

9796
public static SwiftMetatype GetSwiftMetatype ()
@@ -135,21 +134,6 @@ public int Capacity {
135134
}
136135
}
137136

138-
bool disposed = false;
139-
public void Dispose ()
140-
{
141-
if (!disposed) {
142-
disposed = true;
143-
Dispose (true);
144-
GC.SuppressFinalize (this);
145-
}
146-
}
147-
148-
void Dispose (bool disposing)
149-
{
150-
StructMarshal.Marshaler.NominalDestroy (this);
151-
}
152-
153137
public IEnumerator<T> GetEnumerator ()
154138
{
155139
int count = Count;

SwiftRuntimeLibrary/SwiftCharacter.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace SwiftRuntimeLibrary {
1212
[SwiftTypeName ("Swift.Character")]
1313
[SwiftStruct (SwiftCoreConstants.LibSwiftCore, SwiftCoreConstants.SwiftCharacter_NominalTypeDescriptor, SwiftCoreConstants.SwiftCharacter_Metadata, "")]
14-
public sealed class SwiftCharacter : ISwiftStruct {
14+
public sealed class SwiftCharacter : SwiftNativeValueType, ISwiftStruct {
1515
public unsafe SwiftCharacter (string character) : this (SwiftNominalCtorArgument.None)
1616
{
1717
SwiftString swiftString = SwiftString.FromString (character);
@@ -25,19 +25,13 @@ public SwiftCharacter (char character) : this (character.ToString ())
2525
}
2626

2727
internal SwiftCharacter (SwiftNominalCtorArgument unused)
28+
: base ()
2829
{
29-
StructMarshal.Marshaler.PrepareNominal (this);
3030
}
3131

32-
public byte [] SwiftData {
33-
get;
34-
set;
35-
}
36-
37-
public void Dispose ()
32+
~SwiftCharacter ()
3833
{
39-
Dispose (true);
40-
GC.SuppressFinalize (this);
34+
Dispose (false);
4135
}
4236

4337
[DllImport (SwiftCore.kXamGlue,
@@ -69,11 +63,6 @@ public unsafe override string ToString ()
6963

7064
// Unlike the 3 above, this is unsafe as a SwiftCaracter may contain more than a char
7165
//public static explicit operator char (SwiftCharacter character) => throw new NotImplementedException ();
72-
73-
void Dispose (bool disposing)
74-
{
75-
StructMarshal.Marshaler.NominalDestroy (this);
76-
}
7766
}
7867
}
7968

SwiftRuntimeLibrary/SwiftDate.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace SwiftRuntimeLibrary {
1111
[SwiftTypeName ("Foundation.Date")]
1212
[SwiftStruct (SwiftFoundationConstants.LibSwiftFoundation, SwiftFoundationConstants.SwiftDate_NominalTypeDescriptor, SwiftFoundationConstants.SwiftData_TypeMetadata, "")]
13-
public class SwiftDate : ISwiftStruct {
13+
public class SwiftDate : SwiftNativeValueType, ISwiftStruct {
1414
public static SwiftDate SwiftDate_TimeIntervalSinceReferenceDate (double timeIntervalSinceReferenceDate)
1515
{
1616
unsafe {
@@ -79,33 +79,15 @@ public SwiftDate (double timeIntervalSinceNow)
7979
}
8080

8181
internal SwiftDate (SwiftNominalCtorArgument unused)
82+
: base ()
8283
{
83-
StructMarshal.Marshaler.PrepareNominal (this);
8484
}
8585

8686
public static SwiftMetatype GetSwiftMetatype ()
8787
{
8888
return NativeMethodsForDDate.PIMetadataAccessor_DDate (SwiftMetadataRequest.Complete);
8989
}
9090

91-
public void Dispose ()
92-
{
93-
Dispose (true);
94-
GC.SuppressFinalize (this);
95-
}
96-
97-
void Dispose (bool disposing)
98-
{
99-
if (SwiftData != null) {
100-
unsafe {
101-
fixed (byte* p = SwiftData) {
102-
StructMarshal.Marshaler.NominalDestroy (typeof (SwiftDate),
103-
p);
104-
}
105-
SwiftData = null;
106-
}
107-
}
108-
}
10991
~SwiftDate ()
11092
{
11193
Dispose (false);
@@ -125,10 +107,6 @@ public NSDate ToNSDate ()
125107
}
126108
}
127109
}
128-
public byte [] SwiftData {
129-
get;
130-
set;
131-
}
132110

133111
public static explicit operator NSDate (SwiftDate date)
134112
{

SwiftRuntimeLibrary/SwiftDictionary.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ namespace SwiftRuntimeLibrary {
1212

1313
[SwiftTypeName ("Swift.Dictionary")]
1414
[SwiftStruct (SwiftCoreConstants.LibSwiftCore, SwiftCoreConstants.SwiftDictionary_NominalTypeDescriptor, "", "")]
15-
public class SwiftDictionary<T, U> : IDictionary<T, U>, ISwiftStruct {
16-
public byte [] SwiftData { get; set; }
17-
15+
public class SwiftDictionary<T, U> : SwiftNativeValueType, IDictionary<T, U>, ISwiftStruct {
1816
public SwiftDictionary () : this (0) { }
1917

2018
public SwiftDictionary (int capacity)
@@ -41,10 +39,14 @@ public SwiftDictionary (IDictionary<T, U> elems)
4139
}
4240

4341
internal SwiftDictionary (SwiftNominalCtorArgument unused)
42+
: base ()
4443
{
45-
StructMarshal.Marshaler.PrepareNominal (this);
4644
}
4745

46+
~SwiftDictionary ()
47+
{
48+
Dispose (false);
49+
}
4850

4951
public static SwiftMetatype GetSwiftMetatype ()
5052
{
@@ -220,23 +222,6 @@ IEnumerator IEnumerable.GetEnumerator ()
220222
return GetEnumerator ();
221223
}
222224

223-
224-
bool disposed = false;
225-
public void Dispose ()
226-
{
227-
if (!disposed) {
228-
disposed = true;
229-
Dispose (true);
230-
GC.SuppressFinalize (this);
231-
}
232-
}
233-
234-
void Dispose (bool disposing)
235-
{
236-
StructMarshal.Marshaler.NominalDestroy (this);
237-
}
238-
239-
240225
public U this [T key] {
241226
get {
242227
unsafe {

SwiftRuntimeLibrary/SwiftHasher.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace SwiftRuntimeLibrary {
99
[SwiftTypeName ("Swift.Hasher")]
1010
[SwiftStruct(SwiftCoreConstants.LibSwiftCore, SwiftCoreConstants.Hasher_NominalTypeDescriptor, SwiftCoreConstants.Hasher_Metadata, "")]
11-
public class SwiftHasher : ISwiftStruct {
11+
public class SwiftHasher : SwiftNativeValueType, ISwiftStruct {
1212
public unsafe SwiftHasher ()
1313
{
1414
fixed (byte * thisDataPtr = StructMarshal.Marshaler.PrepareNominal (this)) {
@@ -18,6 +18,7 @@ public unsafe SwiftHasher ()
1818
}
1919

2020
internal SwiftHasher (SwiftNominalCtorArgument unused)
21+
: base ()
2122
{
2223
StructMarshal.Marshaler.PrepareNominal (this);
2324
}
@@ -27,22 +28,6 @@ public static SwiftMetatype GetSwiftMetatype ()
2728
return NativeMethodsForSwiftHasher.PIMetadataAccessor_Hasher (SwiftMetadataRequest.Complete);
2829
}
2930

30-
public void Dispose ()
31-
{
32-
Dispose (true);
33-
GC.SuppressFinalize (this);
34-
}
35-
36-
unsafe void Dispose (bool disposing)
37-
{
38-
if (SwiftData != null) {
39-
fixed (byte *p = SwiftData) {
40-
StructMarshal.Marshaler.NominalDestroy (typeof (SwiftHasher), p);
41-
}
42-
SwiftData = null;
43-
}
44-
}
45-
4631
~SwiftHasher ()
4732
{
4833
Dispose (false);
@@ -110,11 +95,6 @@ public nint FinalizeHasher ()
11095
}
11196
}
11297
}
113-
114-
public byte [] SwiftData {
115-
get;
116-
set;
117-
}
11898
}
11999

120100
internal class NativeMethodsForSwiftHasher {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using SwiftRuntimeLibrary.SwiftMarshal;
5+
6+
namespace SwiftRuntimeLibrary {
7+
public abstract class SwiftNativeValueType : SwiftNativeInstance, ISwiftNominalType {
8+
protected SwiftNativeValueType ()
9+
{
10+
StructMarshal.Marshaler.PrepareNominal (this);
11+
}
12+
13+
bool disposed;
14+
protected override void Dispose (bool disposing)
15+
{
16+
if (disposed)
17+
return;
18+
disposed = true;
19+
StructMarshal.Marshaler.NominalDestroy (this);
20+
SwiftData = null;
21+
}
22+
23+
public byte [] SwiftData { get; set; }
24+
}
25+
}

SwiftRuntimeLibrary/SwiftOptional.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ public enum SwiftOptionalCases {
1616

1717
[SwiftTypeName ("Swift.Optional")]
1818
[SwiftEnumType (SwiftCoreConstants.LibSwiftCore, SwiftCoreConstants.SwiftOptional_NominalTypeDescriptor, "", "")]
19-
public class SwiftOptional<T> : ISwiftEnum {
19+
public class SwiftOptional<T> : SwiftNativeValueType, ISwiftEnum {
2020
internal SwiftOptional (SwiftNominalCtorArgument unused)
21+
: base ()
2122
{
2223
}
2324
public SwiftOptional ()
25+
: base ()
2426
{
2527
unsafe {
26-
fixed (byte* p = StructMarshal.Marshaler.PrepareNominal (this)) {
28+
fixed (byte* p = SwiftData) {
2729
NativeMethodsForSwiftOptional.NewNone (new IntPtr (p),
2830
StructMarshal.Marshaler.Metatypeof (typeof (T)));
2931
}
@@ -54,24 +56,11 @@ public static SwiftMetatype GetSwiftMetatype ()
5456
StructMarshal.Marshaler.Metatypeof (typeof (T)));
5557
}
5658

57-
public byte [] SwiftData { get; set; }
58-
5959
~SwiftOptional ()
6060
{
6161
Dispose (false);
6262
}
6363

64-
public void Dispose ()
65-
{
66-
Dispose (true);
67-
GC.SuppressFinalize (this);
68-
}
69-
70-
void Dispose (bool disposing)
71-
{
72-
StructMarshal.Marshaler.NominalDestroy (this);
73-
}
74-
7564
public static SwiftOptional<T> None ()
7665
{
7766
return new SwiftOptional<T> ();

SwiftRuntimeLibrary/SwiftRuntimeLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<Compile Include="BaseAssociatedTypeProxy.cs" />
119119
<Compile Include="SwiftNativeObject.cs" />
120120
<Compile Include="SwiftNativeInstance.cs" />
121+
<Compile Include="SwiftNativeValueType.cs" />
121122
</ItemGroup>
122123
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
123124
<ItemGroup>

0 commit comments

Comments
 (0)