www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - need help to use C++ callback from garnet

reply Dakota <dakota gmail.com> writes:
I try use 
https://github.com/microsoft/garnet/blob/main/libs/storage/Tsavorite/cc/src/device/nati
e_device_wrapper.cc from D


Not sure how to make this work with D:

```c++
	EXPORTED_SYMBOL FASTER::core::Status 
NativeDevice_ReadAsync(NativeDevice* device, uint64_t source, 
void* dest, uint32_t length, FASTER::core::AsyncIOCallback 
callback, void* context) {
		return device->ReadAsync(source, dest, length, callback, 
context);
	}

	EXPORTED_SYMBOL FASTER::core::Status 
NativeDevice_WriteAsync(NativeDevice* device, const void* source, 
uint64_t dest, uint32_t length, FASTER::core::AsyncIOCallback 
callback, void* context) {
		return device->WriteAsync(source, dest, length, callback, 
context);
	}
```

I need to define `FASTER::core::AsyncIOCallback callback` from D, 
any way to workaround ? (like modify garnet source code to 
support pass a C function callback)

I am newbie to C++ and D, any help will be appreciate
May 29
parent reply evilrat <evilrat666 gmail.com> writes:
On Wednesday, 29 May 2024 at 07:47:01 UTC, Dakota wrote:
 I try use 
 https://github.com/microsoft/garnet/blob/main/libs/storage/Tsavorite/cc/src/device/nati
e_device_wrapper.cc from D


 Not sure how to make this work with D:

 ```c++
 	EXPORTED_SYMBOL FASTER::core::Status 
 NativeDevice_ReadAsync(NativeDevice* device, uint64_t source, 
 void* dest, uint32_t length, FASTER::core::AsyncIOCallback 
 callback, void* context) {
 		return device->ReadAsync(source, dest, length, callback, 
 context);
 	}

 	EXPORTED_SYMBOL FASTER::core::Status 
 NativeDevice_WriteAsync(NativeDevice* device, const void* 
 source, uint64_t dest, uint32_t length, 
 FASTER::core::AsyncIOCallback callback, void* context) {
 		return device->WriteAsync(source, dest, length, callback, 
 context);
 	}
 ```

 I need to define `FASTER::core::AsyncIOCallback callback` from 
 D, any way to workaround ? (like modify garnet source code to 
 support pass a C function callback)

 I am newbie to C++ and D, any help will be appreciate
(here is the signature of callback) https://github.com/microsoft/garnet/blob/ade2991f3737b9b5e3151d0dd0b614adfd4bcecd/libs/storage/Tsavorite/cc/src/device/async.h#L25 ```cpp typedef void(*AsyncIOCallback)(IAsyncContext* context, Status result, size_t bytes_transferred); ``` This can be written as following to match namespace and mangling scheme, assuming IAsyncContext is opaque pointer here, though I don't remember if enum need namespace as well. But no guarantess anyways, try it and adapt it. Also I can't remember if string list makes proper namespace or you should put `FASTER.core` instead (without quotes). ```d enum Status : ubyte { Ok = 0, Pending = 1, NotFound = 2, OutOfMemory = 3, IOError = 4, Corruption = 5, Aborted = 6, } extern(C++, "FASTER", "core") class IAsyncContext; alias AsyncIOCallback = extern(C++, "FASTER", "core") void function(IAsyncContext context, Status result, size_t bytes_transferred); ```
May 29
parent Dakota <dakota gmail.com> writes:
On Wednesday, 29 May 2024 at 09:01:13 UTC, evilrat wrote:
 On Wednesday, 29 May 2024 at 07:47:01 UTC, Dakota wrote:
 [...]
(here is the signature of callback) https://github.com/microsoft/garnet/blob/ade2991f3737b9b5e3151d0dd0b614adfd4bcecd/libs/storage/Tsavorite/cc/src/device/async.h#L25 [...]
Thanks for the tips.
Jun 04