Skip to content

Instantly share code, notes, and snippets.

@bwoods
bwoods / uuid.c
Last active November 7, 2020 06:54
A standard conforming implementation of UUIDs in three lines.
// Surprisingly, a standard conforming implementation of UUIDs in three lines.
#include <stdlib.h>
static void generate_random_uuid(unsigned char uuid[16])
{
arc4random_buf(uuid, 16);
// Version 4, Variant 1: http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
uuid[6] = (uuid[6] & 0x0F) | 0x40;
@bwoods
bwoods / build.sh
Created February 25, 2013 23:27
Compiling luajit-2.0 for iOS (as an Xcode Run Script build step)
MKROOT="$SRCROOT/LuaJIT₂"
for CPU in $ARCHS
do
make -C "$MKROOT" CC="clang" HOST_CC="clang -m32 -I/usr/include -isysroot $DEVELOPER_SDK_DIR" HOST_LDFLAGS="-L/usr/lib" TARGET_SYS=iOS CROSS="/usr/bin/" TARGET_FLAGS="-isysroot $SDKROOT -arch $CPU" BUILDMODE=static XCFLAGS="-DLUAJIT_ENABLE_LUA52COMPAT" CCOPT="-O4 -fomit-frame-pointer" CCWARN="-Wall -Wno-string-plus-int" clean amalg
mv "$MKROOT/src/libluajit.a" "$BUILT_PRODUCTS_DIR/libluajit-${CPU}.a"
done
lipo -create $BUILT_PRODUCTS_DIR/libluajit-*.a -output $BUILT_PRODUCTS_DIR/libluajit.a
// From: http://samwize.com/2012/10/03/sentestingkit-does-not-support-wait-for-blocks/
- (void) testBlockMethod
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// Your block method eg. AFNetworking
NSURL *url = [NSURL URLWithString:@"http://httpbin.org/ip"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

NSFetchedResultsControllerDelegates

An extension that allows you to setup an NSFetchResultsConttroller as a UITableViewDataSource or UICollectionViewController in a single line of code.

self.tableView.dataSource = self.fetchedResultsController

All edits to CoreData will be reflected automatically in any relevent UITableViews or UICollectionViews.

@bwoods
bwoods / git.cmake
Last active August 29, 2015 14:15
CMake git submodule macro
function(add_submodule path url)
find_program(GIT git CACHE)
message(STATUS "git submodule ${path}")
execute_process(ERROR_QUIET WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${GIT} submodule add ${url} ${path}
COMMAND ${GIT} submodule update --init --recursive ${path})
if (NOT ARGV2 STREQUAL "") # a tag or a branch
get_filename_component(submodule_directory "${path}" ABSOLUTE)
execute_process(COMMAND ${GIT} checkout ${ARGV2} OUTPUT_QUIET WORKING_DIRECTORY ${submodule_directory} ERROR_QUIET)
template <typename... T, int... I>
static auto subtuple(std::tuple<T...>&& tuple, std::index_sequence<I...>) {
return std::make_tuple(std::get<I>(tuple)...);
}
@bwoods
bwoods / declname.cpp
Created July 8, 2015 23:55
demangle C++ TypeID names (std::unique_ptr version)
#include <cxxabi.h> // gcc and clang…
#include <stdlib.h>
auto demangle(std::string&& name) -> std::unique_ptr<char, void(*)(char *)>
{
int status = 0;
return { abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), [] (char * p) { ::free(p); } };
}
@bwoods
bwoods / uniform_type_identifiers.cpp
Last active September 20, 2015 04:34
Parse Application bundles for UTI information
#include <CoreFoundation/CoreFoundation.h>
#include <sqlite3/sqlite3.h>
#include <cstdio>
#include <iterator>
#include <vector>
#include "raii/raii.hpp"
@bwoods
bwoods / raii.hpp
Last active September 19, 2015 22:48
#pragma once
#include <type_traits>
namespace raii {
template <class T>
class pointer {
template <typename D>
@bwoods
bwoods / literals.hpp
Created December 17, 2015 21:12
C++11 user defined literals for std::integral_constant<int64_t, N>
namespace literals { // inspired by http://codereview.stackexchange.com/a/51576
constexpr int64_t combine(int64_t accumulator) {
return accumulator;
}
template <typename... Characters>
constexpr int64_t combine(int64_t accumulator, char character, Characters... Rest) {
int digit = (character >= '0' && character <= '9') ? character - '0'
: throw std::range_error("Only decimal literals are supported.");