1
1
using System . Runtime . CompilerServices ;
2
2
using System . Runtime . InteropServices ;
3
3
using WPIHal ;
4
+ using unsafe HalConstBufferCreate = delegate * managed< int , delegate * unmanaged[ Cdecl] < byte * , void * , byte * , uint , void > , void * , int > ;
4
5
using unsafe HalGlobalCreate = delegate * managed< delegate * unmanaged[ Cdecl] < byte * , void * , WPIHal . HalValue * , void > , void * , bool , int > ;
5
6
using unsafe HalGlobalFree = delegate * managed< int , void > ;
6
7
using unsafe HalIndexedCreate = delegate * managed< int , delegate * unmanaged[ Cdecl] < byte * , void * , WPIHal . HalValue * , void > , void * , bool , int > ;
9
10
10
11
namespace WPILib . Simulation ;
11
12
12
- public class CallbackStore : IDisposable
13
+ public delegate void NotifyCallback ( string name , HalValue value ) ;
14
+
15
+ public delegate void ConstBufferCallback ( string name , ReadOnlySpan < byte > buffer ) ;
16
+
17
+ public sealed class CallbackStore : IDisposable
13
18
{
14
19
private GCHandle delegateHandle ;
15
20
private readonly int nativeHandle ;
16
21
private readonly int ? index ;
17
- private unsafe readonly HalIndexedFree indexedFree ;
18
- private unsafe readonly HalGlobalFree globalFree ;
22
+ private readonly unsafe HalIndexedFree indexedFree ;
23
+ private readonly unsafe HalGlobalFree globalFree ;
19
24
20
25
[ UnmanagedCallersOnly ( CallConvs = [ typeof ( CallConvCdecl ) ] ) ]
21
26
public static unsafe void HalNotifyCallback ( byte * name , void * param , HalValue * value )
22
27
{
23
28
GCHandle handle = GCHandle . FromIntPtr ( ( nint ) param ) ;
24
- if ( handle . Target is Action < string , HalValue > stringCallback )
29
+ if ( handle . Target is NotifyCallback stringCallback )
25
30
{
26
31
string n = Marshal . PtrToStringUTF8 ( ( nint ) name ) ?? "" ;
27
32
stringCallback ( n , * value ) ;
28
33
}
29
34
}
30
35
36
+ [ UnmanagedCallersOnly ( CallConvs = [ typeof ( CallConvCdecl ) ] ) ]
37
+ public static unsafe void HalConstBufferCallback ( byte * name , void * param , byte * buffer , uint len )
38
+ {
39
+ GCHandle handle = GCHandle . FromIntPtr ( ( nint ) param ) ;
40
+ if ( handle . Target is ConstBufferCallback stringCallback )
41
+ {
42
+ string n = Marshal . PtrToStringUTF8 ( ( nint ) name ) ?? "" ;
43
+ stringCallback ( n , new ReadOnlySpan < byte > ( buffer , ( int ) len ) ) ;
44
+ }
45
+ }
46
+
31
47
public unsafe void Dispose ( )
32
48
{
33
49
GC . SuppressFinalize ( this ) ;
@@ -42,19 +58,27 @@ public unsafe void Dispose()
42
58
delegateHandle . Free ( ) ;
43
59
}
44
60
45
- public unsafe CallbackStore ( Action < string , HalValue > callback , bool immediateNotify , HalGlobalCreate create , HalGlobalFree free )
61
+ public unsafe CallbackStore ( NotifyCallback callback , bool immediateNotify , HalGlobalCreate create , HalGlobalFree free )
46
62
{
47
63
delegateHandle = GCHandle . Alloc ( callback ) ;
48
64
nativeHandle = create ( & HalNotifyCallback , ( void * ) ( nint ) delegateHandle , immediateNotify ) ;
49
65
index = null ;
50
66
globalFree = free ;
51
67
}
52
68
53
- public unsafe CallbackStore ( Action < string , HalValue > callback , int index , bool immediateNotify , HalIndexedCreate create , HalIndexedFree free )
69
+ public unsafe CallbackStore ( NotifyCallback callback , int index , bool immediateNotify , HalIndexedCreate create , HalIndexedFree free )
54
70
{
55
71
delegateHandle = GCHandle . Alloc ( callback ) ;
56
72
nativeHandle = create ( index , & HalNotifyCallback , ( void * ) ( nint ) delegateHandle , immediateNotify ) ;
57
73
this . index = index ;
58
74
indexedFree = free ;
59
75
}
76
+
77
+ public unsafe CallbackStore ( ConstBufferCallback callback , int index , HalConstBufferCreate create , HalIndexedFree free )
78
+ {
79
+ delegateHandle = GCHandle . Alloc ( callback ) ;
80
+ nativeHandle = create ( index , & HalConstBufferCallback , ( void * ) ( nint ) delegateHandle ) ;
81
+ this . index = index ;
82
+ indexedFree = free ;
83
+ }
60
84
}
0 commit comments