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
|
/**
* Classes that wrap the Imagemagick exception handling.
*
* Copyright: Mike Wey 2011
* License: zlib (See accompanying LICENSE file)
* Authors: Mike Wey
*/
module dmagick.Exception;
import std.conv;
import dmagick.c.client;
import dmagick.c.exception;
/**
* A base class for all exceptions thrown bij DMagick.
* The following Exceptions are derived from this class:
*
* ResourceLimitException, TypeException, OptionException,
* DelegateException, MissingDelegateException, CorruptImageException,
* FileOpenException, BlobException, StreamException, CacheException,
* CoderException, FilterException, ModuleException, DrawException,
* ImageException, WandException, RandomException, XServerException,
* MonitorException, RegistryException, ConfigureException, PolicyException
*/
class DMagickException : Exception
{
this(string reason, string description = null, string file = __FILE__, size_t line = __LINE__)
{
string message = to!(string)(GetClientName());
if ( reason.length > 0 )
message ~= ": " ~ reason;
if ( description.length > 0 )
message ~= " (" ~ description ~ ")";
super(message, file, line);
}
private enum string[] severities = [ "Blob", "Cache", "Coder",
"Configure", "CorruptImage", "Delegate", "Draw", "FileOpen",
"Filter", "Image", "MissingDelegate", "Module", "Monitor",
"Option", "Policy", "Random", "Registry", "ResourceLimit",
"Stream", "Type", "Wand", "XServer" ];
/**
* Throws an Exception or error matching the ExceptionInfo.
*/
static void throwException(ExceptionInfo* exception, string file = __FILE__, size_t line = __LINE__)
{
if ( exception.severity == ExceptionType.UndefinedException )
return;
string reason = to!(string)(exception.reason);
string description = to!(string)(exception.description);
scope(exit) exception = DestroyExceptionInfo(exception);
mixin(
{
string exceptions =
"switch ( exception.severity )
{";
foreach ( severity; severities )
{
//TODO: Warnings?
exceptions ~=
"case ExceptionType."~ severity ~"Error:
throw new "~ severity ~"Exception(reason, description, file, line);
break;
case ExceptionType."~ severity ~"FatalError:
throw new "~ severity ~"Error(reason, description, file, line);
break;";
}
return exceptions ~=
" default:
return;
}";
}());
}
}
/**
* A base class for all errors thrown bij DMagick.
* The following Errors are derived from this class:
*
* ResourceLimitError, TypeError, OptionError,
* DelegateError, MissingDelegateError, CorruptImageError,
* FileOpenError, BlobError, StreamError, CacheError,
* CoderError, FilterError, ModuleError, DrawError,
* ImageError, WandError, RandomError, XServerError,
* MonitorError, RegistryError, ConfigureError, PolicyError
*/
class DMagickError : Error
{
this(string reason, string description = null, string file = __FILE__, size_t line = __LINE__)
{
string message = to!(string)(GetClientName());
if ( reason.length > 0 )
message ~= ": " ~ reason;
if ( description.length > 0 )
message ~= " (" ~ description ~ ")";
super(message, file, line);
}
}
/**
* Generate the exceptions and the throwException function;
*/
mixin(
{
string exceptions;
foreach ( severity; DMagickException.severities )
{
exceptions ~=
"class " ~ severity ~ "Exception : DMagickException
{
this(string reason, string description = null, string file = __FILE__, size_t line = __LINE__)
{
super(reason, description, file, line);
}
}";
exceptions ~=
"class " ~ severity ~ "Error : DMagickError
{
this(string reason, string description = null, string file = __FILE__, size_t line = __LINE__)
{
super(reason, description, file, line);
}
}";
}
return exceptions;
}());
/**
* This struct is used to wrap the ImageMagick exception handling.
* Needs dmd >= 2.053
* Usage:
* --------------------
* CFunctionCall(param1, param2, DExceptionInfo());
* --------------------
*/
struct DMagickExceptionInfo
{
ExceptionInfo* exceptionInfo;
string file;
size_t line;
private bool isInitialized;
private size_t* refcount;
alias exceptionInfo this;
static DMagickExceptionInfo opCall(string file = __FILE__, size_t line = __LINE__)
{
DMagickExceptionInfo info;
info.exceptionInfo = AcquireExceptionInfo();
info.refcount = new size_t;
*(info.refcount) = 1;
info.isInitialized = true;
info.file = file;
info.line = line;
return info;
}
this(this)
{
if ( isInitialized )
(*refcount)++;
}
~this()
{
if ( !isInitialized )
return;
(*refcount)--;
if ( *refcount == 0 )
{
DMagickException.throwException(exceptionInfo, file, line);
}
}
}
unittest
{
void testDMagickExcepionInfo(ExceptionInfo* info)
{
assert(info !is null);
}
testDMagickExcepionInfo(DMagickExceptionInfo());
}
|