Skip to content

Instantly share code, notes, and snippets.

View JensAyton's full-sized avatar

Jens Ayton JensAyton

View GitHub Profile
@implementation Foo
- (id) foo
{
return [self bar];
}
- (id) bar
{
@JensAyton
JensAyton / gist:3855499
Created October 8, 2012 23:00
Some of the macros in NSObjCRuntime.h just aren’t ugly enough.
#undef MIN
#define MIN__(A,B,C) ({ __typeof__(A) __a##C = (A); __typeof__(B) __b##C = (B); __a##C < __b##C ? __a##C : __b##C; })
#define MIN_(A,B,C) MIN__(A,B,C)
#define MIN(A,B) MIN_(A,B,__COUNTER__)
#undef MAX
#define MAX__(A,B,C) ({ __typeof__(A) __a##C = (A); __typeof__(B) __b##C = (B); __a##C < __b##C ? __b##C : __a##C; })
#define MAX_(A,B,C) MAX__(A,B,C)
#define MAX(A,B) MAX_(A,B,__COUNTER__)
// Expansion of JATExpandLiteral(@"foo: {foo}; bar: {bar}; baz: {baz}", foo, bar, baz)
JAT_DoExpandTemplateUsingMacroKeysAndValues(@"foo: {foo}; bar: {bar}; baz: {baz}", (JATNameArray){ @"foo", @"bar", @"baz", ((void*)0) }, (JATParameterArray){ JATCastParameter(foo), JATCastParameter(bar), JATCastParameter(baz), ((void*)0) }, 3)
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Test: NSObject
@end
@interface Test (Dynamic)
- (void)foo:(float)x :(float)y;
@JensAyton
JensAyton / oops.c
Last active August 29, 2015 13:56 — forked from davepeck/oops.c
One example of cleanup without using goto. (This isn't an opinion on what Apple “should have” done, it’s just an answer to a specific question on Twitter. It would have done nothing to avoid the actual bug.) I also removed the unused variable “signedHashes”.
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
{
SSLBuffer hashCtx;
hashCtx.date = 0;
OSStatus err = SSLVerifySignedServerKeyExchangeInner(ctx, isRsa, signedParams, signature, signatureLen, &hashCtx);
SSLFreeBuffer(hashCtx);
@JensAyton
JensAyton / JADictionaryOfVariableBindings.h
Created April 28, 2014 22:28
NSDictionaryOfVariableBindings at compile time, just because.
#import <Foundation/Foundation.h>
#define JADictionaryOfVariableBindings(...) \
[NSDictionary dictionaryWithObjects:(const id []){ __VA_ARGS__ } \
forKeys:(const NSString *[]){ JATEMPLATE_MAP(JATEMPLATE_NAME_FROM_ARG, __VA_ARGS__) } \
count:JATEMPLATE_ARGUMENT_COUNT(__VA_ARGS__)]
#define JATEMPLATE_NAME_FROM_ARG(ITEM) @#ITEM
@JensAyton
JensAyton / gist:4c6df3f1ca7d32c15fef
Created June 9, 2014 07:13
try() for discriminated-union error handling in Swift
import Foundation
// Because the real thing crashes
enum Result {
case Value(Any)
case Error(NSError?)
}
@JensAyton
JensAyton / format.h
Created December 10, 2015 08:00
Macro for pasting ANSI escape codes together, based on https://twitter.com/velartrill/status/674797360774569984. This version requires GCC/Clang extensions for handling zero-length macro __VA_ARGS__es, because I’m lazy, but it could be done without. Note that this is an awful idea because sooner or later you’ll want to output to a text file and …
#ifndef format_h
#define format_h
#define reset "0"
#define bold "1"
#define red "31"
#define format(first, ...) \
"\e[" first join_format_codes(__VA_ARGS__) "m"
@JensAyton
JensAyton / lolif.m
Created January 5, 2017 23:02
Something something named arguments
struct ifparams {
#if __has_feature(objc_arc)
__unsafe_unretained
#endif
dispatch_block_t then, otherwise;
};
static inline void iff(bool condition, struct ifparams params) {
((condition?params.then:params.otherwise)?:^{})();
}
extern "C" {
// This is global, but you can call it what you like
inline void MyPrefixedCFShow(const void *object) {
// Redeclared system function is local scope and counts as extern "C"
extern void CFShow(const void * object);
CFShow(object);
}
}
class DumbThing {