Re: Inserting a custom class into class hierarchy?
Re: Inserting a custom class into class hierarchy?
- Subject: Re: Inserting a custom class into class hierarchy?
- From: Shaun Wexler <email@hidden>
- Date: Sat, 4 Dec 2004 16:49:36 -0800
On Dec 3, 2004, at 3:38 PM, The Karl Adam wrote:
Add a poseAsClass to your MyObject to have it pose as NSObject,
however be forewarned that you then need to make sure your app doesn't
explode, the world doesn't end and you didn't tear a hole in the space
time continuum. All that said:
P.S. MAKE SURE MYOBJECT DOES INHERIT FROM NSObject ELSE ALL HELL WILL
BREAK LOOSE
#define POSED_CLASS NSView
@interface ExtendedObject : POSED_CLASS
- (unsigned int *)objectFlags;
- (void *)objectContextInfo;
- (void)setObjectContextInfo:(void *)contextInfo;
@end
@implementation ExtendedObject
typedef struct PrependedIvars {
int refcount;
unsigned int flags;
void *contextInfo;
void *properties;
} PrependedIvars;
#define prependedIvars ((PrependedIvars *)((void *)self -
sizeof(PrependedIvars)))
+ (void)load
{
[self poseAsClass:[POSED_CLASS class]];
}
+ (id)allocWithZone:(NSZone *)zone
{
unsigned int minLength = ((Class)self)->instance_size +
sizeof(PrependedIvars);
void *pointer;
if (NULL == zone) {
zone = NSDefaultMallocZone();
}
if ((pointer = NSZoneCalloc(zone, (minLength + 15) >> 4, 16))) {
id object = pointer + sizeof(PrependedIvars);
*(Class *)object = self; // object->isa = class;
return object;
}
return nil;
}
- (id)retain
{
int *ivar = &prependedIvars->refcount;
int refcount;
__asm__ volatile ("1: lwarx %[refcount], 0, %[ivar]\n"
" addic %[refcount], %[refcount], 1\n"
" stwcx. %[refcount], 0, %[ivar]\n"
" bne- 1b\n"
: [refcount] "=&r" (refcount)
: [ivar] "r" (ivar)
: "cc", "memory"
);
if (refcount == 0) {
NSString *format = @"<%p> %@ inline refcount over max";
[NSException raise:NSInternalInconsistencyException format:format,
self, [self class]];
}
return self;
}
- (void)release
{
int *ivar = &prependedIvars->refcount;
int refcount;
__asm__ volatile ("1: lwarx %[refcount], 0, %[ivar]\n"
" subic %[refcount], %[refcount], 1\n"
" stwcx. %[refcount], 0, %[ivar]\n"
" bne- 1b\n"
: [refcount] "=&r" (refcount)
: [ivar] "r" (ivar)
: "cc", "memory"
);
if (refcount == -1) {
[self dealloc];
}
}
- (unsigned)retainCount
{
return prependedIvars->refcount + 1; // not thread safe
}
- (void)dealloc
{
isa = Nil;
NSZoneFree(NSZoneFromPointer(prependedIvars), prependedIvars);
}
- (unsigned int *)objectFlags
{
return &prependedIvars->flags; // use for atomic bitfield access
}
- (void *)objectContextInfo
{
return prependedIvars->contextInfo;
}
- (void)setObjectContextInfo:(void *)contextInfo
{
prependedIvars->contextInfo = contextInfo;
}
@end
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden