From d901bfa81019ddb781e92ba83f93fc264477ef50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 13 Aug 2015 15:59:50 +0200 Subject: [PATCH] Add UFCS functions get and hasType. --- source/taggedalgebraic.d | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/source/taggedalgebraic.d b/source/taggedalgebraic.d index df2078e..3a925aa 100644 --- a/source/taggedalgebraic.d +++ b/source/taggedalgebraic.d @@ -180,7 +180,27 @@ struct TaggedAlgebraic(U) if (is(U == union) || is(U == struct)) assert(false); // never reached } - private @trusted @property ref inout(typeof(__traits(getMember, U, f))) trustedGet(string f)() inout { return *cast(inout(typeof(__traits(getMember, U, f)))*)m_data.ptr; } + private @trusted @property ref inout(typeof(__traits(getMember, U, f))) trustedGet(string f)() inout { return trustedGet!(inout(typeof(__traits(getMember, U, f)))); } + private @trusted @property ref inout(T) trustedGet(T)() inout { return *cast(inout(T)*)m_data.ptr; } +} + +bool hasType(T, U)(in ref TaggedAlgebraic!U ta) +{ + switch (ta.typeID) { + default: return false; + foreach (i, FT; ta.FieldTypes) + static if (is(FT == T)) { + case __traits(getMember, ta.Type, ta.fieldNames[i]): + return true; + } + } + assert(false); // never reached +} + +ref inout(T) get(T, U)(ref inout(TaggedAlgebraic!U) ta) +{ + assert(hasType!(T, U)(ta)); + return ta.trustedGet!T; }