DIP74: Reference Counted Class Objects

Volodymyr via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 4 05:08:03 PST 2015


Implicit conversion to supertypes (class or interface) is allowed
ONLY if the supertype is also a reference counted type. It
follows that reference counted types cannot be converted to
Object (unless Object itself defines the two methods).

But how about calling methods of supertypes?

class Basic
{
Basic castToBasic() return
{
      return this;
}

}

class RCWidged : Basic
{
void opAddRef();
void opRelease();
// etc
}

Object a;
{
auto b = RCWidget;
a = b.castToBasic; // unsafe? opAddReff?
// b.opRelease?
}

a < new RCWidget // call to Object.opCmp. Error?

The problem with shared_ptr-like solution - "this" parameter is
not shared_ptr. You try to build shared_ptr into "this". But
basic class knows nothing about RC in child class. And those who
use the basic class know nothing.

RefCounter is unsafe and mutable. Let's do it universal (for
classes/value structs/ and unions), add method
auto opShareResource(T)(ref T t)
to handle parts of RC class that go away.

auto opShareResource(T)(ref T t)
{
      return RefCounter!T(t, ownerRefCounter);
}

this method will be called for direct or indirect access to
fields of RCClass and may wrap resource or throw or do anything
else.


More information about the Digitalmars-d mailing list