scope(~this)
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 12 22:18:18 PDT 2017
On Sunday, 12 March 2017 at 21:38:44 UTC, Inquie wrote:
> Is there any easy way to create a scope for termination of the
> object?
>
> I have a template method that takes a type and allocates and
> deallocates based on that type.
>
> class bar
> {
> void foo(T)()
> {
> T x;
> alloc(x);
> scope(~this) dealloc(x); // hypothetical that wraps the
> statement in a lambda and deallocates in the destructor
>
> ... x must stay allocated until class instance
> termination(has to do with COM, can't release it in foo)
> }
>
> }
>
> Now, x cannot be a field because T is unknown(i suppose I could
> use object, void*, etc, but...).
>
If it is COM then you should use IUnknown (the COM root
interface),or if you are expecting multiple calls to foo, an
array of IUnknown. I think the GC will clean up completely for
you in either case and call release(?) on the member(s).
Also as it is COM you probably don't need to template it, just
choose T to be the most recent ancestor of all (old) T's you
would be expecting foo to be instantiated with (e.g. IUnknown if
you expect any COM object.
More information about the Digitalmars-d-learn
mailing list