Emulate C++'s typedefs with structs

Open
#98 29 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
api

Research direction

Start by tracing the H5F.create and H5F.get_filesize declarations and their current Int32 return types. Review how version-dependent hid_t values are represented across the P/Invoke API, then determine the affected callers and tests. Done means distinct marshable wrapper types preserve the return semantics and hid_t width while improving IntelliSense.

Written by the indexing model from the issue text.

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.

image
image

I suggest to wrap the values into structs to create marshable wrapper types to preserve the semantics that are carried by aliases (C++ typedefs).

So instead of adding a lot usings on top of every file:

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:

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:

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.

Dominant language
C#
Stars
87
Forks
32
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from HDFGroup/HDF.PInvoke

All issues in HDFGroup/HDF.PInvoke

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.