8000 Introduce marshaling extension to IntPtr · crashsplash/libgit2sharp@9165ae3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9165ae3

Browse files
committed
Introduce marshaling extension to IntPtr
1 parent dce1367 commit 9165ae3

12 files changed

+31
-23
lines changed

LibGit2Sharp/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private IEnumerable<ConfigurationEntry<string>> BuildConfigEntries()
305305

306306
private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
307307
{
308-
var entry = (GitConfigEntry)Marshal.PtrToStructure(entryPtr, typeof(GitConfigEntry));
308+
var entry = entryPtr.MarshalAs<GitConfigEntry>();
309309

310310
return new ConfigurationEntry<string>(LaxUtf8Marshaler.FromNative(entry.namePtr),
311311
LaxUtf8Marshaler.FromNative(entry.valuePtr),

LibGit2Sharp/Core/Handles/GitConfigEntryHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal class GitConfigEntryHandle : NotOwnedSafeHandleBase
66
{
77
public GitConfigEntry MarshalAsGitConfigEntry()
88
{
9-
return (GitConfigEntry)Marshal.PtrToStructure(handle, typeof(GitConfigEntr 8000 y));
9+
return handle.MarshalAs<GitConfigEntry>();
1010
}
1111
}
1212
}

LibGit2Sharp/Core/Handles/GitErrorSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public GitError MarshalAsGitError()
1515
return null;
1616
}
1717

18-
return (GitError)Marshal.PtrToStructure(handle, typeof(GitError));
18+
return handle.MarshalAs<GitError>();
1919
}
2020
}
2121
}

LibGit2Sharp/Core/Handles/IndexEntrySafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal class IndexEntrySafeHandle : NotOwnedSafeHandleBase
66
{
77
public GitIndexEntry MarshalAsGitIndexEntry()
88
{
9-
return (GitIndexEntry)Marshal.PtrToStructure(handle, typeof(GitIndexEntry));
9+
return handle.MarshalAs<GitIndexEntry>();
1010
}
1111
}
1212
}

LibGit2Sharp/Core/Handles/OidSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal class OidSafeHandle : NotOwnedSafeHandleBase
66
{
77
private GitOid? MarshalAsGitOid()
88
{
9-
return IsInvalid ? null : (GitOid?)Marshal.PtrToStructure(handle, typeof(GitOid));
9+
return IsInvalid ? null : (GitOid?)handle.MarshalAs<GitOid>();
1010
}
1111

1212
public ObjectId MarshalAsObjectId()

LibGit2Sharp/Core/Handles/StatusEntrySafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public StatusEntrySafeHandle(IntPtr handle)
2121

2222
public GitStatusEntry MarshalAsGitStatusEntry()
2323
{
24-
return (GitStatusEntry)Marshal.PtrToStructure(handle, typeof(GitStatusEntry));
24+
return handle.MarshalAs<GitStatusEntry>();
2525
}
2626
}
2727
}

LibGit2Sharp/Core/IntPtrExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
internal static class IntPtrExtensions
7+
{
8+
public static T MarshalAs<T>(this IntPtr ptr, bool throwWhenNull = true)
9+
{
10+
if (!throwWhenNull && ptr == IntPtr.Zero)
11+
{
12+
return default(T);
13+
}
14+
return (T)Marshal.PtrToStructure(ptr, typeof(T));
15+
}
16+
}
17+
}

LibGit2Sharp/Core/Proxy.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ namespace LibGit2Sharp.Core
1313
{
1414
internal class Proxy
1515
{
16-
private static T MarshalAs<T>(IntPtr ptr)
17-
{
18-
if (ptr == IntPtr.Zero)
19-
{
20-
return default(T);
21-
}
22-
return (T)Marshal.PtrToStructure(ptr, typeof(T));
23-
}
24-
2516
#region giterr_
2617

2718
public static void giterr_set_str(GitErrorCategory error_class, Exception exception)
@@ -61,7 +52,7 @@ public static BlameSafeHandle git_blame_file(
6152

6253
public static GitBlameHunk git_blame_get_hunk_byindex(BlameSafeHandle blame, uint idx)
6354
{
64-
return MarshalAs<GitBlameHunk>(NativeMethods.git_blame_get_hunk_byindex(blame, idx));
55+
return NativeMethods.git_blame_get_hunk_byindex(blame, idx).MarshalAs<GitBlameHunk>(false);
6556
}
6657

6758
public static void git_blame_free(IntPtr blame)
@@ -741,7 +732,7 @@ public static int git_diff_num_deltas(DiffSafeHandle diff)
741732

742733
public static GitDiffDelta git_diff_get_delta(DiffSafeHandle diff, int idx)
743734
{
744-
return MarshalAs<GitDiffDelta>(NativeMethods.git_diff_get_delta(diff, (UIntPtr) idx));
735+
return NativeMethods.git_diff_get_delta(diff, (UIntPtr) idx).MarshalAs<GitDiffDelta>(false);
745736
}
746737

747738
#endregion
@@ -2796,7 +2787,7 @@ public static IList<GitRemoteHead> RemoteLsHelper(IntPtr heads, UIntPtr size)
27962787
var list = new List<GitRemoteHead>(count);
27972788
for (int i = 0; i < count; i++)
27982789
{
2799-
list.Add(MarshalAs<GitRemoteHead>(rawHeads[i]));
2790+
list.Add(rawHeads[i].MarshalAs<GitRemoteHead>());
28002791
}
28012792
return list;
28022793
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="CommitSortStrategies.cs" />
7777
<Compile Include="CompareOptions.cs" />
7878
<Compile Include="Core\Handles\PatchSafeHandle.cs" />
79+
<Compile Include="Core\IntPtrExtensions.cs" />
7980
<Compile Include="FetchOptions.cs" />
8081
<Compile Include="RefSpec.cs" />
8182
<Compile Include="RefSpecCollection.cs" />

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal ObjectDatabase(Repository repo)
4343
public virtual IEnumerator<GitObject> GetEnumerator()
4444
{
4545
ICollection<GitOid> oids = Proxy.git_odb_foreach(handle,
46-
ptr => (GitOid) Marshal.PtrToStructure(ptr, typeof (GitOid)));
46+
ptr => ptr.MarshalAs<GitOid>());
4747

4848
return oids
4949
.Select(gitOid => repo.Lookup<GitObject>(new ObjectId(gitOid)))

LibGit2Sharp/RepositoryStatus.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ internal RepositoryStatus(Repository repo, StatusOptions options)
7373

7474
if (entry.HeadToIndexPtr != IntPtr.Zero)
7575
{
76-
deltaHeadToIndex = (GitDiffDelta)Marshal.PtrToStructure(entry.HeadToIndexPtr, typeof(GitDiffDelta));
76+
deltaHeadToIndex = entry.HeadToIndexPtr.MarshalAs<GitDiffDelta>();
7777
}
7878
if (entry.IndexToWorkDirPtr != IntPtr.Zero)
7979
{
80-
deltaIndexToWorkDir = (GitDiffDelta)Marshal.PtrToStructure(entry.IndexToWorkDirPtr, typeof(GitDiffDelta));
80+
deltaIndexToWorkDir = entry.IndexToWorkDirPtr.MarshalAs<GitDiffDelta>();
8181
}
8282

8383
AddStatusEntryForDelta(entry.Status, deltaHeadToIndex, deltaIndexToWorkDir);

LibGit2Sharp/Signature.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public sealed class Signature : IEquatable<Signature>
1919

2020
internal Signature(IntPtr signaturePtr)
2121
{
22-
var handle = new GitSignature();
23-
Marshal.PtrToStructure(signaturePtr, handle);
22+
var handle = signaturePtr.MarshalAs<GitSignature>();
2423

2524
name = LaxUtf8Marshaler.FromNative(handle.Name);
2625
email = LaxUtf8Marshaler.FromNative(handle.Email);

0 commit comments

Comments
 (0)
0