SmartInt

Wrapper for any basic integral type N that uses the checked operations from smartOp and bounds checks assignments with checkedint.to().

  • policy controls the error signalling policy (see checkedint.flags).
  • bitOps may be set to No.bitOps if desired, to turn bitwise operations on this type into a compile-time error.

Constructors

this
this(const M that)

Assign the value of that to this SmartInt instance.

Members

Functions

abs
SmartInt!(Unsigned!N, policy, bitOps) abs()

See smartOp.

bsf
SmartInt!(ubyte, policy, bitOps) bsf()
bsr
SmartInt!(ubyte, policy, bitOps) bsr()
divPow2
auto divPow2(const M exp)
auto divPow2(const M exp)
ilogb
SmartInt!(ubyte, policy, bitOps) ilogb()
modPow2
auto modPow2(const M exp)
auto modPow2(const M exp)
mulPow2
auto mulPow2(const M exp)
auto mulPow2(const M exp)

See smartOp.

opAssign
typeof(this) opAssign(const M that)

Assign the value of that to this SmartInt instance.

opBinary
auto opBinary(const M right)
auto opBinary(const M right)
opBinaryRight
auto opBinaryRight(const M left)
auto opBinaryRight(const M left)

See smartOp.

opCast
M opCast()

Convert this value to floating-point. This always succeeds, although some loss of precision may occur if M.sizeof <= N.sizeof.

opCast
M opCast()

this != 0

opCast
M opCast()

Convert this value to type M using checkedint.to() for bounds checking. An IntFlag will be raised if M cannot represent the current value of this SmartInt.

opCmp
auto opCmp(const M right)
int opCmp(const M right)

Perform a mathematically correct comparison to right.

opEquals
bool opEquals(const M right)

Returns true if this value is mathematically precisely equal to right.

opOpAssign
typeof(this) opOpAssign(const M right)

See smartOp.

opUnary
typeof(this) opUnary()
SmartInt!(Signed!N, policy, bitOps) opUnary()
typeof(this) opUnary()

See smartOp.

popcnt
SmartInt!(int, policy, bitOps) popcnt()

Count the number of set bits using core.bitop.popcnt().

pow
auto pow(const M exp)

Raise this to the exp power using std.math.pow().

pow
auto pow(const M exp)

See smartOp.

toHash
size_t toHash()

Get a simple hashcode for this value.

toString
string toString()

Get a string representation of this value.

toString
void toString(Writer w, FormatSpec!Char fmt = (FormatSpec!Char).init)

Puts a string representation of this value into w. This overload will not allocate, unless std.range.primitives.put(w, ...) allocates.

Properties

bits
inout(SmartInt!(N, policy, Yes.bitOps)) bits [@property getter]

Get a view of this SmartInt that allows bitwise operations.

idx
Select!(isSigned!N, ptrdiff_t, size_t) idx [@property getter]

Convert this value to a type suitable for indexing an array:

  • If N is signed, a ptrdiff_t is returned.
  • If N is unsigned, a size_t is returned.

checkedint.to() is used for bounds checking.

Variables

bscal
N bscal;

The basic scalar value of this SmartInt. Accessing this directly may be useful for:

  • Intentionally doing modular (unchecked) arithmetic, or
  • Interacting with APIs that are not checkedint aware.
max
enum SmartInt!(N, policy, bitOps) max;

The most positive possible value of this SmartInt type.

min
enum SmartInt!(N, policy, bitOps) min;

The most negative possible value of this SmartInt type.

policy
enum IntFlagPolicy policy;

The error signalling policy used by this SmartInt type.

Examples

1 // Mixing standard signed and unsigned types is dangerous, but...
2 int ba = -1;
3 uint bb = 0;
4 assert(ba > bb);
5 
6 auto bc = ba + bb;
7 assert(is(typeof(bc) == uint));
8 assert(bc == 4294967295u);
9 
10 // ...with SmartInt, mixed signed/unsigned operations "just work":
11 import checkedint.throws : SmartInt; // use IntFlagPolicy.throws
12 
13 SmartInt!int ma = -1;
14 SmartInt!uint mb = 0;
15 assert(ma < mb);
16 
17 auto mc = ma + mb;
18 assert(is(typeof(mc) == SmartInt!int));
19 assert(mc != 4294967295u);
20 assert(mc == -1);
1 // When IntFlagPolicy.throws is used, failed SmartInt operations will throw a CheckedIntException.
2 import checkedint.throws : SmartInt;
3 
4 SmartInt!uint ma = 1;
5 SmartInt!uint mb = 0;
6 
7 bool overflow = false;
8 try
9 {
10     SmartInt!uint mc = mb - ma;
11     assert(false);
12 }
13 catch (CheckedIntException e)
14 {
15     assert(e.intFlags == IntFlag.negOver);
16     overflow = true;
17 }
18 assert(overflow);
19 
20 bool div0 = false;
21 try
22 {
23     // With standard integers, this would crash the program with an unrecoverable FPE...
24     SmartInt!uint mc = ma / mb;
25     assert(false);
26 }
27 catch (CheckedIntException e)
28 {
29     // ...but with SmartInt, it just throws a normal Exception.
30     assert(e.intFlags == IntFlag.div0);
31     div0 = true;
32 }
33 assert(div0);
1 // When IntFlagPolicy.sticky is used, failed SmartInt operations set one or more bits in IntFlags.local.
2 import checkedint.sticky : SmartInt;
3 
4 SmartInt!uint ma = 1;
5 SmartInt!uint mb = 0;
6 SmartInt!uint mc;
7 
8 mc = mb - ma;
9 assert(IntFlags.local == IntFlag.negOver);
10 
11 // With standard integers, this would crash the program with an unrecoverable FPE...
12 mc = ma / mb;
13 // ...but with SmartInt, it just sets a bit in IntFlags.local.
14 assert(IntFlags.local & IntFlag.div0);
15 
16 // Each flag will remain set until cleared:
17 assert(IntFlags.local.clear() == (IntFlag.negOver | IntFlag.div0));
18 assert(!IntFlags.local);

Meta