digitalmars.D.learn - weak_ptr alternative
- Kamil Koczurek (11/11) Aug 05 2018 Is there a way to keep track of objects without owning them? That
- aliak (5/17) Aug 05 2018 Not sure if there's anything in the language or standard library,
- Kamil Koczurek (7/26) Aug 05 2018 Not exactly what I want, in my case the pointer should become
Is there a way to keep track of objects without owning them? That is, could I have a smart pointer that behaves somewhat like this: WeakPtr!Class wptr = getSomeInstance(); auto obj = wptr.peek; //[1] if(obj !is null) { obj.stuff(); } [1]: If wptr points to something that is still reachable from somewhere else (and wasn't yet marked for collection by GC), .peek would return a normal reference of type Class. OTOH, when no active reference is present it should return null.
Aug 05 2018
On Sunday, 5 August 2018 at 20:10:49 UTC, Kamil Koczurek wrote:Is there a way to keep track of objects without owning them? That is, could I have a smart pointer that behaves somewhat like this: WeakPtr!Class wptr = getSomeInstance(); auto obj = wptr.peek; //[1] if(obj !is null) { obj.stuff(); } [1]: If wptr points to something that is still reachable from somewhere else (and wasn't yet marked for collection by GC), .peek would return a normal reference of type Class. OTOH, when no active reference is present it should return null.Not sure if there's anything in the language or standard library, but there this: https://github.com/w0rp/dstruct/blob/master/source/dstruct/weak_reference.d Which admittedly I have not tested.
Aug 05 2018
On Sunday, 5 August 2018 at 20:57:32 UTC, aliak wrote:On Sunday, 5 August 2018 at 20:10:49 UTC, Kamil Koczurek wrote:Not exactly what I want, in my case the pointer should become null when all other instances go out of reach, not when they're collected, which isn't really predictable in D AFAIK. I can't base logic of my program on this. Still interesting though, I'll look into its implementation and docs on core.memory, maybe I'll manage to implement it by myself.Is there a way to keep track of objects without owning them? That is, could I have a smart pointer that behaves somewhat like this: WeakPtr!Class wptr = getSomeInstance(); auto obj = wptr.peek; //[1] if(obj !is null) { obj.stuff(); } [1]: If wptr points to something that is still reachable from somewhere else (and wasn't yet marked for collection by GC), .peek would return a normal reference of type Class. OTOH, when no active reference is present it should return null.Not sure if there's anything in the language or standard library, but there this: https://github.com/w0rp/dstruct/blob/master/source/dstruct/weak_reference.d Which admittedly I have not tested.
Aug 05 2018