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

Skip to content

Commit c9c49b1

Browse files
committed
Use a common abstract class for the handles
Instead of copying each and every class, we can put the common functionality into a single class, using a void pointer and use the template to generate the bits which need to know about the pointer we're dealing with. Freeing and the implicit conversion to the pointer type are thus still there, but we now don't copy the disposing code everywhere.
1 parent b0a88b7 commit c9c49b1

File tree

8 files changed

+286
-1361
lines changed

8 files changed

+286
-1361
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
3+
namespace LibGit2Sharp.Core.Handles
4+
{
5+
internal unsafe abstract class Libgit2Object : IDisposable
6+
{
7+
protected void* ptr;
8+
9+
internal void* Handle
10+
{
11+
get
12+
{
13+
return ptr;
14+
}
15+
}
16+
17+
bool owned;
18+
bool disposed;
19+
20+
internal unsafe Libgit2Object(void* handle, bool owned)
21+
{
22+
this.ptr = handle;
23+
this.owned = owned;
24+
}
25+
26+
internal unsafe Libgit2Object(IntPtr ptr, bool owned)
27+
{
28+
this.ptr = ptr.ToPointer();
29+
this.owned = owned;
30+
}
31+
32+
~Libgit2Object()
33+
{
34+
Dispose(false);
35+
}
36+
37+
internal bool IsNull
38+
{
39+
get
40+
{
41+
return ptr == null;
42+
}
43+
}
44+
45+
internal IntPtr AsIntPtr()
46+
{
47+
return new IntPtr(ptr);
48+
}
49+
50+
public abstract void Free();
51+
52+
void Dispose(bool disposing)
53+
{
54+
if (!disposed)
55+
{
56+
if (owned)
57+
{
58+
Free();
59+
}
60+
61+
ptr = null;
62+
}
63+
64+
disposed = true;
65+
}
66+
67+
public void Dispose()
68+
{
69+
Dispose(true);
70+
}
71+
}
72+
}
73+

0 commit comments

Comments
 (0)