discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fast enumeration


From: Ivan Vučica
Subject: Re: Fast enumeration
Date: Fri, 4 Feb 2011 18:37:27 +0100


On 4. vel. 2011., at 16:31, Jens Ayton wrote:

On Feb 4, 2011, at 15:54, Ivan Vučica wrote:


If you refer to for(id x in y) syntax, I believe you just need to use a compiler with objc2.0 support, as well as a sufficiently recent runtime.

That, and support for the fast enumeration protocol in Base collections. This exists at least in base-1.20.1.

This doesn't actually resolve the question, though, it just restates it.

Which compilers support [this aspect of] Objective-C 2.0 outside of Apple's platforms? Neither gcc nor clang provide sufficiently comprehensive release notes to determine this sort of thing reliably. (It seems that the answer is "Clang 2.6", since "Most of Objective-C 2.0 is now supported with the GNU runtime", but it might not be.)

If someone has a clear answer, this is something that would interest me as well.

How do I detect, at build time, which runtime I'm using, and precisely which version of what runtime has the necessary support? This is the sort of information one might expect to find on the ObjC2_FAQ wiki page, but one doesn't (and it's probably out of date anyway).

With regards to the runtime support, I believe you can test at -- runtime. Attempting to enumerate over an NSString throws this exception under Cocoa runtime:

2011-02-04 18:18:58.003 test[2804:903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7fff710f2170'

which leads me to believe you could test for countByEnumeratingWithState:objects:count: selector. However, this does not resolve the issue of testing this kind of stuff at build time.

I have figured out a way you might be able to resolve this. A "configure" step in the build process. (If you did not using autotools, you might write a manual "configure" script which would perform this test.)

Test program would simply iterate over an empty NSArray using for(x in y) syntax. If either compilation or running of the program were to fail, then the fast iteration is not supported. 

Here's a simple configure script I threw together under Cocoa environment. For GNUstep, I'd probably go with outputting a GNUmakefile as well. instead of just calling the compiler directly.


#!/bin/bash
cat << _END > /tmp/__configure_test.m
#include <Foundation/Foundation.h>
int main (int argc, char**argv)
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSArray *testArray = [[NSArray alloc] init];
        for(id i in testArray)
        {
                // nothing
        }
        [pool release];
        return 0;
}

_END

echo "#ifndef CONFIG_H_INCLUDED" >  config.h
echo "#define CONFIG_H_INCLUDED" >> config.h

echo "==> Testing fast iteration..."
if gcc -x objective-c /tmp/__configure_test.m -framework Foundation -o /tmp/__configure_test && /tmp/__configure_test; then

        echo "#define HAVE_FAST_ITERATION 1" >> config.h

        echo "====> Pass"
else
        echo "#define HAVE_FAST_ITERATION 0" >> config.h

        echo "====> Fail"
fi

echo "#endif" >> config.h

I did something similar for testing whether or not NSViewController exists (it didn't in older GNUstep releases, such as the one in Debian). If anyone has a better way to test for fast iteration, I'm all ears. ;-)




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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]