A naive attempt at a refcounted class proxy
aldanor via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 13 08:17:51 PST 2015
This discussion:
http://forum.dlang.org/thread/bqtcdpsopxmnfbjyrrzf@forum.dlang.org
-- led me wondering if it would be possible to create some
crippled version of a class proxy that is based on RefCounted and
came up with something like this:
struct Box(T) if (is(T == class)) {
@disable this();
this(Args...)(Args args) {
_payload._refCounted.initialize(new T(args));
}
private {
struct _Box(T) {
private T _instance;
~this() {
destroy(_instance);
}
}
RefCounted!(_Box!T) _payload;
}
~this() {
}
auto opDispatch(string name, Args...)(Args args) {
return mixin("_payload._instance.%s(args)".format(name));
}
}
which lets you create Box!SomeClass(args) and it will be
refcounted unless you escape references and do other weird stuff.
It actually sort of seems to work at first glance, at least it
seems like it does... But with my D experience being fairly
limited I wonder what the potential pitfalls would be?
Full source code with example and stdout:
https://gist.github.com/aldanor/d5fb5e45ddf3dd2cb642
More information about the Digitalmars-d-learn
mailing list