privateWeakReference_weak;// the reference itself.
publicvoidNewTarget(inttag,objecttarget){
Debug.Assert(!HasTarget,"Entry already has a valid target");
Debug.Assert(tag!=0,"Bad tag");
Debug.Assert(target!=null,"Invalid target");
if(_weak==null){
_weak=newWeakReference(target,false);
}
else{
_weak.Target=target;
}
_tag=tag;
}
publicvoidRemoveTarget(){
_tag=0;
}
publicboolHasTarget{
get{
return((_tag!=0)&&(_weak.IsAlive));
}
}
publicintTag{
get{
return_tag;
}
}
publicobjectTarget{
get{
return(_tag==0?null:_weak.Target);
}
}
}
privateconstintLockPollTime=100;// Time to wait (in ms) between attempting to get the _itemLock
privateconstintDefaultCollectionSize=20;// Default size for the collection, and the amount to grow everytime the collection is full
privateCollectionEntry[]_items;// The collection of items we are keeping track of
privatereadonlyobject_itemLock;// Used to synchronize access to the _items collection
privateint_optimisticCount;// (#ItemsAdded - #ItemsRemoved) - This estimates the number of items that we *should* have (but doesn't take into account item targets being GC'd)
privateint_lastItemIndex;// Location of the last item in _items
privatevolatilebool_isNotifying;// Indicates that the collection is currently being notified (and, therefore, about to be cleared)
protectedDbReferenceCollection(){
_items=newCollectionEntry[DefaultCollectionSize];
_itemLock=newobject();
_optimisticCount=0;
_lastItemIndex=0;
}
abstractpublicvoidAdd(objectvalue,inttag);
protectedvoidAddItem(objectvalue,inttag){
Debug.Assert(null!=value&&0!=tag,"AddItem with null value or 0 tag");