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 module ocean.core.Verify;
21 
22 import ocean.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 public void verify ( bool ok, lazy istring msg = "",
38     istring file = __FILE__, int line = __LINE__ )
39 {
40     static SanityException exc;
41 
42     if (!ok)
43     {
44         if (exc is null)
45             exc = new SanityException("");
46 
47         exc.file = file;
48         exc.line = line;
49         exc.msg = msg;
50 
51         throw exc;
52     }
53 }
54 
55 unittest
56 {
57     try
58     {
59         verify(false);
60     }
61     catch (SanityException e) { }
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 public class SanityException : Exception
74 {
75     public this ( istring msg, istring file = __FILE__, int line = __LINE__ )
76     {
77         super(msg, file, line);
78     }
79 }