Skip to content

Instantly share code, notes, and snippets.

View JensAyton's full-sized avatar

Jens Ayton JensAyton

View GitHub Profile
@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
@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__)
@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"
// 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 / 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 {
@JensAyton
JensAyton / gist:548432
Created August 24, 2010 22:11
Sketch for multiple return values in Objective-C
// Sketch for multiple return values in Objective-C
// Method declaration:
- (id, NSError *) someMethod:param;
// Desugars to:
typedef struct { id a, NSError *b } __someMethodResult; // Obviously gensym/anonymous in real implementation
- (__someMethodResult) someMethod:param;
// Return type encoding would be "{?=@@}", anonymous struct of two objects.
@JensAyton
JensAyton / BrinfArk.swift
Created October 17, 2019 11:21
How do you write Swift without using the A key? Reformulate the problem in terms of a language without pesky letters, of course. Unfortunately doesn’t tail recurse. :-(
let opIncr: UInt8 = 43 // +
let opDecr: UInt8 = 45 // -
let opPrev: UInt8 = 60 // <
let opNext: UInt8 = 62 // >
let opFwd: UInt8 = 91 // [
let opBck: UInt8 = 93 // ]
let opOutput: UInt8 = 46 // .
let opInput: UInt8 = 44 // ,
let opStop: UInt8 = 0
@JensAyton
JensAyton / StringInits.swift
Created February 7, 2020 14:58
Surprising init resolution in Swift (5.1 and 5.2)
struct Test {}
extension Test: LosslessStringConvertible {
init?(_ description: String) {
print("unlabelled init")
}
var description: String { "Test" }
}