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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/++
`@nogc` exceptions and errors definitions.
Most of the API Requires DIP1008.
+/
module mir.exception;
version(D_Exceptions):
version(D_Ddoc)
private enum _version_D_Ddoc = true;
else
private enum _version_D_Ddoc = false;
private enum NOGCEXP = __traits(compiles, (()@nogc {throw new Exception("");})());
private enum HASFORMAT = __traits(compiles, (()@nogc {import mir.format;})());
package template staticException(string fmt, string file, int line)
{
static immutable staticException = new Exception(fmt, file, line);
}
@trusted pure nothrow @nogc
Exception toMutable()(immutable Exception e)
{
return cast() e;
}
@trusted pure nothrow @nogc
Error toMutable()(immutable Error e)
{
return cast() e;
}
@trusted pure nothrow @nogc
Exception toMutable()(const Exception e)
{
return cast() e;
}
@trusted pure nothrow @nogc
Error toMutable()(const Error e)
{
return cast() e;
}
///
auto ref enforce(string fmt, string file = __FILE__, int line = __LINE__, Expr)(scope auto return ref Expr arg) @trusted
{
version(LDC) pragma(inline, true);
import core.lifetime: forward;
import mir.utility: _expect;
static if (__traits(compiles, arg !is null))
{
if (_expect(arg !is null, true))
return forward!arg;
}
else
{
if (_expect(cast(bool)arg, true))
return forward!arg;
}
throw staticException!(fmt, file, line).toMutable;
}
///
@safe pure nothrow @nogc
version (mir_core_test) unittest
{
import mir.exception;
try enforce!"Msg"(false);
catch(Exception e) assert(e.msg == "Msg");
}
/++
+/
class MirException : Exception
{
///
mixin MirThrowableImpl;
}
/// Generic style
version (mir_test) static if (NOGCEXP && HASFORMAT)
@safe pure nothrow @nogc
unittest
{
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
import mir.exception;
try throw new MirException("Hi D", 2, "!");
catch(MirException e) assert(e.scopeMessage == "Hi D2!");
}
}
/// Generic style, GC allocated MSG
version (mir_test) static if (NOGCEXP && HASFORMAT)
@safe pure nothrow @nogc
unittest
{
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
import mir.exception;
try throw new MirException("Hi D", 2, "!");
catch(Exception e) assert(e.message == "Hi D2!");
}
}
/// C++ style
version (mir_test) static if (NOGCEXP && HASFORMAT)
@safe pure nothrow @nogc
unittest
{
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
import mir.exception;
import mir.format;
try throw new MirException(stringBuf() << "Hi D" << 2 << "!" << getData);
catch(Exception e) assert(e.scopeMessage == "Hi D2!");
}
}
/// Low-level style
version (mir_test) static if (NOGCEXP && HASFORMAT)
@safe pure nothrow @nogc
unittest
{
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
import mir.exception;
import mir.format;
auto buffer = stringBuf();
try throw new MirException(buf.print( "Hi D", 2, "!").data);
catch(Exception e) assert(e.msg == "Hi D2!");
}
}
///
version (mir_core_test) static if (NOGCEXP)
@safe pure nothrow @nogc
unittest
{
@safe pure nothrow @nogc
bool func(scope const(char)[] msg)
{
/// scope messages are copied
try throw new MirException(msg);
catch(Exception e) assert(e.msg == msg);
/// immutable strings are not copied
static immutable char[] gmsg = "global msg";
try throw new MirException(gmsg);
catch(Exception e) assert(e.msg is gmsg);
return __ctfe;
}
assert(func("runtime-time check") == 0);
static assert(func("compile-time check") == 1);
}
// ///
// auto ref enforce(T, Args...)(scope auto return ref T arg, lazy @nogc Args args, string file = __FILE__, int line = __LINE__) @nogc
// if (Args.length)
// {
// import mir.utility: _expect;
// static if (__traits(compiles, arg !is null))
// {
// if (_expect(arg !is null, true))
// return arg;
// }
// else
// {
// if (_expect(cast(bool)arg, true))
// return arg;
// }
// import mir.format;
// stringBuf buf;
// throw new MirException(buf.print(args).data, file, line);
// }
// ///
// @safe pure nothrow @nogc
// version (mir_core_test) unittest static if (NOGCEXP && HASFORMAT)
// {
// import mir.exception;
// try enforce(false, "Hi D", 2, "!");
// catch(Exception e) assert(e.msg == "Hi D2!");
// }
// ///
// auto ref enforce(T)(scope auto return ref T arg, lazy scope const(char)[] msg, string file = __FILE__, int line = __LINE__) @nogc
// {
// import core.lifetime: forward;
// import mir.utility: _expect;
// static if (__traits(compiles, arg !is null))
// {
// if (_expect(arg !is null, true))
// return forward!arg[0];
// }
// else
// {
// if (_expect(cast(bool)arg, true))
// return forward!arg[0];
// }
// throw new MirException(msg, file, line);
// }
// ///
// @safe pure nothrow @nogc
// version (mir_core_test) unittest static if (NOGCEXP && HASFORMAT)
// {
// import mir.exception;
// try enforce(false, "Msg");
// catch(Exception e) assert(e.msg == "Msg");
// }
/++
+/
class MirError : Error
{
///
mixin MirThrowableImpl;
}
///
@system pure nothrow @nogc
version (mir_test) static if (NOGCEXP)
unittest
{
@system pure nothrow @nogc
bool func(scope const(char)[] msg)
{
/// scope messages are copied
try throw new MirException(msg);
catch(Exception e) assert(e.msg == msg);
/// immutable strings are not copied
static immutable char[] gmsg = "global msg";
try throw new MirError(gmsg);
catch(Error e) assert(e.msg is gmsg);
return __ctfe;
}
assert(func("runtime-time check") == 0);
static assert(func("compile-time check") == 1);
}
/++
+/
mixin template MirThrowableImpl()
{
private bool _global;
private char[maxMirExceptionMsgLen] _payload = void;
import mir.exception: maxMirExceptionMsgLen, mirExceptionInitilizePayloadImpl;
const(char)[] _msg;
override string message() const @safe pure nothrow
{
return _msg ? _msg.idup : msg;
}
const(char)[] scopeMessage() scope const @safe pure nothrow @nogc
{
return _msg ? _msg : msg;
}
/++
Params:
msg = message. No-scope `msg` is assumed to have the same lifetime as the throwable. scope strings are copied to internal buffer.
file = file name, zero terminated global string
line = line number
nextInChain = next exception in the chain (optional)
+/
@nogc @trusted pure nothrow this(scope const(char)[] msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
{
this._msg = mirExceptionInitilizePayloadImpl(_payload, msg);
super(cast(immutable)this._msg, file, line, nextInChain);
}
/// ditto
@nogc @trusted pure nothrow this(scope const(char)[] msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
{
this._msg = mirExceptionInitilizePayloadImpl(_payload, msg);
super(cast(immutable)this._msg, file, line, nextInChain);
}
/// ditto
@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
{
this._global = true;
super(msg, file, line, nextInChain);
}
/// ditto
@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
{
this._global = true;
super(msg, file, line, nextInChain);
}
///
~this() @trusted
{
import mir.internal.memory: free;
if (!_global && msg.ptr != _payload.ptr)
free(cast(void*)msg.ptr);
}
/++
Generic multiargument overload.
Constructs a string using the `print` function.
+/
this(Args...)(auto ref scope const Args args, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null) pure
if (Args.length > 1 && !is(Args[$ - 1] == Throwable))
{
static assert (__traits(compiles, {import mir.format;}), "MirThrowableImpl needs mir-algorithm for mir.format and exception formatting.");
import mir.format;
auto buf = stringBuf();
foreach(ref arg; args)
buf.print(arg);
this(buf.data, file, line, nextInChain);
}
this(Args...)(auto ref scope const Args args) pure @trusted
if (Args.length > 4 && is(Args[$ - 1] == Throwable))
{
static assert (__traits(compiles, {import mir.format;}), "MirThrowableImpl needs mir-algorithm for mir.format and exception formatting.");
import mir.format;
auto buf = stringBuf();
foreach(ref arg; args[0 .. $ - 3])
buf.print(arg);
this(buf.data, args[$ - 3 .. $ - 1], cast() args[$ - 1]);
}
}
///
enum maxMirExceptionMsgLen = 447;
pragma(inline, false)
pure nothrow @nogc @safe
const(char)[] mirExceptionInitilizePayloadImpl(ref return char[maxMirExceptionMsgLen] payload, scope const(char)[] msg)
{
import mir.internal.memory: malloc;
import core.stdc.string: memcpy;
if (msg.length > payload.length)
{
if (auto ret = (() @trusted
{
if (__ctfe)
return null;
if (auto ptr = malloc(msg.length))
{
memcpy(ptr, msg.ptr, msg.length);
return cast(const(char)[]) ptr[0 .. msg.length];
}
return null;
})())
return ret;
msg = msg[0 .. payload.length];
// remove tail UTF-8 symbol chunk if any
uint c = msg[$-1];
if (c > 0b_0111_1111)
{
do {
c = msg[$-1];
msg = msg[0 .. $ - 1];
}
while (msg.length && c < 0b_1100_0000);
}
}
if (__ctfe)
payload[][0 .. msg.length] = msg;
else
(() @trusted => memcpy(payload.ptr, msg.ptr, msg.length))();
return payload[0 .. msg.length];
}
|