• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Inserting a custom class into class hierarchy?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inserting a custom class into class hierarchy?


  • Subject: Re: Inserting a custom class into class hierarchy?
  • From: Shaun Wexler <email@hidden>
  • Date: Fri, 3 Dec 2004 15:11:08 -0800

On Dec 3, 2004, at 10:33 AM, Alex Majora wrote:

I am on a very large project, so I've established a common custom class, MyObject, inserted beneath NSObject for all of our objects that inherit directly from NSObject; In this way, we can add all of our common functionality and instance variables there, so there's no duplication amongst the various classes.

So far so good.

However, for objects that necessarily must subclass from GUI elements like NSImageView, NSQuickDrawView, etc., I don't see a way to do this.

In general, I'm not a fan of multiple inheritance, but it would be handy in this case. Simply adding a category to NSObject doesn't allow us to add instance variables, so that's out. I don't see any way to "insert" a custom class into an established hierarchy. And I certainly don't want to replace NSObject, for obvious reasons. Any ideas?

malloc granularity dictates that pointers are returned in multiples of 16 bytes, which are aligned to 16-byte addresses, thus the low 4 bits are always clear for object pointers ("id"). All you need to do is decide how many bytes of private object storage you need, round that up to a multiple of 16 bytes, and override NSObject +allocWithZone: to alloc memory with the increased length which is >= 32. Set the object's isa pointer to the first 16-byte address beyond your custom ivar's, and write custom accessor methods for the hidden ivar's. Everything else should be automatic, and transparent to the rest of Cocoa; CF-bridged objects won't be allocated this way, though; you'll have to override the default CF allocator as well.


If you enforce 32-byte alignment and granularity of your objects (which I do, but only in my base SKWObject subclass of NSObject) then you'll also enjoy a speed enhancement on G3/G4 due to native cacheline orthogonality, and you can use 128-byte granularity on G5, if memory usage will not be an issue... remember that if you override NSObject, all objects will increase in length, and there are many thousands of hidden objects you never knew existed. I've never tried prepending ivar's like this, but it should work. Untested, written in Mail.app:

typedef struct MyCustomIvarStruct16 {
	int myHiddenIvar;
	int moreIvars[7];
} MyCustomIvarStruct;

00	myHiddenIvar <first byte of custom object>
	...
10	isa <first byte of normal object, *id>
	...
1f	<last byte of object>

HTH~

// multiple of 16 bytes, >= 16
#define myCustomObjectLengthBytes 16

id AllocCustomObject(Class cls, int len)
{
	if (cls && len) {
		void *buf = malloc(len + myCustomObjectLengthBytes);
		if (buf) {
			id obj = buf + myCustomObjectLengthBytes;
			*object = cls; // self->isa
			return obj;
		}
	}
	return NULL;
}
--
Shaun Wexler
MacFOH
http://www.macfoh.com

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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

  • Follow-Ups:
    • Re: Inserting a custom class into class hierarchy?
      • From: The Karl Adam <email@hidden>
  • Prev by Date: newbie question NSMatrix
  • Next by Date: Re: Inserting a custom class into class hierarchy?
  • Previous by thread: Fwd: newbie question NSMatrix
  • Next by thread: Re: Inserting a custom class into class hierarchy?
  • Index(es):
    • Date
    • Thread