Re: NSCopyObject
Re: NSCopyObject
- Subject: Re: NSCopyObject
- From: Thomas Lachand-Robert <email@hidden>
- Date: Fri, 26 Apr 2002 08:45:44 +0200
This works... sort of. Now you need to allocate with an unusual syntax:
LCVMatrix *m = [[LCVMatrix allocWithDims:mydims] init];
This is definitely not a good idea. Some other people on the list may have
additional comments, but it is obvious that you will shoot yourself in the
feet sooner or later by using the standard alloc/init sequence. If not
tomorrow, then in a few days or weeks. Not adhering to the standard
paradigm of the programming language is almost always a bad thing.
If you really need to put the floats in the same struct (I still don't
understand why), then use NSFoundation way. Here is a somehow simplified
version, that is probably enough for your case:
@implementation LCVMatrix
LCVMatrix* _dummyPlaceholderForMatrix; // these both are global variable
for the class
NSZone* _zoneToAllocateIn;
+(id) allocWithZone:(NSZone*) zone {
if (! _dummyPlaceholderForMatrix)
_dummyPlaceholderForMatrix = NSAllocateObject(self, 0, zone);
_zoneToAllocateIn = zone;
return _dummyPlaceholderForMatrix;
}
-(id) initWithRows:(unsigned)r columns:(unsigned)c {
if (self == _dummyPlaceholderForMatrix) {
self = NSAllocateObject([self class], sizeof(float) * r *c,
_zoneToAllocateIn);
}
else /* this is probably an error, so catch it */;
rows = r;
columns = c;
return self;
}
-(id) init {// you must override it, at least to catch accidental use of
init
return [self initWithRows:0 columns:0];
}
NSFoundation does that slightly better by using an auxiliary class instead
of _dummyPlaceholderForMatrix, and allocating an object of this class for
each required zone. But this scheme works for me, and allocating an object
is done the standard way:
LCVMatrix *m = [[LCVMatrix alloc] initWithRows:5 columns:8];
Note that it assumes that alloc is never separated from init, which is
standard. In a multithreaded application, that could be a problem, but in
the worst case the zone will be incorrect.
Le vendredi 26 avril 2002, ` 01:05 , Robert Lee Dotson a icrit :
What I ended up doing was this:
typedef struct {
unsigned int width;
unsigned int height;
} LCVMatrixDims;
@interface LCVMatrix : NSObject {
unsigned int rows;
unsigned int columns;
#ifdef __VEC__
vector float values[];
#else
float values[];
#endif
}
+(id)allocWithDims:(LCVMatrixDims)dims {
return NSAllocateObject(self, sizeof(float) * dims.width *
dims.height, NULL);
}
- -(LCVMatrix *)initZeros {
int i;
for (i = 0; i < sizeof(values); i++) values[i] = 0.0;
return self;
}
It compiles, but i'm not sure if it will work. I've implemented the alloc,
allocWithZone, and init methods to return nil, instead, you have to send
specific dimensions in order to initialize the data in the array.
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.