www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Workaround for RefCounted with classes would work?

reply Bienlein <jeti789 web.de> writes:
Hello,

RefCounted so far works well with structs, but not with classes 
(see http://wiki.dlang.org/DIP74). I have now set up a little 
kludge where a struct takes a class as a template parameter. The 
struct with the containing object, which is of the type of the 
struct's template type, is handed over to the RefCounted 
instance. When RefCounted calls the destructor of the contained 
struct, the destructor of the object contained by the struct is 
also called.

I have tried this out and it works fine. It seems to be good 
enough for my purposes. Question is whether there is some hidden 
problem with this approach. Anything I need to be aware of? I'm 
not proficient with D. So I thought it's better to ask ;-).

Regards, Bienlein
Jun 19 2016
parent cym13 <cpicard openmailbox.org> writes:
On Sunday, 19 June 2016 at 19:39:05 UTC, Bienlein wrote:
 Hello,

 RefCounted so far works well with structs, but not with classes 
 (see http://wiki.dlang.org/DIP74). I have now set up a little 
 kludge where a struct takes a class as a template parameter. 
 The struct with the containing object, which is of the type of 
 the struct's template type, is handed over to the RefCounted 
 instance. When RefCounted calls the destructor of the contained 
 struct, the destructor of the object contained by the struct is 
 also called.

 I have tried this out and it works fine. It seems to be good 
 enough for my purposes. Question is whether there is some 
 hidden problem with this approach. Anything I need to be aware 
 of? I'm not proficient with D. So I thought it's better to ask 
 ;-).

 Regards, Bienlein
The idea of encapsulating a class in a struct is known, good, and it's all already in the standard library : have a look at std.typecons.scoped.
Jun 19 2016