Skip to content

Instantly share code, notes, and snippets.

View TurkeyMan's full-sized avatar

Manu Evans TurkeyMan

View GitHub Profile
@TurkeyMan
TurkeyMan / guarded.d
Last active October 13, 2019 00:17
guarded.d
struct ReadLease(T)
{
~this() { releaseReadLease(this); }
const(T)* get() @property return const pure nothrow @safe @nogc { return object; }
alias get this;
private:
const(T)* object;
shared(ptrdiff_t)* leaseCounter;
@TurkeyMan
TurkeyMan / test.d
Created April 16, 2019 20:18
Some F12 mistakes
module visuald_test;
import std.stdio;
int main()
{
writeln("Hello D World!\n");
return 0;
}
@TurkeyMan
TurkeyMan / blocked_array.cpp
Created May 17, 2018 01:58
Blocked array skeleton
#include <stdio.h>
#include <string.h>
class BlockedArray
{
static const size_t BlockSize = 1024; // how many elements should fit in a block?
struct Block
{
union
@TurkeyMan
TurkeyMan / sample_alloc.d
Last active March 15, 2018 20:39
Example comprehensive allocator
import core.memory : pureMalloc, pureFree;
import object : destroy;
// this API supports classes and non-classes with the same functions
auto myAlloc(T, Args...)(auto ref Args ctorArgs) @trusted
{
static if (is(T == class))
{
size_t bytes = __traits(classInstanceSize, T); // this is because classes are reference types; therefore T.sizeof == sizeof the class reference (pointer)
alias ReturnType = T; // classes are references, so you don't handle them by pointer
enemy = { screenWidth/2, screenTop };
player = { screenWidth/2, screenBottom };
while(1)
{
if(enemy.x > screenWidth)
v = -1;
else if(enemy.x < 0)
v = 1
enemy.x += v;
@TurkeyMan
TurkeyMan / constrange.d
Created April 26, 2015 04:58
Literal/constant ranges
auto literalRange(alias value)(size_t length)
{
struct LiteralRange(alias Value)
{
alias value = Value;
size_t length;
@property bool empty() const { return !length; }
@property auto front() inout { return value; }
void popFront() { --length; }