Skip to content

Commit

Permalink
Remove code duplication in Variant fptr for compare selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapps committed Feb 14, 2014
1 parent 83b0cf5 commit 51fae69
Showing 1 changed file with 38 additions and 54 deletions.
92 changes: 38 additions & 54 deletions std/variant.d
Expand Up @@ -253,6 +253,38 @@ private:
return null;
}

static ptrdiff_t compare(A* rhsPA, A* zis, OpID selector)
{
static if (is(typeof(*rhsPA == *zis)))
{
if (*rhsPA == *zis)
{
return 0;
}
static if (is(typeof(*zis < *rhsPA)))
{
// Many types (such as any deriving from Object)
// will throw on an invalid opCmp, so do it only
// if the caller requests it.
if(selector == OpID.compare)
return *zis < *rhsPA ? -1 : 1;
else
return ptrdiff_t.min;
}
else
{
// Not equal, and type does not support ordering
// comparisons.
return ptrdiff_t.min;
}
}
else
{
// Type does not support comparisons at all.
return ptrdiff_t.min;
}
}

auto zis = getPtr(pStore);
// Input: TypeInfo object
// Output: target points to a copy of *me, if me was not null
Expand Down Expand Up @@ -338,34 +370,7 @@ private:
{
// cool! Same type!
auto rhsPA = getPtr(&rhsP.store);
static if (is(typeof(*rhsPA == *zis)))
{
if (*rhsPA == *zis)
{
return 0;
}
static if (is(typeof(*zis < *rhsPA)))
{
// Many types (such as any deriving from Object)
// will throw on an invalid opCmp, so do it only
// if the caller requests it.
if(selector == OpID.compare)
return *zis < *rhsPA ? -1 : 1;
else
return ptrdiff_t.min;
}
else
{
// Not equal, and type does not support ordering
// comparisons.
return ptrdiff_t.min;
}
}
else
{
// Type does not support comparisons at all.
return ptrdiff_t.min;
}
return compare(rhsPA, zis, selector);
} else if (rhsType == typeid(void))
{
// No support for ordering comparisons with
Expand All @@ -380,39 +385,18 @@ private:
// also fix up its fptr
temp.fptr = rhsP.fptr;
// now lhsWithRhsType is a full-blown VariantN of rhs's type
return temp.opCmp(*rhsP);
if(selector == OpID.compare)
return temp.opCmp(*rhsP);
else
return temp.opEquals(*rhsP) ? 0 : 1;
}
// Does rhs convert to zis?
*cast(TypeInfo*) &temp.store = typeid(A);
if (rhsP.fptr(OpID.get, &rhsP.store, &temp.store) == 0)
{
// cool! Now temp has rhs in my type!
auto rhsPA = getPtr(&temp.store);
static if (is(typeof(*rhsPA == *zis)))
{
if (*rhsPA == *zis)
{
return 0;
}
static if (is(typeof(*zis < *rhsPA)))
{
if(selector == OpID.compare)
return *zis < *rhsPA ? -1 : 1;
else
return ptrdiff_t.min;
}
else
{
// Not equal, and type does not support ordering
// comparisons.
return ptrdiff_t.min;
}
}
else
{
// Type does not support comparisons at all.
return ptrdiff_t.min;
}
return compare(rhsPA, zis, selector);
}
return ptrdiff_t.min; // dunno
case OpID.toString:
Expand Down

0 comments on commit 51fae69

Please sign in to comment.