Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
JakobOvrum / dlang-org-redesign-dark-theme.css
Created March 14, 2016 11:51
dlang.org Re-Design Dark Theme
@-moz-document regexp("https?://(www\.|forum\.)?dlang.org/(|library|library-prerelease|phobos|phobos-prerelease|spec)(.*)") {
body {
color: #BFBFBF !important;
background-color: #222 !important;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAgMAAAANjH3HAAAACVBMVEUaGhohISElJSUh9lebAAAB20lEQVRIx4XWuZXDMAwE0C0SAQtggIIYoAAEU+aKOHhYojTrYP2+QfOW/5QIJOih/q8HwF/pb3EX+UPIveYcQGgEHiu9hI+ihEc5Jz5KBIlRRRaJ1JtoSAl5Hw96hLB1/up1tnIXOck5jZQy+3iU2hAOKSH1JvwxHsp+5TLF5MOl1/MQXsVs1miXc+KDbYydyMeUgpPQreZ7fWidbNhkXNJSeAhc6qHmHD8AYovunYyEACWEbyIhNeB9fRrH3hFi0bGPLuEW7xCNaohw1vAlS805nfsrTspclB/hVdoqusg53eH7FWot+wjYpOViX8KbFFKTwlnzvj65P9H/vD0/hibYBGhPwlPO8TmxRsaxsNnrUmUXpNhirlJMPr6Hqq9k5Xn/8iYQHYIuQsWFC6Z87IOxLxHphSY4SpuiU87xJnJr5axfeRd+lnMExXpEWPpuZ1v7qZdNBOjiHzDREHX5fs5Zz9p6X0vVKbKKchlSl5rv+3p//FJ/PYvoKryI8vs+2G9lzRmnEKkh+BU8yDk515jDj/HAswu7CCz6U/Mxb/PnC9N41ndpU4hUU7JGk/C9PmP/M2xZYdvBW2PObyf1IUiIzoHmHW9yTncliYs9A9tVNppdShfgQaTLMf+j3X723tLeHgAAAABJRU5ErkJggg==) !important;
background-cl
@JakobOvrum
JakobOvrum / factory.d
Last active March 17, 2016 18:15
Factory implementation
import std.traits : FunctionAttribute;
private string faToString(FunctionAttribute fa)
{
import std.algorithm, std.traits;
string result;
foreach (member; __traits(derivedMembers, FunctionAttribute))
{
if (fa & __traits(getMember, FunctionAttribute, member))
@JakobOvrum
JakobOvrum / gist:7881811
Created December 9, 2013 22:03
Approximation of C#'s `using` statement in D
interface IDisposable
{
void dispose();
}
auto using(T : IDisposable, CtorArgs...)(auto ref CtorArgs ctorArgs)
if(is(T == class) && __traits(compiles, new T(ctorArgs)))
{
static struct Result
{
@JakobOvrum
JakobOvrum / gist:7165288
Last active December 26, 2015 14:19
Demonstration of template pattern matching
// Takes instances of `Foo`
struct Foo(T){ T t; }
string takesFoo(T)(T t) if(is(T == Foo!U, U))
{
static if(is(T == Foo!U, U))
return U.stringof;
else
static assert(false);
@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)
@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: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 / 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: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 / 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