December 11, 2004 Re: Weak references | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo | >> I had to do this for a few things in DFL, like menus. I accomplished it by storing the reference in malloc'd memory.
>
> I am very interested in this. Where can I find the details?
>
Something like this. It's just an idea, it won't compile as-is:
class Item // Item you want weak references to.
{
this()
{
addItem(this);
}
~this()
{
removeItem(this);
}
}
Item[] refItems; // malloc'd memory holding weak references to Item`s.
void addItem(Item item)
{
refItems = realloc(refItems, count * Item.sizeof);
refItems[i] = item;
}
void removeItem(Item item)
{
int i;
for(i = 0; i != refItems.length; i++)
{
if(item == refItems[i])
{
refItems = realloc(refItems, count * Item.sizeof);
break;
}
}
}
|
Copyright © 1999-2021 by the D Language Foundation