gnustep-dev
[Top][All Lists]
Advanced

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

Re: Objective-C boilerplate header (sort of like boost)


From: Maxthon Chan
Subject: Re: Objective-C boilerplate header (sort of like boost)
Date: Fri, 14 Jun 2013 13:59:51 +0800

Copied from Xcode and Mail saved all formatting from code highlighting as RTF. The code boilerplate is what I always use as a start point of my code. It makes it possible to expose some Objective-C code as plain C/C++ as I did in CGIKit 5, and it detects compiler. I have a newer version that does easy string constant too.

Sent from my iPhone

On 2013年6月14日, at 13:51, Jamie Ramone <address@hidden> wrote:

Colorful text is colorful! So what is this "boilerplate" thing exactly? I've heard the _expression_ before but not quite sure what it refers to.


On Wed, Jun 12, 2013 at 12:25 PM, Maxthon Chan <address@hidden> wrote:
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__)


_______________________________________________
Gnustep-dev mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/gnustep-dev




--
Besos, abrazos, confeti y aplausos.
Jamie Ramone
"El Vikingo"

reply via email to

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