Skip to content

Commit 9e2a4fc

Browse files
committed
.NET: bindings cleanup/fix
1 parent bc337ab commit 9e2a4fc

File tree

6 files changed

+48
-25
lines changed

6 files changed

+48
-25
lines changed

.clang-format

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Language: Cpp
33
# BasedOnStyle: WebKit
44
AccessModifierOffset: -4
5-
ConstructorInitializerIndentWidth: 4
65
AlignEscapedNewlinesLeft: false
76
AlignTrailingComments: false
87
AllowAllParametersOfDeclarationOnNextLine: true

bindings/dn/Imageflow.Test/TestApi.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Diagnostics;
3+
using Xunit;
4+
using Imageflow;
5+
using System.Dynamic;
6+
using System.IO;
7+
using System.Text;
8+
using Imageflow.Native;
9+
using Xunit.Abstractions;
10+
11+
namespace Imageflow.Test
12+
{
13+
public class TestApi
14+
{
15+
private readonly ITestOutputHelper output;
16+
17+
public TestApi(ITestOutputHelper output)
18+
{
19+
this.output = output;
20+
}
21+
22+
[Fact]
23+
public void TestCreateDestroyContext()
24+
{
25+
using (var c = new JobContext())
26+
{
27+
c.AssertReady();
28+
}
29+
}
30+
}
31+
}

bindings/dn/Imageflow/ImageflowException.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ImageflowException : Exception
1111
{
1212
const int MaxBufferSize = 8096;
1313

14-
internal ImageflowException(string message) : base(message)
14+
private ImageflowException(string message) : base(message)
1515
{
1616

1717
}
@@ -25,14 +25,15 @@ public static Exception FromContext(JobContext c, bool fullPaths = true, ulong d
2525
var buffer = new byte[defaultBufferSize];
2626
var pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned);
2727

28-
var bytesWritten = UIntPtr.Zero;
29-
var everythingWritten = false;
28+
29+
bool everythingWritten;
3030

3131
string message = null;
3232
try
3333
{
34+
3435
everythingWritten = NativeMethods.imageflow_context_error_write_to_buffer(c.Pointer,
35-
pinned.AddrOfPinnedObject(), new UIntPtr((ulong) buffer.LongLength), out bytesWritten);
36+
pinned.AddrOfPinnedObject(), new UIntPtr((ulong) buffer.LongLength), out var bytesWritten);
3637

3738
if (bytesWritten.ToUInt64() > 0)
3839
{

bindings/dn/Imageflow/ImageflowUnmanagedReadStream.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Imageflow
99
/// <summary>
1010
/// An UnmanagedMemoryStream that checks that the underlying Imageflow context isn't in a disposed or errored state
1111
/// </summary>
12+
/// <inheritdoc cref="UnmanagedMemoryStream"/>
1213
public class ImageflowUnmanagedReadStream : UnmanagedMemoryStream
1314
{
1415
private readonly IAssertReady _underlying;

bindings/dn/Imageflow/JobContext.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public JobContext()
3636
throw new Exception($".NET Imageflow bindings only support ABI {NativeMethods.ABI_MAJOR}.{NativeMethods.ABI_MINOR}. libimageflow ABI {major}.{minor} is loaded.");
3737
}
3838

39-
internal void AddPinnedData(GCHandle handle)
39+
private void AddPinnedData(GCHandle handle)
4040
{
4141
if (_pinned == null) _pinned = new List<GCHandle>();
4242
_pinned.Add(handle);
4343
}
4444

4545
public bool HasError => NativeMethods.imageflow_context_has_error(Pointer);
4646

47-
public static byte[] SerializeToJson<T>(T obj){
47+
private static byte[] SerializeToJson<T>(T obj){
4848
using (var stream = new MemoryStream())
4949
using (var writer = new StreamWriter(stream, new UTF8Encoding(false))){
5050
JsonSerializer.Create().Serialize(writer, obj);
@@ -82,7 +82,7 @@ public void AssertReady()
8282
{
8383
if (HasError) throw ImageflowException.FromContext(this);
8484
}
85-
85+
8686
public JsonResponse ExecuteImageResizer4CommandString( int inputId, int outputId, string commands)
8787
{
8888
var message = new
@@ -96,7 +96,7 @@ public JsonResponse ExecuteImageResizer4CommandString( int inputId, int outputId
9696
command_string = new
9797
{
9898
kind = "ir4",
99-
value = "w=200&h=200&scale=both&format=jpg",
99+
value = commands,
100100
decode = inputId,
101101
encode = outputId
102102
}
@@ -208,11 +208,8 @@ public void AddOutputBuffer(int ioId)
208208
public Stream GetOutputBuffer(int ioId)
209209
{
210210
AssertReady();
211-
IntPtr buffer;
212-
UIntPtr bufferSize;
213-
AssertReady();
214-
if (!NativeMethods.imageflow_context_get_output_buffer_by_id(Pointer, ioId, out buffer,
215-
out bufferSize))
211+
if (!NativeMethods.imageflow_context_get_output_buffer_by_id(Pointer, ioId, out var buffer,
212+
out var bufferSize))
216213
{
217214
AssertReady();
218215
throw new ImageflowAssertionFailed("AssertReady should raise an exception if method fails");

bindings/dn/Imageflow/JsonResponse.cs

+5-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class JsonResponse : IDisposable, IAssertReady
1111
private IntPtr _ptr;
1212
private readonly JobContext _parent;
1313

14-
internal IntPtr Pointer
14+
private IntPtr Pointer
1515
{
1616
get
1717
{
@@ -39,26 +39,20 @@ private void Read(out int statusCode, out IntPtr utf8Buffer, out UIntPtr bufferS
3939

4040
public int GetStatusCode()
4141
{
42-
int statusCode;
43-
IntPtr utf8Buffer;
44-
UIntPtr bufferSize;
45-
Read(out statusCode, out utf8Buffer, out bufferSize);
42+
Read(out var statusCode, out var utf8Buffer, out var bufferSize);
4643
return statusCode;
4744
}
4845

4946
public Stream GetStream()
5047
{
51-
int statusCode;
52-
IntPtr utf8Buffer;
53-
UIntPtr bufferSize;
54-
Read(out statusCode, out utf8Buffer, out bufferSize);
48+
Read(out var statusCode, out var utf8Buffer, out var bufferSize);
5549
return new ImageflowUnmanagedReadStream(this, utf8Buffer, bufferSize);
5650
}
5751

5852
public T Deserialize<T>() where T : class
5953
{
6054
using (var reader = new StreamReader(GetStream(), Encoding.UTF8))
61-
return JsonSerializer.Create().Deserialize((JsonReader) new JsonTextReader(reader), typeof(T)) as T;
55+
return JsonSerializer.Create().Deserialize(new JsonTextReader(reader), typeof(T)) as T;
6256
}
6357

6458
public dynamic DeserializeDynamic()
@@ -108,7 +102,7 @@ protected virtual void Dispose(bool disposing)
108102
public void AssertReady()
109103
{
110104
_parent.AssertReady();
111-
if (this.Pointer == IntPtr.Zero) throw new ImageflowAssertionFailed("Pointer must never return zero");
105+
if (this.Pointer == IntPtr.Zero) throw new ImageflowDisposedException("JsonResponse");
112106
}
113107

114108
}

0 commit comments

Comments
 (0)