need help to use C++ callback from garnet

evilrat evilrat666 at gmail.com
Wed May 29 09:01:13 UTC 2024


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/native_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);
```


More information about the Digitalmars-d-learn mailing list