Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86498c7

Browse files
committed
Get rid of RepositorySafeHandle
We add an implicit conversion from the handle to the pointer as there are a lot of places which rely on the equivalent functionality for the SafeHandle.
1 parent 8be1e38 commit 86498c7

File tree

11 files changed

+452
-372
lines changed

11 files changed

+452
-372
lines changed

LibGit2Sharp/BlameHunkCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class BlameHunkCollection : IEnumerable<BlameHunk>
2020
/// </summary>
2121
protected BlameHunkCollection() { }
2222

23-
internal BlameHunkCollection(Repository repo, RepositorySafeHandle repoHandle, string path, BlameOptions options)
23+
internal BlameHunkCollection(Repository repo, RepositoryHandle repoHandle, string path, BlameOptions options)
2424
{
2525
this.repo = repo;
2626

LibGit2Sharp/Core/Handles/Objects.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ internal bool IsNull
4343
}
4444
}
4545

46+
internal IntPtr AsIntPtr()
47+
{
48+
return new IntPtr(ptr);
49+
}
4650

4751
void Dispose(bool disposing)
4852
{
@@ -61,6 +65,11 @@ public void Dispose()
6165
{
6266
Dispose(true);
6367
}
68+
69+
public static implicit operator git_tree_entry*(TreeEntryHandle handle)
70+
{
71+
return handle.Handle;
72+
}
6473
}
6574

6675
internal unsafe class ReferenceHandle : IDisposable
@@ -102,6 +111,10 @@ internal bool IsNull
102111
}
103112
}
104113

114+
internal IntPtr AsIntPtr()
115+
{
116+
return new IntPtr(ptr);
117+
}
105118

106119
void Dispose(bool disposing)
107120
{
@@ -120,6 +133,79 @@ public void Dispose()
120133
{
121134
Dispose(true);
122135
}
136+
137+
public static implicit operator git_reference*(ReferenceHandle handle)
138+
{
139+
return handle.Handle;
140+
}
141+
}
142+
143+
internal unsafe class RepositoryHandle : IDisposable
144+
{
145+
git_repository* ptr;
146+
internal git_repository* Handle
147+
{
148+
get
149+
{
150+
return ptr;
151+
}
152+
}
153+
154+
bool owned;
155+
bool disposed;
156+
157+
public unsafe RepositoryHandle(git_repository* handle, bool owned)
158+
{
159+
this.ptr = handle;
160+
this.owned = owned;
161+
}
162+
163+
public unsafe RepositoryHandle(IntPtr ptr, bool owned)
164+
{
165+
this.ptr = (git_repository*) ptr.ToPointer();
166+
this.owned = owned;
167+
}
168+
169+
~RepositoryHandle()
170+
{
171+
Dispose(false);
172+
}
173+
174+
internal bool IsNull
175+
{
176+
get
177+
{
178+
return ptr == null;
179+
}
180+
}
181+
182+
internal IntPtr AsIntPtr()
183+
{
184+
return new IntPtr(ptr);
185+
}
186+
187+
void Dispose(bool disposing)
188+
{
189+
if (!disposed)
190+
{
191+
if (owned)
192+
{
193+
NativeMethods.git_repository_free(ptr);
194+
}
195+
}
196+
197+
disposed = true;
198+
}
199+
200+
public void Dispose()
201+
{
202+
Dispose(true);
203+
}
204+
205+
public static implicit operator git_repository*(RepositoryHandle handle)
206+
{
207+
return handle.Handle;
208+
}
123209
}
124210

125211
}

LibGit2Sharp/Core/Handles/Objects.tt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ namespace LibGit2Sharp.Core
1414
var cNames = new[] {
1515
"git_tree_entry",
1616
"git_reference",
17+
"git_repository",
1718
};
1819

1920
var csNames = new[] {
2021
"TreeEntryHandle",
2122
"ReferenceHandle",
23+
"RepositoryHandle",
2224
};
2325

2426
for (var i = 0; i < cNames.Length; i++)
@@ -63,6 +65,10 @@ for (var i = 0; i < cNames.Length; i++)
6365
}
6466
}
6567

68+
internal IntPtr AsIntPtr()
69+
{
70+
return new IntPtr(ptr);
71+
}
6672

6773
void Dispose(bool disposing)
6874
{
@@ -81,6 +87,11 @@ for (var i = 0; i < cNames.Length; i++)
8187
{
8288
Dispose(true);
8389
}
90+
91+
public static implicit operator <#= cNames[i] #>*(<#= csNames[i] #> handle)
92+
{
93+
return handle.Handle;
94+
}
8495
}
8596

8697
<#

LibGit2Sharp/Core/Handles/RepositorySafeHandle.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)