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+
usingSystem;
2+
3+
namespaceLibGit2Sharp.Core.Handles
4+
{
5+
internalunsafeabstractclassLibgit2Object:IDisposable
6+
{
7+
protectedvoid*ptr;
8+
9+
internalvoid*Handle
10+
{
11+
get
12+
{
13+
returnptr;
14+
}
15+
}
16+
17+
boolowned;
18+
booldisposed;
19+
20+
internalunsafeLibgit2Object(void*handle,boolowned)
21+
{
22+
this.ptr=handle;
23+
this.owned=owned;
24+
}
25+
26+
internalunsafeLibgit2Object(IntPtrptr,boolowned)
27+
{
28+
this.ptr=ptr.ToPointer();
29+
this.owned=owned;
30+
}
31+
32+
~Libgit2Object()
33+
{
34+
Dispose(false);
35+
}
36+
37+
internalboolIsNull
38+
{
39+
get
40+
{
41+
returnptr==null;
42+
}
43+
}
44+
45+
internalIntPtrAsIntPtr()
46+
{
47+
returnnewIntPtr(ptr);
48+
}
49+
50+
publicabstractvoidFree();
51+
52+
voidDispose(booldisposing)
53+
{
54+
if(!disposed)
55+
{
56+
if(owned)
57+
{
58+
Free();
59+
}
60+
61+
ptr=null;
62+
}
63+
64+
disposed=true;
65+
}
66+
67+
publicvoidDispose()
68+
{
69+
Dispose(true);
70+
}
71+
}
72+
}
73+

0 commit comments

Comments
(0)