Slight improvement to enforceEx's constraints and its documentation.

This commit is contained in:
jmdavis 2014-06-21 21:30:11 -07:00
parent e63514cea4
commit 7b6123f6a6

View file

@ -520,9 +520,10 @@ T errnoEnforce(T, string file = __FILE__, size_t line = __LINE__)
enforceEx!DataCorruptionException(line.length); enforceEx!DataCorruptionException(line.length);
-------------------- --------------------
+/ +/
template enforceEx(E) template enforceEx(E : Throwable)
if (is(typeof(new E("", __FILE__, __LINE__)))) if (is(typeof(new E("", __FILE__, __LINE__))))
{ {
/++ Ditto +/
T enforceEx(T)(T value, lazy string msg = "", string file = __FILE__, size_t line = __LINE__) T enforceEx(T)(T value, lazy string msg = "", string file = __FILE__, size_t line = __LINE__)
{ {
if (!value) throw new E(msg, file, line); if (!value) throw new E(msg, file, line);
@ -530,9 +531,11 @@ template enforceEx(E)
} }
} }
template enforceEx(E) /++ Ditto +/
template enforceEx(E : Throwable)
if (is(typeof(new E(__FILE__, __LINE__))) && !is(typeof(new E("", __FILE__, __LINE__)))) if (is(typeof(new E(__FILE__, __LINE__))) && !is(typeof(new E("", __FILE__, __LINE__))))
{ {
/++ Ditto +/
T enforceEx(T)(T value, string file = __FILE__, size_t line = __LINE__) T enforceEx(T)(T value, string file = __FILE__, size_t line = __LINE__)
{ {
if (!value) throw new E(file, line); if (!value) throw new E(file, line);
@ -561,6 +564,24 @@ unittest
assert(e.file == "file"); assert(e.file == "file");
assert(e.line == 42); assert(e.line == 42);
} }
{
auto e = collectException!Error(enforceEx!OutOfMemoryError(false));
assert(e !is null);
assert(e.msg == "Memory allocation failed");
assert(e.file == __FILE__);
assert(e.line == __LINE__ - 4);
}
{
auto e = collectException!Error(enforceEx!OutOfMemoryError(false, "file", 42));
assert(e !is null);
assert(e.msg == "Memory allocation failed");
assert(e.file == "file");
assert(e.line == 42);
}
static assert(!is(typeof(enforceEx!int(true))));
} }
unittest unittest