Windows bindings

Steven Schveighoffer schveiguy at gmail.com
Sat Feb 13 16:01:26 UTC 2021


On 2/13/21 4:40 AM, Rumbu wrote:
> On Saturday, 13 February 2021 at 08:25:21 UTC, Jonathan Marler wrote:

>>
>> One thing you could do is define a common method that calls it:
>>
>> struct HDC
>> {
>>     auto free()
>>     {
>>         return CloseDC(this.value);
>>     }
>> }
>>
>> struct HANDLE
>> {
>>     auto free()
>>     {
>>         return CloseHandle(this.value);
>>     }
>> }
> 
> Excellent idea! I can take it further, why not a destructor?
> 
> struct HDC
> {
>    ~this()
>    {
>      CloseDC(this.value);
>    }
> }

Don't do this, because it will close on copying too.

In order to *properly* implement this, it would need reference counting.

A nice feature would be to implement a common method (like free), and 
then provide a reference-counting wrapper that would then call the 
appropriate thing when refcount is 0.

Another possibility is to define a freeFunction template that can be 
applied as an attribute:

template freeFunction(alias f)
{
     alias fn = f;
}

@freeFunction!CloseDC struct HDC ...

And then an appropriate free function can be called when the UDA is 
detected.

-Steve


More information about the Digitalmars-d mailing list