mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 00:41:02 +00:00
revise to make embedding in other classes easier
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@27865 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
aaca31acf2
commit
5ff1f5d2ca
2 changed files with 98 additions and 91 deletions
|
@ -25,32 +25,41 @@
|
|||
|
||||
#import "Foundation/NSPointerFunctions.h"
|
||||
|
||||
/* Declare a structure type to copy poinnnnnnter functions information
|
||||
* around easily.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
void* (*acquireFunction)(const void *item,
|
||||
NSUInteger (*size)(const void *item), BOOL shouldCopy);
|
||||
|
||||
NSString *(*descriptionFunction)(const void *item);
|
||||
|
||||
NSUInteger (*hashFunction)(const void *item,
|
||||
NSUInteger (*size)(const void *item));
|
||||
|
||||
BOOL (*isEqualFunction)(const void *item1, const void *item2,
|
||||
NSUInteger (*size)(const void *item));
|
||||
|
||||
void (*relinquishFunction)(const void *item,
|
||||
NSUInteger (*size)(const void *item));
|
||||
|
||||
NSUInteger (*sizeFunction)(const void *item);
|
||||
|
||||
BOOL shouldCopyIn;
|
||||
|
||||
BOOL usesStrongWriteBarrier;
|
||||
|
||||
BOOL usesWeakReadAndWriteBarriers;
|
||||
} PFInfo;
|
||||
|
||||
/* Declare the concrete pointer functions class as a wrapper around
|
||||
* an instance of the PFInfo structure.
|
||||
*/
|
||||
@interface NSConcretePointerFunctions : NSPointerFunctions
|
||||
{
|
||||
@public
|
||||
NSUInteger _options;
|
||||
|
||||
void* (*_acquireFunction)(const void *item,
|
||||
NSUInteger (*size)(const void *item), BOOL shouldCopy);
|
||||
|
||||
NSString *(*_descriptionFunction)(const void *item);
|
||||
|
||||
NSUInteger (*_hashFunction)(const void *item,
|
||||
NSUInteger (*size)(const void *item));
|
||||
|
||||
BOOL (*_isEqualFunction)(const void *item1, const void *item2,
|
||||
NSUInteger (*size)(const void *item));
|
||||
|
||||
void (*_relinquishFunction)(const void *item,
|
||||
NSUInteger (*size)(const void *item));
|
||||
|
||||
NSUInteger (*_sizeFunction)(const void *item);
|
||||
|
||||
BOOL _shouldCopyIn;
|
||||
|
||||
BOOL _usesStrongWriteBarrier;
|
||||
|
||||
BOOL _usesWeakReadAndWriteBarriers;
|
||||
PFInfo _x;
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -61,11 +70,11 @@
|
|||
/* Acquire the pointer value to store for the specified item.
|
||||
*/
|
||||
static inline void
|
||||
pointerFunctionsAcquire(NSConcretePointerFunctions *PF, void **dst, void *src)
|
||||
pointerFunctionsAcquire(PFInfo *PF, void **dst, void *src)
|
||||
{
|
||||
if (PF->_acquireFunction != 0)
|
||||
src = (*PF->_acquireFunction)(src, PF->_sizeFunction, PF->_shouldCopyIn);
|
||||
#if GS_WITH_GC
|
||||
if (PF->acquireFunction != 0)
|
||||
src = (*PF->acquireFunction)(src, PF->sizeFunction, PF->shouldCopyIn);
|
||||
#if GSWITHGC
|
||||
if (PF->usesWeakReadAndWriteBarriers)
|
||||
GSAssignZeroingWeakPointer(dst, src);
|
||||
else
|
||||
|
@ -77,10 +86,10 @@ pointerFunctionsAcquire(NSConcretePointerFunctions *PF, void **dst, void *src)
|
|||
/* Generate an NSString description of the item
|
||||
*/
|
||||
static inline NSString *
|
||||
pointerFunctionsDescribe(NSConcretePointerFunctions *PF, void *item)
|
||||
pointerFunctionsDescribe(PFInfo *PF, void *item)
|
||||
{
|
||||
if (PF->_descriptionFunction != 0)
|
||||
return (*PF->_descriptionFunction)(item);
|
||||
if (PF->descriptionFunction != 0)
|
||||
return (*PF->descriptionFunction)(item);
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
@ -88,10 +97,10 @@ pointerFunctionsDescribe(NSConcretePointerFunctions *PF, void *item)
|
|||
/* Generate the hash of the item
|
||||
*/
|
||||
static inline NSUInteger
|
||||
pointerFunctionsHash(NSConcretePointerFunctions *PF, void *item)
|
||||
pointerFunctionsHash(PFInfo *PF, void *item)
|
||||
{
|
||||
if (PF->_hashFunction != 0)
|
||||
return (*PF->_hashFunction)(item, PF->_sizeFunction);
|
||||
if (PF->hashFunction != 0)
|
||||
return (*PF->hashFunction)(item, PF->sizeFunction);
|
||||
return (NSUInteger)item;
|
||||
}
|
||||
|
||||
|
@ -99,10 +108,10 @@ pointerFunctionsHash(NSConcretePointerFunctions *PF, void *item)
|
|||
/* Compare two items for equality
|
||||
*/
|
||||
static inline BOOL
|
||||
pointerFunctionsEqual(NSConcretePointerFunctions *PF, void *item1, void *item2)
|
||||
pointerFunctionsEqual(PFInfo *PF, void *item1, void *item2)
|
||||
{
|
||||
if (PF->_isEqualFunction != 0)
|
||||
return (*PF->_isEqualFunction)(item1, item2, PF->_sizeFunction);
|
||||
if (PF->isEqualFunction != 0)
|
||||
return (*PF->isEqualFunction)(item1, item2, PF->sizeFunction);
|
||||
if (item1 == item2)
|
||||
return YES;
|
||||
return NO;
|
||||
|
@ -112,11 +121,11 @@ pointerFunctionsEqual(NSConcretePointerFunctions *PF, void *item1, void *item2)
|
|||
/* Relinquish the specified item and set it to zero.
|
||||
*/
|
||||
static inline void
|
||||
pointerFunctionsRelinquish(NSConcretePointerFunctions *PF, void **itemptr)
|
||||
pointerFunctionsRelinquish(PFInfo *PF, void **itemptr)
|
||||
{
|
||||
if (PF->_relinquishFunction != 0)
|
||||
(*PF->_relinquishFunction)(*itemptr, PF->_sizeFunction);
|
||||
if (PF->_usesWeakReadAndWriteBarriers)
|
||||
if (PF->relinquishFunction != 0)
|
||||
(*PF->relinquishFunction)(*itemptr, PF->sizeFunction);
|
||||
if (PF->usesWeakReadAndWriteBarriers)
|
||||
GSAssignZeroingWeakPointer(itemptr, (void*)0);
|
||||
else
|
||||
*itemptr = 0;
|
||||
|
|
|
@ -62,7 +62,7 @@ acquireExistingMemory(const void *item,
|
|||
static NSString*
|
||||
describeString(const void *item)
|
||||
{
|
||||
return AUTORELEASE([[NSString alloc] initWithUTF8String: item]);
|
||||
return [NSString stringWithFormat: @"%s", item];
|
||||
}
|
||||
|
||||
static NSString*
|
||||
|
@ -187,28 +187,26 @@ relinquishRetainedMemory(const void *item,
|
|||
|
||||
- (id) initWithOptions: (NSPointerFunctionsOptions)options
|
||||
{
|
||||
_options = options;
|
||||
|
||||
/* First we look at the memory management options to see which function
|
||||
* should be used to relinquish contents of a container with these
|
||||
* options.
|
||||
*/
|
||||
if (options & NSPointerFunctionsZeroingWeakMemory)
|
||||
{
|
||||
_relinquishFunction = 0;
|
||||
_usesWeakReadAndWriteBarriers = YES;
|
||||
_x.relinquishFunction = 0;
|
||||
_x.usesWeakReadAndWriteBarriers = YES;
|
||||
}
|
||||
else if (options & NSPointerFunctionsOpaqueMemory)
|
||||
{
|
||||
_relinquishFunction = 0;
|
||||
_x.relinquishFunction = 0;
|
||||
}
|
||||
else if (options & NSPointerFunctionsMallocMemory)
|
||||
{
|
||||
_relinquishFunction = relinquishMallocMemory;
|
||||
_x.relinquishFunction = relinquishMallocMemory;
|
||||
}
|
||||
else if (options & NSPointerFunctionsMachVirtualMemory)
|
||||
{
|
||||
_relinquishFunction = relinquishMallocMemory;
|
||||
_x.relinquishFunction = relinquishMallocMemory;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -216,58 +214,58 @@ relinquishRetainedMemory(const void *item,
|
|||
* so for these we set the usesStrongWriteBarrier flag to tell
|
||||
* containers to allocate scanned memory.
|
||||
*/
|
||||
_usesStrongWriteBarrier = YES;
|
||||
_relinquishFunction = relinquishRetainedMemory;
|
||||
_x.usesStrongWriteBarrier = YES;
|
||||
_x.relinquishFunction = relinquishRetainedMemory;
|
||||
}
|
||||
|
||||
if (options & NSPointerFunctionsCopyIn)
|
||||
{
|
||||
_shouldCopyIn = YES;
|
||||
_x.shouldCopyIn = YES;
|
||||
}
|
||||
|
||||
/* Now we look at the personality options to determine other functions.
|
||||
*/
|
||||
if (options & NSPointerFunctionsOpaquePersonality)
|
||||
{
|
||||
_acquireFunction = acquireExistingMemory;
|
||||
_descriptionFunction = describePointer;
|
||||
_hashFunction = hashShifted;
|
||||
_isEqualFunction = equalDirect;
|
||||
_x.acquireFunction = acquireExistingMemory;
|
||||
_x.descriptionFunction = describePointer;
|
||||
_x.hashFunction = hashShifted;
|
||||
_x.isEqualFunction = equalDirect;
|
||||
}
|
||||
else if (options & NSPointerFunctionsObjectPointerPersonality)
|
||||
{
|
||||
_acquireFunction = acquireRetainedObject;
|
||||
_descriptionFunction = describeObject;
|
||||
_hashFunction = hashShifted;
|
||||
_isEqualFunction = equalDirect;
|
||||
_x.acquireFunction = acquireRetainedObject;
|
||||
_x.descriptionFunction = describeObject;
|
||||
_x.hashFunction = hashShifted;
|
||||
_x.isEqualFunction = equalDirect;
|
||||
}
|
||||
else if (options & NSPointerFunctionsCStringPersonality)
|
||||
{
|
||||
_acquireFunction = acquireMallocMemory;
|
||||
_descriptionFunction = describeString;
|
||||
_hashFunction = hashString;
|
||||
_isEqualFunction = equalString;
|
||||
_x.acquireFunction = acquireMallocMemory;
|
||||
_x.descriptionFunction = describeString;
|
||||
_x.hashFunction = hashString;
|
||||
_x.isEqualFunction = equalString;
|
||||
}
|
||||
else if (options & NSPointerFunctionsStructPersonality)
|
||||
{
|
||||
_acquireFunction = acquireMallocMemory;
|
||||
_descriptionFunction = describePointer;
|
||||
_hashFunction = hashMemory;
|
||||
_isEqualFunction = equalMemory;
|
||||
_x.acquireFunction = acquireMallocMemory;
|
||||
_x.descriptionFunction = describePointer;
|
||||
_x.hashFunction = hashMemory;
|
||||
_x.isEqualFunction = equalMemory;
|
||||
}
|
||||
else if (options & NSPointerFunctionsIntegerPersonality)
|
||||
{
|
||||
_acquireFunction = acquireExistingMemory;
|
||||
_descriptionFunction = describeInteger;
|
||||
_hashFunction = hashDirect;
|
||||
_isEqualFunction = equalDirect;
|
||||
_x.acquireFunction = acquireExistingMemory;
|
||||
_x.descriptionFunction = describeInteger;
|
||||
_x.hashFunction = hashDirect;
|
||||
_x.isEqualFunction = equalDirect;
|
||||
}
|
||||
else /* objects */
|
||||
{
|
||||
_acquireFunction = acquireRetainedObject;
|
||||
_descriptionFunction = describeObject;
|
||||
_hashFunction = hashObject;
|
||||
_isEqualFunction = equalObject;
|
||||
_x.acquireFunction = acquireRetainedObject;
|
||||
_x.descriptionFunction = describeObject;
|
||||
_x.hashFunction = hashObject;
|
||||
_x.isEqualFunction = equalObject;
|
||||
}
|
||||
|
||||
return self;
|
||||
|
@ -276,89 +274,89 @@ relinquishRetainedMemory(const void *item,
|
|||
- (void* (*)(const void *item,
|
||||
NSUInteger (*size)(const void *item), BOOL shouldCopy)) acquireFunction
|
||||
{
|
||||
return _acquireFunction;
|
||||
return _x.acquireFunction;
|
||||
}
|
||||
|
||||
- (NSString *(*)(const void *item)) descriptionFunction
|
||||
{
|
||||
return _descriptionFunction;
|
||||
return _x.descriptionFunction;
|
||||
}
|
||||
|
||||
- (NSUInteger (*)(const void *item,
|
||||
NSUInteger (*size)(const void *item))) hashFunction
|
||||
{
|
||||
return _hashFunction;
|
||||
return _x.hashFunction;
|
||||
}
|
||||
|
||||
- (BOOL (*)(const void *item1, const void *item2,
|
||||
NSUInteger (*size)(const void *item))) isEqualFunction
|
||||
{
|
||||
return _isEqualFunction;
|
||||
return _x.isEqualFunction;
|
||||
}
|
||||
|
||||
- (void (*)(const void *item,
|
||||
NSUInteger (*size)(const void *item))) relinquishFunction
|
||||
{
|
||||
return _relinquishFunction;
|
||||
return _x.relinquishFunction;
|
||||
}
|
||||
|
||||
- (void) setAcquireFunction: (void* (*)(const void *item,
|
||||
NSUInteger (*size)(const void *item), BOOL shouldCopy))func
|
||||
{
|
||||
_acquireFunction = func;
|
||||
_x.acquireFunction = func;
|
||||
}
|
||||
|
||||
- (void) setDescriptionFunction: (NSString *(*)(const void *item))func
|
||||
{
|
||||
_descriptionFunction = func;
|
||||
_x.descriptionFunction = func;
|
||||
}
|
||||
|
||||
- (void) setHashFunction: (NSUInteger (*)(const void *item,
|
||||
NSUInteger (*size)(const void *item)))func
|
||||
{
|
||||
_hashFunction = func;
|
||||
_x.hashFunction = func;
|
||||
}
|
||||
|
||||
- (void) setIsEqualFunction: (BOOL (*)(const void *item1, const void *item2,
|
||||
NSUInteger (*size)(const void *item)))func
|
||||
{
|
||||
_isEqualFunction = func;
|
||||
_x.isEqualFunction = func;
|
||||
}
|
||||
|
||||
- (void) setRelinquishFunction: (void (*)(const void *item,
|
||||
NSUInteger (*size)(const void *item))) func
|
||||
{
|
||||
_relinquishFunction = func;
|
||||
_x.relinquishFunction = func;
|
||||
}
|
||||
|
||||
- (void) setSizeFunction: (NSUInteger (*)(const void *item))func
|
||||
{
|
||||
_sizeFunction = func;
|
||||
_x.sizeFunction = func;
|
||||
}
|
||||
|
||||
- (void) setUsesStrongWriteBarrier: (BOOL)flag
|
||||
{
|
||||
_usesStrongWriteBarrier = flag;
|
||||
_x.usesStrongWriteBarrier = flag;
|
||||
}
|
||||
|
||||
- (void) setUsesWeakReadAndWriteBarriers: (BOOL)flag
|
||||
{
|
||||
_usesWeakReadAndWriteBarriers = flag;
|
||||
_x.usesWeakReadAndWriteBarriers = flag;
|
||||
}
|
||||
|
||||
- (NSUInteger (*)(const void *item)) sizeFunction
|
||||
{
|
||||
return _sizeFunction;
|
||||
return _x.sizeFunction;
|
||||
}
|
||||
|
||||
- (BOOL) usesStrongWriteBarrier
|
||||
{
|
||||
return _usesStrongWriteBarrier;
|
||||
return _x.usesStrongWriteBarrier;
|
||||
}
|
||||
|
||||
- (BOOL) usesWeakReadAndWriteBarriers
|
||||
{
|
||||
return _usesStrongWriteBarrier;
|
||||
return _x.usesStrongWriteBarrier;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in a new issue