1 /*******************************************************************************
2 3 Utility intended as a replacement for `assert` to check for programming
4 errors and sanity violations in situations when neither removing the check
5 in -release mode nor bringing down the application by throwing an `Error`
6 is acceptable.
7 8 This module must have as few import dependencies as possible so that it can
9 be used in place of `assert` freely without introducing cyclic imports.
10 11 Copyright: Copyright (c) 2017 dunnhumby Germany GmbH. All rights reserved
12 13 License:
14 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
15 Alternatively, this file may be distributed under the terms of the Tango
16 3-Clause BSD License (see LICENSE_BSD.txt for details).
17 18 *******************************************************************************/19 20 moduleocean.core.Verify;
21 22 importocean.meta.types.Qualifiers : istring;
23 24 /*******************************************************************************
25 26 Verifies that certain condition is met.
27 28 Params:
29 ok = boolean condition to check
30 msg = optional exception message
31 32 Throws:
33 SanityException if `ok` condition is `false`.
34 35 *******************************************************************************/36 37 publicvoidverify ( boolok, lazyistringmsg = "",
38 istringfile = __FILE__, intline = __LINE__ )
39 {
40 staticSanityExceptionexc;
41 42 if (!ok)
43 {
44 if (excisnull)
45 exc = newSanityException("");
46 47 exc.file = file;
48 exc.line = line;
49 exc.msg = msg;
50 51 throwexc;
52 }
53 }
54 55 unittest56 {
57 try58 {
59 verify(false);
60 }
61 catch (SanityExceptione) { }
62 63 verify(true);
64 }
65 66 /*******************************************************************************
67 68 Indicates some internal sanity violation in the app, essentially a less
69 fatal version of `AssertError`.
70 71 *******************************************************************************/72 73 publicclassSanityException : Exception74 {
75 publicthis ( istringmsg, istringfile = __FILE__, intline = __LINE__ )
76 {
77 super(msg, file, line);
78 }
79 }