From 15a9ae7941bccdb2bfd61be954262c442f6ea5a8 Mon Sep 17 00:00:00 2001 From: monarchdodra Date: Tue, 13 May 2014 22:30:26 +0200 Subject: [PATCH] Duplicate pointsTo as "doesPointTo" --- std/exception.d | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/std/exception.d b/std/exception.d index ac33c3e27..98e5492fe 100644 --- a/std/exception.d +++ b/std/exception.d @@ -945,11 +945,48 @@ bool pointsTo(S, T, Tdummy=void)(auto ref const S source, ref const T target) @t return false; } } + +bool mayPointTo(S, T, Tdummy=void)(auto ref const S source, ref const T target) @trusted pure nothrow + if (__traits(isRef, source) || isDynamicArray!S || + isPointer!S || is(S == class)) +{ + static if (isPointer!S || is(S == class)) + { + const m = cast(void*) source, + b = cast(void*) &target, e = b + target.sizeof; + return b <= m && m < e; + } + else static if (is(S == struct) || is(S == union)) + { + foreach (i, Subobj; typeof(source.tupleof)) + if (mayPointTo(source.tupleof[i], target)) return true; + return false; + } + else static if (isStaticArray!S) + { + foreach (size_t i; 0 .. S.length) + if (mayPointTo(source[i], target)) return true; + return false; + } + else static if (isDynamicArray!S) + { + return overlap(cast(void[])source, cast(void[])(&target)[0 .. 1]).length != 0; + } + else + { + return false; + } +} + // for shared objects bool pointsTo(S, T)(auto ref const shared S source, ref const shared T target) @trusted pure nothrow { return pointsTo!(shared S, shared T, void)(source, target); } +bool mayPointTo(S, T)(auto ref const shared S source, ref const shared T target) @trusted pure nothrow +{ + return mayPointTo!(shared S, shared T, void)(source, target); +} /// Pointers unittest