Permalink
Browse files

Add implicit cast from GitOid(?) to ObjectId

Also, support marshaling NULL in OidSafeHandle
  • Loading branch information...
dahlbyk authored and nulltoken committed Feb 28, 2013
1 parent fd218c0 commit 09beddcee56015bcdd15bda692c028698fc9cb96
Showing with 14 additions and 18 deletions.
  1. +10 −0 LibGit2Sharp/Core/GitOid.cs
  2. +3 −3 LibGit2Sharp/Core/Handles/OidSafeHandle.cs
  3. +1 −15 LibGit2Sharp/ObjectId.cs
@@ -12,5 +12,15 @@ internal struct GitOid
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] Id;
+
+ public static implicit operator ObjectId(GitOid oid)
+ {
+ return new ObjectId(oid);
+ }
+
+ public static implicit operator ObjectId(GitOid? oid)
+ {
+ return oid == null ? null : new ObjectId(oid.Value);
+ }
}
}
@@ -4,14 +4,14 @@ namespace LibGit2Sharp.Core.Handles
{
internal class OidSafeHandle : NotOwnedSafeHandleBase
{
- private GitOid MarshalAsGitOid()
+ private GitOid? MarshalAsGitOid()
{
- return (GitOid)Marshal.PtrToStructure(handle, typeof(GitOid));
+ return (GitOid?)Marshal.PtrToStructure(handle, typeof(GitOid));
}
public ObjectId MarshalAsObjectId()
{
- return new ObjectId(MarshalAsGitOid());
+ return MarshalAsGitOid();
}
}
}
View
@@ -98,7 +98,7 @@ public virtual string Sha
/// <returns>true if the <paramref name = "sha" /> parameter was converted successfully; otherwise, false.</returns>
public static bool TryParse(string sha, out ObjectId result)
{
- result = BuildFrom(sha, false);
+ result = BuildOidFrom(sha, false);
return result != null;
}
@@ -113,20 +113,6 @@ public static bool TryParse(string sha, out ObjectId result)
return ToOid(sha);
}
- private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid)
- {
- GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid);
-
- if (!oid.HasValue)
- {
- return null;
- }
-
- var objectId = new ObjectId(oid.Value);
-
- return objectId;
- }
-
/// <summary>
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "ObjectId" />.
/// </summary>

0 comments on commit 09beddc

Please sign in to comment.