Skip to content

Instantly share code, notes, and snippets.

View JensAyton's full-sized avatar

Jens Ayton JensAyton

View GitHub Profile
@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)?:^{})();
}
// 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)
@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 / 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__)
@implementation Foo
- (id) foo
{
return [self bar];
}
- (id) bar
{
@JensAyton
JensAyton / abjc_arc.m
Last active September 28, 2015 04:37
A trivial implementation of Objective-C ARC runtime support for the Cocotron, excluding weak references.
/* Copyright (c) 2011 Jens Ayton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
@JensAyton
JensAyton / arc-exception-funhouse.m
Created October 22, 2011 17:08
Fun with ARC and exceptions
/*
The desired output of each test case is:
<MyException 0xPTR>{ TestName, retain count 1 } created.
<MyException 0xPTR>{ TestName, retain count 1 } caught.
<MyException 0xPTR>{ TestName, retain count 1 } dealloced.
For a fun time, try to predict the actual output for all six test cases!
// There are exact equivalents for CoreFoundation, if you like typing CFRelease a lot.
NSString *path = [[NSBundle mainBundle] builtInPlugInsPath]; // Or privateFrameworksPath, or some other thing.
path = [path stringByAppendingPathComponent:@"foo.dylib"];
void *handle = dlopen([path fileSystemRepresentation], flags);
@JensAyton
JensAyton / objc_dep.py
Created January 6, 2011 22:03
Header dependency mapper for Objective-C. This fork randomly colours arcs, for better tracing of spaghetti.
#!/usr/bin/python
# Nicolas Seriot
# 2011-01-06
# http://github.com/nst/objc_dep
"""
Input: path of an Objective-C project
Output: import dependancies Graphviz format
@JensAyton
JensAyton / BlockActionHack.m
Created September 21, 2010 14:17
Hack to enable the use of block ivars as IBAction targets. Stuffs a bunch of nastiness in NSObject, but transparenty works for classes implemeting actions: create a property of type BlockAction, assign a block to it, and use the property name (with a trai
/*
Hack to enable the use of block ivars as IBAction targets. Stuffs a bunch
of nastiness in NSObject, but transparenty works for classes implemeting
actions: create a property of type BlockAction, assign a block to it,
and use the property name (with a trailing colon) as the target selector.
Did I mention it's a hack? There are two major problems:
* Type safety. It assumes any block property will have the correct
signature. Can be partially worked around when compiling with Clang,
which embeds @encode() strings into blocks, but this is undocumented.