From 60f11f1fdd1e428f3e2203c2723cf7c7a282e561 Mon Sep 17 00:00:00 2001 From: Mike Wey Date: Sat, 7 May 2011 16:32:37 +0200 Subject: Use a struct/struct destructors to wrap ImageMagick Exception handling. --- dmagick/Exception.d | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'dmagick/Exception.d') diff --git a/dmagick/Exception.d b/dmagick/Exception.d index 3c89d3c..da05832 100644 --- a/dmagick/Exception.d +++ b/dmagick/Exception.d @@ -135,3 +135,63 @@ mixin( return exceptions; }()); +/** + * This struct is used to wrap the ImageMagick exception handling. + * Needs dmd >= 2.053 + * Usage: + * -------------------- + * CFunctionCall(param1, param2, DExceptionInfo()); + * -------------------- + */ +struct DMagickExcepionInfo +{ + ExceptionInfo* exceptionInfo; + + private bool isInitialized; + private size_t* refcount; + + alias exceptionInfo this; + + static DMagickExcepionInfo opCall() + { + DMagickExcepionInfo info; + + info.exceptionInfo = AcquireExceptionInfo(); + info.refcount = new size_t; + + *(info.refcount) = 1; + info.isInitialized = true; + + return info; + } + + this(this) + { + if ( isInitialized ) + (*refcount)++; + } + + ~this() + { + if ( !isInitialized ) + return; + + (*refcount)--; + + if ( *refcount == 0 ) + { + DMagickException.throwException(exceptionInfo); + exceptionInfo = DestroyExceptionInfo(exceptionInfo); + } + } +} + +unittest +{ + void testDMagickExcepionInfo(ExceptionInfo* info) + { + assert(info !is null); + } + + testDMagickExcepionInfo(DMagickExceptionInfo()); +} -- cgit v1.2.3