Re: objective-c / cocoa efficiency
Re: objective-c / cocoa efficiency
- Subject: Re: objective-c / cocoa efficiency
- From: John Stiles <email@hidden>
- Date: Tue, 8 Mar 2005 18:43:08 -0800
On Mar 8, 2005, at 5:59 PM, Jeff Laing wrote:
Well, firstly, I did say this two paragraphs later on:
(quote)
(Yes, this is way suboptimal, use a hash-table or something similiar or
better, a flex parser - in both cases, these are harder to code and
keep
straight during development (when addition of new keywords require
more than
just a .c recompile)
(/quote)
I should have spotted this. That'll teach me to skim :)
FWIW I think it's pretty darn useful to wrap up the hash-table solution
in a reusable class. For a while I was writing code to parse XML files
and I needed to handle tons of different keywords, so I whipped up a
class that let me do this (slightly simplified):
static KeywordParser<MyClass> parser
(
"firstkeyword", KeywordParser::Callback(
&MyClass::FirstKeyword ),
"secondkeyword", KeywordParser::Callback(
&MyClass::SecondKeyword ),
"thirdkeyword", KeywordParser::Callback(
&MyClass::ThirdKeyword ),
... etc ...
NULL // this was done using va_list so I needed to stop the list with
a NULL
);
// call this->FirstKeyword/SecondKeyword/ThirdKeyword depending on the
value of myString bool success = parser.Parse( myString, this );
The parser is declared static so that the hash table is only
constructed once, the first time the parser is used. (Building the hash
table every time would suck! :) )
This was a total lifesaver and it only took a short while to write. It
was totally worth it. I'd recommend the technique to anyone who needs
to parse tons of strings. Of course if you're into ObjC then you'd
probably design it a little differently.
PS note that "KeywordParser::Callback" is just used to force the
va_list to treat "&MyClass::WhicheverKeyword" as a
pointer-to-member-function. Without that, the compiler wouldn't know
which type it was supposed to use, and I got strange results (don't
remember the details). If you weren't using PTMF you could probably
omit it.
_______________________________________________
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