Closure Support in C / C ++ / Objective-C in Snow Leopard

    Guess what it is:

    testblock = ^(char *s) { printf("String is %s\n", s); };
    testblock("TEST!");

    * This source code was highlighted with Source Code Highlighter.

    Soon, such designs will increasingly be found in applications for Mac OS and, possibly, iPhone OS.

    Back in late summer 2008, Apple announced that it was working on extensions to C / C ++ / Obj-C under the code name “Blocks”, which are nothing more than closures .


    Why all this?


    Using closures often makes code cleaner and clearer, but that's not the only reason Apple has acted.

    As macresearch.com guessed a year ago, this is also connected with the “multi-core war”, which was confirmed in June 2009: Apple released a “technology” brief devoted to the work of Grand Central Dispatch (one of the key elements of the Mac OS X 10.6 Snow Leopard).

    Grand Central Dispatch allows you to easily manipulate various queues with blocks of code (this is very interestingly written in the brief), allowing you to more efficiently use the resources of multi-core systems.

    What does the programmer look like?


    This can be understood by developers who have access to Xcode from Snow Leopard ... as well as anyone who installs the PLBlocks toolchain / runtime for Mac OS X 10.5. This project contains the "advanced" GCC, a plug-in for Xcode and the necessary frameworks. Where did this code come from? The author of PLBlocks actually ported Apple's GCC patches to 10.5 . The installation is described on the project page and does not present any difficulties.

    Example code I wrote in 15 minutes:

    #include
    #include

    void run_callback(char *s, void (^callback)(int len)) {

        printf(" %s:", s);
        callback(strlen(s));
    }

    void print_numbers(int *array, size_t array_size, int (^chooser)(int)) {
        int i;
        
        for (i = 0; i < array_size; i++)
            if (chooser(array[i]))
                printf("%4d", array[i]);
        
        printf("\n");
    }

    int main (int argc, const char * argv[]) {
        void (^testblock)(char *);
        
        testblock = ^(char *s) { printf("String is %s\n", s); };
        testblock("TEST!");

        
        int test[] = { -1, 5, 91, -45, 0, 4, -43, 42 };
        int len = sizeof(test)/sizeof(int);

        printf("all numbers:\n ");
        print_numbers(test, len, ^(int n) {
            return 1;
        });
        
        printf("positive only:\n ");
        print_numbers(test, len, ^(int n) {
            return n > 0;
        });
        
        
        int factor = 2;
        printf("another demo..\n");    
        run_callback("hello", ^(int len) {
            int i;
            for (i = 1; i <= len*factor; i++) printf("%3d", i);
            printf("\n");
        });
        
        return 0;
    }

    * This source code was highlighted with Source Code Highlighter.

    Execution Result:

    String is TEST!
    all numbers:
    -1 5 91 -45 0 4 -43 42
    positive only:
    5 91 4 42
    another demo ...
    hello: 1 2 3 4 5 6 7 8 9 10


    The example is very simple; Of course, the theme of code blocks in C / Obj-C and the use of Grand Central Dispatch is much wider and deeper.

    What else to read?


    Specially selected materials for further reading (eng.):
    We look forward to the release of Snow Leopard and the new Xcode. However, while there is no evidence that the blocks will also appear in the iPhone OS (except for the mentioned implementation of PLBlocks).

    All in all, this is great.

    Also popular now: