Last active
August 29, 2015 14:24
-
-
Save John-Colvin/103d3c064ad6cb4cf435 to your computer and use it in GitHub Desktop.
Messing with types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.typetuple; | |
import std.typecons; | |
import std.traits; | |
struct Pack(TL...) | |
{ | |
alias expand = TL; | |
enum length = TL.length; | |
@disable this(); | |
} | |
enum isPack(T) = is(T == Pack!Args, Args...); | |
template EqualLength(Packs ...) | |
if(allSatisfy!(isPack, Packs) && Packs.length != 0) | |
{ | |
static if (Packs.length == 1) enum EqualLength = true; | |
else | |
{ | |
template EqualLengthTo(size_t len) | |
{ | |
enum EqualLengthTo(T) = len == T.length; | |
} | |
enum EqualLength = Filter!(EqualLengthTo!(Packs[0].length), Packs[1 .. $]).length == Packs.length - 1; | |
} | |
} | |
template Alias(alias a) | |
{ | |
static if (__traits(compiles, { alias x = a; })) | |
alias Alias = a; | |
else static if (__traits(compiles, { enum x = a; })) | |
enum Alias = a; | |
else | |
static assert(0, "Cannot alias " ~ a.stringof); | |
} | |
template Alias(a...) | |
{ | |
alias Alias = a; | |
} | |
template Head(Pack) | |
if(isPack!Pack) | |
{ | |
alias Head = Alias!(Pack.expand[0]); | |
} | |
template Tail(Pack) | |
if(isPack!Pack) | |
{ | |
alias Tail = Pack!(Pack.expand[1 .. $]); | |
} | |
template TemplAppender(string suffix) | |
{ | |
enum TemplAppender(string s) = s ~ suffix; | |
} | |
template RoundRobin(Args ...) | |
if(Args.length >= 2 && allSatisfy!(isPack, Args) && EqualLength!Args) | |
{ | |
static if (Args[0].length == 0) | |
alias RoundRobin = TypeTuple!(); | |
else | |
alias RoundRobin = TypeTuple!(staticMap!(Head, Args), RoundRobin!(staticMap!(Tail, Args))); | |
} | |
alias SliceOf(T) = T[]; | |
alias TransformMembers(alias TypeTransform, alias NameTransform, T) = Tuple!( | |
RoundRobin!(Pack!(staticMap!(TypeTransform, FieldTypeTuple!PriceBar)), | |
Pack!(staticMap!(NameTransform, FieldNameTuple!PriceBar)))); | |
import std.datetime; | |
struct PriceBar | |
{ | |
DateTime date; | |
double open; | |
double high; | |
double low; | |
double close; | |
} | |
alias PriceBars = TransformMembers!(SliceOf, TemplAppender!"s", PriceBar); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment