libs-base/Headers/Additions/GNUstepBase/GSBlocks.h

52 lines
1.2 KiB
C
Raw Normal View History

Added GSBlocks header. This defines two macros: DEFINE_BLOCK_TYPE() defines a new block type. CALL_BLOCK() calls a block. These use the compiler's native support for blocks if it exists, but otherwise implement the lower-level version. Note that the structure defined by the fall-back versions is NOT a complete definition of the block layout, and copies of this structure should never be instantiated. This does not allow non-blocks-aware compiles (like GCC) to create blocks, but it does allow them to use blocks. This means that we can implement things like NSArray's -enumerateObjectsUsingBlock: without breaking GCC compatibility. To do so, you'd first need to declare a block type in the header, like this: DEFINE_BLOCK_TYPE(GSEnumeratorBlock, void, id, NSUInteger, BOOL*); Then declare the method like this: - (void)enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock; Finally, implement it like this: - (void)enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock { NSUInteger count = 0; BOOL shouldStop = NO; for (obj in self) { CALL_BLOCK(aBlock, obj, count++, &shouldStop); if (shouldStop) { return; } } } If you compile this with clang -fblocks then it will work using Clang's native support for blocks. If you compile it with GCC or clang without -fblocks then it will use the other version. Note that this example uses fast enumeration, so will only compile with clang. A proper GNUstep implementation should have some fall-back code for primitive compilers that don't support this either. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@29169 72102866-910b-0410-8b05-ffd578937521
2009-12-27 13:54:51 +00:00
/* Define the has_feature pseudo-macro for GCC. */
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#if __has_feature(blocks)
/**
* Defines a block type. Will work whether or not the compiler natively
* supports blocks.
*/
#define DEFINE_BLOCK_TYPE(name, retTy, argTys, ...) \
typedef retTy(^name)(argTys, ## __VA_ARGS__)
/**
* Calls a block. Works irrespective of whether the compiler supports blocks.
*/
#define CALL_BLOCK(block, args, ...) \
block(args, ## __VA_ARGS__)
/* Fall-back versions for when the compiler doesn't have native blocks support.
*/
#else
#if (GCC_VERSION >= 3000)
Added GSBlocks header. This defines two macros: DEFINE_BLOCK_TYPE() defines a new block type. CALL_BLOCK() calls a block. These use the compiler's native support for blocks if it exists, but otherwise implement the lower-level version. Note that the structure defined by the fall-back versions is NOT a complete definition of the block layout, and copies of this structure should never be instantiated. This does not allow non-blocks-aware compiles (like GCC) to create blocks, but it does allow them to use blocks. This means that we can implement things like NSArray's -enumerateObjectsUsingBlock: without breaking GCC compatibility. To do so, you'd first need to declare a block type in the header, like this: DEFINE_BLOCK_TYPE(GSEnumeratorBlock, void, id, NSUInteger, BOOL*); Then declare the method like this: - (void)enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock; Finally, implement it like this: - (void)enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock { NSUInteger count = 0; BOOL shouldStop = NO; for (obj in self) { CALL_BLOCK(aBlock, obj, count++, &shouldStop); if (shouldStop) { return; } } } If you compile this with clang -fblocks then it will work using Clang's native support for blocks. If you compile it with GCC or clang without -fblocks then it will use the other version. Note that this example uses fast enumeration, so will only compile with clang. A proper GNUstep implementation should have some fall-back code for primitive compilers that don't support this either. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@29169 72102866-910b-0410-8b05-ffd578937521
2009-12-27 13:54:51 +00:00
#define DEFINE_BLOCK_TYPE(name, retTy, argTys, ...) \
typedef struct {\
void *isa;\
int flags;\
int reserved;\
retTy (*invoke)(void*, argTys, ## __VA_ARGS__);\
} *name
#define CALL_BLOCK(block, args, ...) \
block->invoke(block, args, ## __VA_ARGS__)
#else /* GCC_VERSION >= 3000 */
#define DEFINE_BLOCK_TYPE(name, retTy, argTys, args...) \
typedef struct {\
void *isa;\
int flags;\
int reserved;\
retTy (*invoke)(void*, argTys, args);\
} *name
#define CALL_BLOCK(block, args...) \
block->invoke(block, args)
#endif /* GCC_VERSION >= 3000 */
Added GSBlocks header. This defines two macros: DEFINE_BLOCK_TYPE() defines a new block type. CALL_BLOCK() calls a block. These use the compiler's native support for blocks if it exists, but otherwise implement the lower-level version. Note that the structure defined by the fall-back versions is NOT a complete definition of the block layout, and copies of this structure should never be instantiated. This does not allow non-blocks-aware compiles (like GCC) to create blocks, but it does allow them to use blocks. This means that we can implement things like NSArray's -enumerateObjectsUsingBlock: without breaking GCC compatibility. To do so, you'd first need to declare a block type in the header, like this: DEFINE_BLOCK_TYPE(GSEnumeratorBlock, void, id, NSUInteger, BOOL*); Then declare the method like this: - (void)enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock; Finally, implement it like this: - (void)enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock { NSUInteger count = 0; BOOL shouldStop = NO; for (obj in self) { CALL_BLOCK(aBlock, obj, count++, &shouldStop); if (shouldStop) { return; } } } If you compile this with clang -fblocks then it will work using Clang's native support for blocks. If you compile it with GCC or clang without -fblocks then it will use the other version. Note that this example uses fast enumeration, so will only compile with clang. A proper GNUstep implementation should have some fall-back code for primitive compilers that don't support this either. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@29169 72102866-910b-0410-8b05-ffd578937521
2009-12-27 13:54:51 +00:00
#endif