Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
JakobOvrum / gist:2656623
Created May 10, 2012 23:46
Workaround for type template identification with variadic template arguments
struct S(T...){}
// void foo(T : S!U, U...)(T s) {} // DMD ICE
void foo(T)(T s) if(is(T Unused == S!U, U...)) {}
void main()
{
S!int s;
foo(s);
}
ReturnType!func checkGl(alias func, Args...)(Args args) {
debug scope(success) {
GLenum error_code = glGetError();
if(error_code != GL_NO_ERROR) {
stderr.writefln(`OpenGL function "%s" failed: "%s."`, func.stringof, gl_error_string(error_code));
}
}
return func(args);
@JakobOvrum
JakobOvrum / gist:3196553
Created July 29, 2012 07:49
initOnce function
import std.stdio;
void initOnce(alias var)(lazy typeof(var) init)
{
static bool hasInitialised = false;
if(!hasInitialised)
{
var = init();
hasInitialised = true;
@JakobOvrum
JakobOvrum / gist:3198842
Created July 29, 2012 13:36
byte array to hex string
import std.stdio;
import std.format;
import std.array;
void main()
{
ubyte[] data = [0xAA, 0xFF];
auto app = appender!string();
@JakobOvrum
JakobOvrum / values.d
Last active December 14, 2015 06:59
`values` function, taking a number of values with a common type and returning an aggregate containing the values in order, with a range interface. Useful when a few values need to be returned from a function as part of a range composition.
auto values(Elems...)(auto ref Elems elems) if(is(CommonType!Elems))
{
alias CommonType!Elems ElemType;
static struct StaticArray
{
ElemType[Elems.length] data = void;
size_t i = 0;
bool empty() const
@JakobOvrum
JakobOvrum / gist:5574550
Last active December 17, 2015 07:39
malloc/free wrappers for classes
import core.exception : onOutOfMemoryError;
import core.stdc.stdlib : malloc, free;
import std.conv : emplace;
T alloc(T, Args...)(auto ref Args args) if(is(T == class))
{
enum size = __traits(classInstanceSize, T);
if(auto p = malloc(size))
{
@JakobOvrum
JakobOvrum / gist:5608585
Last active December 17, 2015 12:19
tuplify - create tuple with named fields given a list of variables
import std.typecons : Tuple;
import std.typetuple : TypeTuple;
template NameTypePairs(alias front, vars...)
{
private enum name = __traits(identifier, front);
private alias pair = TypeTuple!(typeof(front), name);
static if(vars.length == 0)
alias NameTypePairs = pair;
@JakobOvrum
JakobOvrum / gist:5656352
Created May 27, 2013 10:18
DeclareVars - declare variables in-place given a type and list of identifiers
// Could use `alias front` instead to bind to single characters as well
mixin template DeclareVars(T, string front, names...)
{
mixin("T " ~ front ~ ";");
static if(names.length > 0)
mixin DeclareVars!(T, names);
}
struct S
@JakobOvrum
JakobOvrum / list_sources.d
Last active December 19, 2015 06:48
Helper script for writing makefiles
#!/usr/bin/env rdmd
/**
* Output a makefile-compatible list of source files
* given a list of directories containing the files.
* Example:
* ./list_sources.d src util >> Makefile
*/
import std.algorithm, std.path;
// returns range of file paths
@JakobOvrum
JakobOvrum / gist:6816017
Created October 3, 2013 19:50
ExceptionConstructor mixin template
mixin template ExceptionConstructor()
{
@safe pure nothrow
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
{
super(msg, file, line, next);
}
}
mixin template ExceptionConstructor(string defaultMessage)