HDFGroup / HDFGroup/HDF.PInvoke
Emulate C++'s typedefs with structs
- Dominant language
- C#
- Stars
- 87
- Forks
- 31
- PR merge metrics
- No merged PRs in 30d
Description
At the current state e.g. the functions `H5F.create( .. )` and `H5F.get_filesize( .. )` both return `Int32` even though `create` returns an actual id and `get_filesize` only indicates if there was an error. So the real meanings are lost in C# when using IntelliSense.


I suggest to wrap the values into structs to create marshable wrapper types to preserve the semantics that are carried by aliases (C++ `typedef`s).
So instead of adding a lot `using`s on top of every file:
```C#
using herr_t = System.Int32;
#if HDF5_VER1_10
using hid_t = System.Int64;
#else
using hid_t = System.Int32;
#endif
```
I would add these types ONCE into the library:
```C#
public struct herr_t
{
public System.Int32 Value;
public static implicit operator System.Int32(herr_t value) => value.Value;
}
public struct hid_t
{
#if HDF5_VER1_10
public System.Int64 Value;
public static implicit operator System.Int64( hid_t value ) => value.Value;
#else
public System.Int32 Value;
public static implicit operator System.Int32(hid_t value) => value.Value;
#endif
}
```
This allows would lead to much more readable IntelliSense hints and enables the use of ad-hoc polymorphism like:
```C#
public static class HdfPInvokeHelpers
{
public static void ThrowOnError(this herr_t value)
{
if (value < 0)
throw new Exception(H5EHelper.GetErrorStack().ToString());
}
public static hid_t ThrowOnError(this hid_t value)
{
if (value < 0)
throw new Exception(H5EHelper.GetErrorStack().ToString());
return value;
}
}
class Program
{
static void Main(string[] args)
{
var file = H5F.create("file.h5", H5F.ACC_RDONLY, H5P.DEFAULT, H5P.DEFAULT).ThrowOnError();
ulong size;
H5F.get_filesize(file, ref size).ThrowOnError();
}
}
```
### Update
Missing cross-define for `hid_t` added.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.