📜 ⬆️ ⬇️

Support for closures 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 constructions will increasingly be found in applications for Mac OS and, possibly, iPhone OS.

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

Why all this?


The use of closures can often make the code cleaner and clearer, but this is not the only reason for Apple’s actions.

As macresearch.com guessed a year ago, this is also associated with the “war for multi-core”, which was confirmed in June 2009: Apple released a “technology” brief dedicated to the work of the Grand Central Dispatch (one of the key elements of 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.

How does it look from the programmer?


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

Sample code written by me in 15 minutes:

#include <stdio.h>
#include <string.h>

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 .

Result of performance:

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 topic of code blocks in C / Obj-C and the use of Grand Central Dispatch are much wider and deeper.

What else to read?


Selected materials for further reading (English):
We are waiting for the release of Snow Leopard and the new XCode. However, so far there is no evidence that the blocks will also appear in the iPhone OS (except for the PLBlocks implementation mentioned).

In general, it is beautiful.

Source: https://habr.com/ru/post/66632/


All Articles