|
From: | Maxthon Chan |
Subject: | Objective-C boilerplate header (sort of like boost) |
Date: | Wed, 12 Jun 2013 23:25:17 +0800 |
Do you guys have a boilerplate that you are very used to build code on top of? I have one like this (sans include guard which depend on header file name): #include <sys/cdefs.h> #include <stdarg.h> #include <stdint.h> #include <stddef.h> #include <sys/types.h> // Feature testers #ifndef __has_feature #define __has_feature(x) 0 #endif #ifndef __has_builtin #define __has_builtin(x) 0 #endif #ifndef __has_extension #define __has_extension(x) 0 #endif #ifndef __has_attribute #define __has_attribute(x) 0 #endif // __inline #if __has_attribute(always_inline) #define __inline static inline __attribute__((always_inline)) #else // !__has_attribute(always_inline) #define __inline static inline #endif// __has_attribute(always_inline) // __restrict #ifndef __cplusplus #ifndef __restrict #if __STDC_VERSION__ >= 199901L #define __restrict restrict #else // __STDC_VERSION__ < 199901L #define __restrict #endif // __STDC_VERSION__ >= 199901L #endif // !defined(__restrict) #endif // !defined(__cplusplus) // noreturn (__noreturn and unreachable()) #if __has_attribute(noreturn) #define __noreturn __attribute__((noreturn)) #if __has_builtin(__builtin_unreachable) #define unreachable() __builtin_unreachable() #else // !__has_builtin(__builtin_unreachable) #define unreachable() do {} while (0) #endif // __has_builtin(unreachable) #else // !__has_attribute(noreturn) #define __noreturn #define unreachable() do {} while (0) #endif // __has_attribute(noreturn) // Deprecated/unavalible with messages #undef __deprecated #undef __unavailable #if __has_attribute(deprecated) #if __has_extension(attribute_deprecated_with_message) #define __deprecated(_msg) __attribute__((deprecated(_msg))) #else // !__has_extension(attribute_deprecated_with_message) #define __deprecated(_msg) __attribute__((deprecated)) #endif // __has_extension(attribute_deprecated_with_message) #else // !__has_attribute(deprecated) #define __deprecated(_msg) #endif // __has_attribute(deprecated) #if __has_attribute(unavailable) #if __has_extension(attribute_unavailable_with_message) #define __unavailable(_msg) __attribute__((unavailable(_msg))) #else // !__has_extension(attribute_unavailable_with_message) #define __unavailable(_msg) __attribute__((unavailable)) #endif // __has_extension(attribute_unavailable_with_message) #else // !__has_attribute(unavailable) #define __unavailable(_msg) #endif // __has_attribute(unavailable) #if __has_extension(enumerator_attributes) #define __e_deprecated(_msg) __deprecated(_msg) #define __e_unavailable(_msg) __unavailable(_msg) #else // !__has_extension(enumerator_attributes) #define __e_deprecated(_msg) #define __e_unavailable(_msg) #endif // __has_extension(enumerator_attributes) // Format strings #if __has_attribute(format) #define __format(...) __attribute__((format(__VA_ARGS__))) #else // __has_attribute(format) #define __format(...) #endif // C-safe Objective-C type declaration #if defined(__OBJC__) #import <Foundation/Foundation.h> #define __class @class #else // !defined(__OBJC__) #include <objc/runtime.h> #define __class typedef struct objc_object #endif // defined(__OBJC__) // Objective-C instancetype #if defined(__OBJC__) #if !__has_feature(objc_instancetype) typedef id instancetype #endif // !__has_feature(objc_instancetype) #endif // defined(__OBJC__) // Enumerations #define __enum(_name, _type) enum _name : _type; enum _name // Some convenience Objective-C functions and macros __class NSString; #define NSStringConstant(_name, _value) extern NSString *const _name; #if defined(__OBJC__) __BEGIN_DECLS __inline __format(NSString, 1, 0) NSString *NSSTRv(NSString *format, va_list args) { NSString *string = [[NSString alloc] initWithFormat:format arguments:args]; #if !__has_feature(objc_arc) && !__has_feature(objc_gc) [string autorelease]; #endif return string; } __inline __format(NSString, 1, 2) NSString *NSSTR(NSString *format, ...) { va_list args; va_start(args, format); NSString *string = NSSTRv(format, args); va_end(args); return string; } __END_DECLS #endif // defined(__OBJC__) |
[Prev in Thread] | Current Thread | [Next in Thread] |