From 313139cfb568823c854ba88a69f42da150cb342b Mon Sep 17 00:00:00 2001 From: LunaTheFoxgirl Date: Sun, 26 May 2024 19:16:43 +0200 Subject: [PATCH] Add exception support to numem --- source/numem/mem/exception.d | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 source/numem/mem/exception.d diff --git a/source/numem/mem/exception.d b/source/numem/mem/exception.d new file mode 100644 index 0000000..248ffad --- /dev/null +++ b/source/numem/mem/exception.d @@ -0,0 +1,92 @@ +/** + Numem Exception support +*/ +module numem.mem.exception; +import numem.mem.string; +import numem.mem; +import core.stdc.stdio; + +@nogc: + +/** + An exception which can be thrown from numem +*/ +class NuException : Exception { +nothrow @nogc: +private: + nstring _msg; + +public: + + ~this() { + // Debug build printing + debug printf("Exception freed! (msg=%s)\n", _msg.toCString()); + + nogc_delete(_msg); + + // Free next-in-chain + Throwable t = this.next(); + nogc_delete(t); + } + + /** + Constructs a nogc exception + */ + this(nstring msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__) { + this._msg = msg; + super(this._msg.toDString(), nextInChain, file, line); + } + + /** + Constructs a nogc exception + */ + this(nstring msg, string file = __FILE__, size_t line = __LINE__) { + this(msg, null, file, line); + } + + /** + Returns the error message + */ + override + @__future const(char)[] message() const @safe nothrow { + return this.msg; + } + + /** + Helper function to free this exception + */ + void free() { + NuException ex = this; + nogc_delete(ex); + } +} + +/** + Enforces the truthiness of a nstring +*/ +void enforce(T)(T in_, nstring err) { + if (!in_) { + throw nogc_new!NuException(err); + } +} + +/** + Enforces the truthiness of a nstring +*/ +void enforce(T)(T in_, lazy NuException t) { + if (!in_) { + throw t; + } +} + +@("Test catching exceptions") +unittest { + auto str = nstring("Ooops!"); + try { + enforce(false, str); + } catch(NuException ex) { + assert(ex.message() == str.toDString()); + + ex.free(); + } +} \ No newline at end of file