Allow the class of the links in a link store to be controlled

This commit is contained in:
Richard Frith-Macdonald 2021-05-22 12:27:45 +01:00
parent 3b16cbb283
commit ab83081490
2 changed files with 39 additions and 8 deletions

View file

@ -326,9 +326,16 @@ GSLinkedListRemove(GSListLink *link, GSLinkedList *list);
@interface GSLinkStore : GSLinkedList
{
@public
GSListLink *free; /** The unused links */
Class linkClass; /** The class used for links */
GSListLink *free; /** The unused links */
}
/** Creates an instance of a store to be used to create links using the
* specified class (must be a subclass of GSListLink). If the class is
* nil then GSListLink is used.
*/
+ (GSLinkStore*) storeFor: (Class)theLinkClass;
/** Adds an object at the tail of the list (calls -insertObject:after:),
* making it the last object in the list.<br />
* Returns the list link that the object is stored in.

View file

@ -498,6 +498,21 @@ GSLinkedListMoveToTail(GSListLink *link, GSLinkedList *list)
@implementation GSLinkStore
+ (GSLinkStore*) storeFor: (Class)theLinkClass
{
Class c = [GSListLink class];
GSLinkStore *s;
if (Nil == theLinkClass)
{
theLinkClass = c;
}
NSAssert([theLinkClass isSubclassOfClass: c], NSInvalidArgumentException);
s = [self new];
s->linkClass = c;
return AUTORELEASE(s);
}
- (GSListLink*) addObject: (id)anObject
{
return GSLinkStoreInsertObjectAfter(anObject, self, tail);
@ -507,7 +522,7 @@ GSLinkedListMoveToTail(GSListLink *link, GSLinkedList *list)
{
[self empty];
[self purge];
[super dealloc];
DEALLOC
}
- (void) empty
@ -523,6 +538,15 @@ GSLinkedListMoveToTail(GSListLink *link, GSLinkedList *list)
return GSLinkedListFirstObject(self);
}
- (id) init
{
if (nil != (self = [super init]))
{
linkClass = [GSListLink class];
}
return self;
}
- (GSListLink*) insertObject: (id)anObject after: (GSListLink*)at
{
return GSLinkStoreInsertObjectAfter(anObject, self, at);
@ -546,7 +570,7 @@ GSLinkedListMoveToTail(GSListLink *link, GSLinkedList *list)
free = link->next;
link->next = nil;
[link release];
RELEASE(link);
}
}
@ -598,14 +622,14 @@ GSLinkStoreInsertObjectAfter(
if (nil == link)
{
link = [GSListLink new];
link = [list->linkClass new];
}
else
{
list->free = link->next;
link->next = nil;
}
link->item = [anObject retain];
link->item = RETAIN(anObject);
GSLinkedListInsertAfter(link, list, (nil == at) ? list->tail : at);
return link;
}
@ -618,14 +642,14 @@ GSLinkStoreInsertObjectBefore(
if (nil == link)
{
link = [GSListLink new];
link = [list->linkClass new];
}
else
{
list->free = link->next;
link->next = nil;
}
link->item = [anObject retain];
link->item = RETAIN(anObject);
GSLinkedListInsertBefore(link, list, (nil == at) ? list->head : at);
return link;
}
@ -636,7 +660,7 @@ GSLinkStoreRemoveObjectAt(GSLinkStore *list, GSListLink *at)
if (nil != at && at->owner == list)
{
GSLinkedListRemove(at, list);
[at->item release];
RELEASE(at->item);
at->item = nil;
at->next = list->free;
list->free = at;