1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/*
Asserte functions. This file is part of BioD.
Copyright (C) 2018 Pjotr Prins <pjotr.prins@thebird.nl>
*/
// This code is based on 'exception.d' in D's Phobos
module bio.core.utils.exception;
import std.exception;
import std.traits;
/++
Asserts that the given value is true, but unlike standard assert
throws an exception on error.
Params:
value = The value to test.
ex = The exception to throw if the value evaluates to false.
Returns: $(D value), if `cast(bool)value` is true. Otherwise, $(D ex) is
thrown.
Example:
--------------------
auto f = asserte(fopen("data.txt"));
auto line = readln(f);
asserte(line.length, new IOException); // expect a non-empty line
--------------------
+/
T asserte(T)(T value, lazy Throwable ex)
{
version(assert) {
if (!value) throw ex();
}
return value;
}
T asserte(T)(T value, lazy string msg = "asserte failed")
{
version(assert) {
if (!value) throw new Exception(msg);
}
return value;
}
/++
Asserts that the given value is true, but unlike standard assert
throws an exception on error.
Params:
value = The value to test.
dg = The delegate to be called if the value evaluates to false.
file = The source file of the caller.
line = The line number of the caller.
Returns: $(D value), if `cast(bool)value` is true. Otherwise, the given
delegate is called.
The safety and purity of this function are inferred from $(D Dg)'s safety
and purity.
+/
T asserte(T, Dg, string file = __FILE__, size_t line = __LINE__)
(T value, scope Dg dg)
if (isSomeFunction!Dg && is(typeof( dg() )) &&
is(typeof({ if (!value) {} })))
{
version(assert) {
if (!value) dg();
}
return value;
}
|