Skip to content

Instantly share code, notes, and snippets.

View ctreffs's full-sized avatar
💭
🏊 🚴‍♂️ 🏃

Christian Treffs ctreffs

💭
🏊 🚴‍♂️ 🏃
View GitHub Profile
@ctreffs
ctreffs / MemoryAddress.swift
Last active January 14, 2021 07:20
Memory address String of AnyObject instance in Swift
extension MemoryLayout where T: AnyObject {
/// Returns the memory address of the given instance.
/// - Parameter instance: The instance to inspect.
/// - Returns: A hex string representation of the memory address without leading zeros (e.g. `0x102f6dda0`).
@inline(__always)
public static func address(of instance: T) -> String {
"0x"+String(UInt(bitPattern: ObjectIdentifier(instance)), radix: 16, uppercase: false)
}
}
@ctreffs
ctreffs / CommonProfile.metal
Created October 15, 2020 17:41 — forked from warrenm/CommonProfile.metal
SceneKit's CommonProfile Shader v2 (macOS 10.15)
////////////////////////////////////////////////
// CommonProfile Shader v2
#import <metal_stdlib>
using namespace metal;
#ifndef __SCNMetalDefines__
#define __SCNMetalDefines__
struct RelativePointer<Pointee> {
var offset: Int32
mutating func advanced() -> UnsafeMutablePointer<Pointee> {
return withUnsafePointer(to: &self) { [offset] pointer in
let rawPointer = UnsafeRawPointer(pointer)
let advanced = rawPointer.advanced(by: Int(offset))
let pointer = advanced.assumingMemoryBound(to: Pointee.self)
return UnsafeMutablePointer(mutating: pointer)
}
@ctreffs
ctreffs / QuickAlphabet.swift
Last active August 24, 2020 19:09
Generate the alphabet quickly [Swift]
let asciiChars = Character("A")...Character("z")
let allCharsAsString = String(asciiChars)
extension Character: Strideable {
public func distance(to other: Character) -> Int {
guard let value = self.asciiValue, let otherValue = other.asciiValue else {
fatalError("ASCII values are nil")
}
//
// ManagedContiguousArray.swift
// FirebladeECS
//
// Created by Christian Treffs on 28.10.17.
//
public struct ManagedContiguousArray<Element> {
public typealias Index = Int
extension BinaryInteger {
/// Provides a padded binary representation of this value (i.e. 01001001).
public var binaryDescription: String {
let str = String(self, radix: 2)
return String(repeatElement("0", count: (MemoryLayout<Self>.stride * 8) - str.utf8.count)) + str
}
}
extension OptionSet where RawValue: BinaryInteger {
/// Provides a padded binary representation of this value (i.e. 01001001).
@ctreffs
ctreffs / rsync.sh
Last active March 30, 2020 20:39
Rsync with options
rsync --archive --compress --crtimes --executability --human-readable --info=progress2 --ipv6 --partial --prune-empty-dirs --safe-links --sparse --update <SRC> <DEST>
- or -
rsync -azNE6mSu --info=progress2 --partial --safe-links <SRC> <DEST>
@ctreffs
ctreffs / CArray.swift
Last active January 5, 2021 12:24
[Swift] Access uniform tuples (i.e. C arrays) like a collection in Swift
public enum CArray<T> {
@discardableResult
@_transparent
public static func write<C, O>(_ cArray: inout C, _ body: (UnsafeMutableBufferPointer<T>) throws -> O) rethrows -> O {
try withUnsafeMutablePointer(to: &cArray) {
try body(UnsafeMutableBufferPointer<T>(start: UnsafeMutableRawPointer($0).assumingMemoryBound(to: T.self),
count: (MemoryLayout<C>.stride / MemoryLayout<T>.stride)))
}
}
@ctreffs
ctreffs / main.swift
Created August 30, 2019 22:10
Minimal Cocoa/AppKit Swift Package Manager application (SwiftPM)
import AppKit
_ = NSApplication.shared
NSApp.setActivationPolicy(.regular)
//let delegate = AppDelegate()
//NSApplication.shared.delegate = delegate
let menubar = NSMenu()
let appMenuItem = NSMenuItem()