Changeset 1706
- Timestamp:
- 07/01/10 18:54:26 (14 years ago)
- Files:
-
- trunk/phobos/std/contracts.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/contracts.d
r1499 r1706 88 88 89 89 T enforce(T, string file = __FILE__, int line = __LINE__) 90 90 (T value, scope void delegate() dg) 91 91 { 92 92 if (!value) dg(); 93 93 return value; 94 94 } 95 95 96 96 private void bailOut(string file, int line, in char[] msg) 97 97 { 98 throw new Exception(text(file, '(', line, "): ", 99 msg ? msg : "Enforcement failed")); 98 throw new Exception(msg ? msg.idup : "Enforcement failed", file, line); 99 } 100 101 unittest 102 { 103 assert (enforce(123) == 123); 104 105 try 106 { 107 enforce(false, "error"); 108 assert (false); 109 } 110 catch (Exception e) 111 { 112 assert (e.msg == "error"); 113 assert (e.file == __FILE__); 114 assert (e.line == __LINE__-7); 115 } 100 116 } 101 117 102 118 /** 103 119 * If $(D_PARAM value) is nonzero, returns it. Otherwise, throws 104 120 * $(D_PARAM ex). 105 121 * Example: 106 122 * ---- 107 123 * auto f = enforce(fopen("data.txt")); 108 124 * auto line = readln(f); 109 125 * enforce(line.length, new IOException); // expect a non-empty line
