The OpenD Programming Language

Checked

Checked integral type wraps an integral T and customizes its behavior with the help of a Hook type. The type wrapped must be one of the predefined integrals (unqualified), or another instance of Checked.

Constructors

this
this(U rhs)

Constructor taking a value properly convertible to the underlying type. U may be either an integral that can be converted to T without a loss, or another Checked instance whose representation may be in turn converted to T without a loss.

this
this(Range str)

Construct from a decimal string. The conversion follows the same rules as std.conv.to converting a string to the wrapped T type.

Members

Aliases

Representation
alias Representation = T

The type of the integral subject to checking.

Functions

get
auto get()
opAssign
Checked opAssign(U rhs)

Assignment operator. Has the same constraints as the constructor.

opBinary
auto opBinary(Rhs rhs)
auto opBinary(Checked!(U, Hook1) rhs)

Defines binary operators +, -, *, /, %, ^^, &, |, ^, <<, >>, and >>>. If Hook defines hookOpBinary, opBinary forwards to Checked!(typeof(hook.hookOpBinary!op(get, rhs)), Hook)(hook.hookOpBinary!op(get, rhs)).

opBinaryRight
auto opBinaryRight(Lhs lhs)

Defines binary operators +, -, *, /, %, ^^, &, |, ^, <<, >>, and >>> for the case when a built-in numeric or Boolean type is on the left-hand side, and a Checked instance is on the right-hand side.

opCast
U opCast()

Casting operator to integral, bool, or floating point type.

opCmp
auto opCmp(U rhs)
auto opCmp(Checked!(U, Hook1) rhs)

Compares this against rhs for ordering. If Hook defines hookOpCmp, the function forwards to hook.hookOpCmp(get, rhs). Otherwise, the result of the built-in comparison operation is returned.

opEquals
bool opEquals(U rhs)

Compares this against rhs for equality.

opOpAssign
Checked opOpAssign(Rhs rhs)

Defines operators +=, -=, *=, /=, %=, ^^=, &=, |=, ^=, <<=, >>=, and >>>=.

opUnary
auto opUnary()
Checked opUnary()

Defines unary operators +, -, ~, ++, and --. Unary + is not overridable and always has built-in behavior (returns this). For the others, if Hook defines hookOpUnary, opUnary forwards to Checked!(typeof(hook.hookOpUnary!op(get)), Hook)(hook.hookOpUnary!op(get)).

toHash
size_t toHash()

Generates a hash for this. If Hook defines hookToHash, the call immediately returns hook.hookToHash(payload). If Hook does not implement hookToHash, but it has state, a hash will be generated for the Hook using the built-in function and it will be xored with the hash of the payload.

toString
void toString(Writer sink, FormatSpec!Char fmt)

Writes a string representation of this to a sink.

Variables

hook
Hook hook;

hook is a member variable if it has state, or an alias for Hook otherwise.

max
enum Checked!(T, Hook) max;

Defines the minimum and maximum. These values are hookable by defining Hook.min and/or Hook.max.

min
enum Checked!(T, Hook) min;

Defines the minimum and maximum. These values are hookable by defining Hook.min and/or Hook.max.

Parameters

T

type that is wrapped in the Checked type

Hook

hook type that customizes the behavior of the Checked type

Examples

// Hook that ignores all problems.
static struct Ignore
{
    @nogc nothrow pure @safe static:
    Dst onBadCast(Dst, Src)(Src src) { return cast(Dst) src; }
    Lhs onLowerBound(Rhs, T)(Rhs rhs, T bound) { return cast(T) rhs; }
    T onUpperBound(Rhs, T)(Rhs rhs, T bound) { return cast(T) rhs; }
    bool hookOpEquals(Lhs, Rhs)(Lhs lhs, Rhs rhs) { return lhs == rhs; }
    int hookOpCmp(Lhs, Rhs)(Lhs lhs, Rhs rhs) { return (lhs > rhs) - (lhs < rhs); }
    typeof(~Lhs()) onOverflow(string x, Lhs)(ref Lhs lhs) { return mixin(x ~ "lhs"); }
    typeof(Lhs() + Rhs()) onOverflow(string x, Lhs, Rhs)(Lhs lhs, Rhs rhs)
    {
        static if (x == "/")
            return typeof(lhs / rhs).min;
        else
            return mixin("lhs" ~ x ~ "rhs");
    }
}

auto x = Checked!(int, Ignore)(5) + 7;

Meta