Re: Specifying primitive types in NSCoders
Re: Specifying primitive types in NSCoders
- Subject: Re: Specifying primitive types in NSCoders
- From: Marcel Weiher <email@hidden>
- Date: Sun, 30 Jun 2002 01:13:45 +0200
On Saturday, June 29, 2002, at 06:22 Uhr, Andy Lee wrote:
At 11:50 AM -0400 6/29/02, Ken Tozier wrote:
I'm sure this is a simple one, but I can't find any concrete examples
of
how to specify primitive types in encoding and decoding methods.
For example:
- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data
What exactly do you pass into "valueType"?
There is a clue in the doc for -decodeValuesOfObjCTypes:, which directs
us to look up the @encode() compiler directive. I admit this hint is a
little tricky to find.
A tactic that helps me is to use MTLibrarian to index the Examples
directory. If there's a class or method whose usage is unclear to me,
it helps to see how somebody else uses it. A search on
"decodeValueOfObjCType" turned up a file called ProgressCell.m, which
contains these lines:
[decoder decodeValueOfObjCType:@encode(float)
at:&percentageIncrement];
[decoder decodeValueOfObjCType:@encode(float) at:&percentage];
[decoder decodeValueOfObjCType:@encode(int) at:&tag];
Of course, using @encode() with straight types means that you (the
programmer) have to correctly put down the type-information in three
places: the declaration, the encode and the the decode. Which is not
only tedious but also a source for errors.
Better to let the compiler do this work for you: use typeof()! So the
above becomes:
[decoder decodeValueOfObjCType:@encode(typeof(percentageIncrement))
at:&percentageIncrement];
[decoder decodeValueOfObjCType:@encode(typeof(percentage))
at:&percentage];
[decoder decodeValueOfObjCType:@encode(typeof(tag)) at:&tag];
Now you can be sure that you're getting the right type, but it's still
tedious: you have to type the name of the variable twice, and most what
is there is redundant. Since @encode and typeof() act at compile-time,
we can't encapsulate this in a method, but have to use the pre-processor
instead:
#define encodeVarName( coder, var, name ) [(coder)
encodeValueOfObjCType:@encode(typeof(var)) at:(void*)&var
withName:MPWUniqueStringWithCString(name,(sizeof name)-1)];
#define encodeVar( coder, var ) encodeVarName( coder,
var, #var )
(and the same for decoding)
So all you have to type is:
decodeVar( decoder, percentageIncrement );
decodeVar( decoder, percentage );
decodeVar( decoder, tag );
And the compiler will take care of the rest.
As usual, this stuff can be found in the MPWFoundation, at
<
http://www.metaobject.com/Community.html>
Of course, it is possible to write a generic coder/decoder method that
looks at the runtime information and encodes/decodes everything it finds
there, with the appropriate type-information. (You'd probably want to
support an exception list of names you don't want to encode).
Marcel (hoping this message won't get replicated)
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
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.