8000 Create handles via templating · vrkcse2011/libgit2sharp@c05ee27 · GitHub
[go: up one dir, main page]

Skip to content

Commit c05ee27

Browse files
committed
Create handles via templating
We cannot create libgit2 wrapper objects which are generic over the pointer types, so let's use a template instead. We map the C name to the C# name and generate the same code for each of them.
1 parent 690af70 commit c05ee27

File tree

7 files changed

+126
-32
lines changed

7 files changed

+126
-32
lines changed

LibGit2Sharp/Core/Handles/Objects.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+

2+
using System;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
7+
internal unsafe class TreeEntryHandle : IDisposable
8+
{
9+
internal git_tree_entry* Handle { get; private set; }
10+
bool owned;
11+
bool disposed;
12+
13+
public unsafe TreeEntryHandle(git_tree_entry* handle, bool owned)
14+
{
15+
this.Handle = handle;
16+
this.owned = owned;
17+
}
18+
19+
~TreeEntryHandle()
20+
{
21+
Dispose(false);
22+
}
23+
24+
void Dispose(bool disposing)
25+
{
26+
if (!disposed)
27+
{
28+
if (owned)
29+
{
30+
NativeMethods.git_tree_entry_free(Handle);
31+
}
32+
}
33+
34+
disposed = true;
35+
}
36+
37+
public void Dispose()
38+
{
39+
Dispose(true);
40+
}
41+
}
42+
}

LibGit2Sharp/Core/Handles/Objects.tt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<#@ template language="C#" #>
2+
<#@ output extention=".cs" #>
3+
<#@ assembly name="System.Core" #>
4+
<#@ import namespace="System.Linq" #>
5+
<#@ import namespace="System.Text" #>
6+
<#@ import namespace="System.Collections.Generic" #>
7+
8+
using System;
9+
10+
namespace LibGit2Sharp.Core
11+
{
12+
13+
<#
14+
var cNames = new[] {
15+
"git_tree_entry",
16+
};
17+
18+
var csNames = new[] {
19+
"TreeEntryHandle"
20+
};
21+
22+
for (var i = 0; i < cNames.Length; i++)
23+
{
24+
#>
25+
internal unsafe class <#= csNames[i] #> : IDisposable
26+
{
27+
internal <#= cNames[i] #>* Handle { get; private set; }
28+
bool owned;
29+
bool disposed;
30+
31+
public unsafe <#= csNames[i] #>(<#= cNames[i] #>* handle, bool owned)
32+
{
33+
this.Handle = handle;
34 6D40 +
this.owned = owned;
35+
}
36+
37+
~<#= csNames[i] #>()
38+
{
39+
Dispose(false);
40+
}
41+
42+
void Dispose(bool disposing)
43+
{
44+
if (!disposed)
45+
{
46+
if (owned)
47+
{
48+
NativeMethods.<#= cNames[i] #>_free(Handle);
49+
}
50+
}
51+
52+
disposed = true;
53+
}
54+
55+
public void Dispose()
56+
{
57+
Dispose(true);
58+
}
59+
}
60+
<#
61+
}
62+
#>
63+
}

LibGit2Sharp/Core/Handles/TreeEntryOwnedHandle.cs

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

LibGit2Sharp/Core/Proxy.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,12 +3159,18 @@ public static unsafe Mode git_tree_entry_attributes(git_tree_entry* entry)
31593159
return (Mode)NativeMethods.git_tree_entry_filemode(entry);
31603160
}
31613161

3162-
public static unsafe git_tree_entry* git_tree_entry_byindex(GitObjectSafeHandle tree, long idx)
3162+
public static unsafe TreeEntryHandle git_tree_entry_byindex(GitObjectSafeHandle tree, long idx)
31633163
{
3164-
return NativeMethods.git_tree_entry_byindex(tree, (UIntPtr)idx);
3164+
var handle = NativeMethods.git_tree_entry_byindex(tree, (UIntPtr)idx);
3165+
if (handle == null)
3166+
{
3167+
return null;
3168+
}
3169+
3170+
return new TreeEntryHandle(handle, false);
31653171
}
31663172

3167-
public static unsafe TreeEntryOwnedHandle git_tree_entry_bypath(RepositorySafeHandle repo, ObjectId id, FilePath treeentry_path)
3173+
public static unsafe TreeEntryHandle git_tree_entry_bypath(RepositorySafeHandle repo, ObjectId id, FilePath treeentry_path)
31683174
{
31693175
using (var obj = new ObjectSafeWrapper(id, repo))
31703176
{
@@ -3178,15 +3184,10 @@ public static unsafe TreeEntryOwnedHandle git_tree_entry_bypath(RepositorySafeHa
31783184

31793185
Ensure.ZeroResult(res);
31803186

3181-
return new TreeEntryOwnedHandle(treeEntryPtr);
3187+
return new TreeEntryHandle(treeEntryPtr, true);
31823188
}
31833189
}
31843190

3185-
public static unsafe void git_tree_entry_free(git_tree_entry* treeEntry)
3186-
{
3187-
NativeMethods.git_tree_entry_free(treeEntry);
3188-
}
3189-
31903191
public static unsafe ObjectId git_tree_entry_id(git_tree_entry* entry)
31913192
{
31923193
return NativeMethods.git_tree_entry_id(entry).MarshalAsObjectId();

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@
379379
<Compile Include="CertificateSsh.cs" />
380380
<Compile Include="IDiffResult.cs" />
381381
<Compile Include="Core\Opaques.cs" />
382-
<Compile Include="Core\Handles\TreeEntryOwnedHandle.cs" />
382+
<Compile Include="Core\Handles\Objects.cs">
383+
<DependentUpon>Objects.tt</DependentUpon>
384+
</Compile>
383385
</ItemGroup>
384386
<ItemGroup>
385387
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
@@ -389,6 +391,10 @@
389391
</ItemGroup>
390392
<ItemGroup>
391393
<None Include="packages.config" />
394+
<None Include="Core\Handles\Objects.tt">
395+
<Generator>TextTemplatingFileGenerator</Generator>
396+
<LastGenOutput>Objects.cs</LastGenOutput>
397+
</None>
392398
</ItemGroup>
393399
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
394400
<Import Project="UniqueIdentifier.targets" />

LibGit2Sharp/Tree.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
5454
return null;
5555
}
5656

57-
using (TreeEntryOwnedHandle treeEntry = Proxy.git_tree_entry_bypath(repo.Handle, Id, relativePath))
57+
using (TreeEntryHandle treeEntry = Proxy.git_tree_entry_bypath(repo.Handle, Id, relativePath))
5858
{
5959
if (treeEntry == null)
6060
{
@@ -64,7 +64,7 @@ private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
6464
string posixPath = relativePath.Posix;
6565
string filename = posixPath.Split('/').Last();
6666
string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length);
67-
return new TreeEntry(treeEntry.Handle, Id, repo, path.Combine(parentPath));
67+
return new TreeEntry(treeEntry, Id, repo, path.Combine(parentPath));
6868
}
6969
}
7070

LibGit2Sharp/TreeEntry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public class TreeEntry : IEquatable<TreeEntry>
2727
protected TreeEntry()
2828
{ }
2929

30-
internal unsafe TreeEntry(git_tree_entry* entry, ObjectId parentTreeId, Repository repo, FilePath parentPath)
30+
internal unsafe TreeEntry(TreeEntryHandle handle, ObjectId parentTreeId, Repository repo, FilePath parentPath)
3131
{
32+
var entry = handle.Handle;
3233
this.parentTreeId = parentTreeId;
3334
this.repo = repo;
3435
targetOid = Proxy.git_tree_entry_id(entry);

0 commit comments

Comments
 (0)
0