Windows bindings
Steven Schveighoffer
schveiguy at gmail.com
Wed Feb 17 14:49:58 UTC 2021
On 2/16/21 4:48 PM, Elronnd wrote:
> On Tuesday, 16 February 2021 at 21:35:32 UTC, Elronnd wrote:
>> On Saturday, 13 February 2021 at 00:32:30 UTC, Rumbu wrote:
>>> 4) On RAIFree attribute
>> struct RaiiFreer(T, alias f) ...
>
> On Saturday, 13 February 2021 at 16:01:26 UTC, Steven Schveighoffer wrote:
>> Don't do this, because it will close on copying too.
>>
>> In order to *properly* implement this, it would need reference counting.
>
>
> It's easy to get double frees with that solution--right. Can disable
> copy constructor, or make it a class. I would go for the former, as
> it's more flexible: if you need ref semantics, just make a pointer or
> use phobos refcount.
Yes, this is a good solution -- it just alters the semantics of the
function return.
>
> I disagree that you _need_ refcounting to get it right, though.
> Providing a destructor makes it a more native interface. Manually
> destroying is something you would have had to do anyway, but now you can
> destroy the object the same as you would any other.
It's fine as long as it isn't the true return type. As long as you want
the same *semantics* as the original type, it needs to be copyable.
I'd say, leave the type itself as the return value, add an attribute
that identifies how to free it, and then provide a wrapper that does
what you want based on introspecting the attribute.
e.g.:
@freeFunction!CloseDC struct HDC { ... normal contents }
HDC someWindowsFunc(...);
auto autoFree(T)(T val) if (hasUDA!(freeFunction, T)) {
return RAIIWrapper!(extractFreeFunction!T)(val);
}
auto v = someWindowsFunc(...).autoFree;
-Steve
More information about the Digitalmars-d
mailing list