Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 748ab6a

Browse files
committed
[[ Foundation ]] Add new forms of MCError construction
This patch adds new constructors for MCError, allowing varargs or va_list forms.
1 parent d845947 commit 748ab6a

2 files changed

Lines changed: 127 additions & 33 deletions

File tree

libfoundation/include/foundation.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2813,9 +2813,23 @@ MC_DLLEXPORT extern MCTypeInfoRef kMCUnboundTypeErrorTypeInfo;
28132813
MC_DLLEXPORT extern MCTypeInfoRef kMCUnimplementedErrorTypeInfo;
28142814

28152815
MC_DLLEXPORT bool MCErrorCreate(MCTypeInfoRef typeinfo, MCArrayRef info, MCErrorRef& r_error);
2816+
MC_DLLEXPORT bool MCErrorCreateS(MCErrorRef& r_error,
2817+
MCTypeInfoRef typeinfo,
2818+
...);
2819+
MC_DLLEXPORT bool MCErrorCreateV(MCErrorRef& r_error,
2820+
MCTypeInfoRef typeinfo,
2821+
va_list args);
28162822

28172823
MC_DLLEXPORT bool MCErrorCreateWithMessage(MCTypeInfoRef typeinfo, MCStringRef message, MCArrayRef info, MCErrorRef & r_error);
2818-
2824+
MC_DLLEXPORT bool MCErrorCreateWithMessageS(MCErrorRef& r_error,
2825+
MCTypeInfoRef typeinfo,
2826+
MCStringRef message,
2827+
...);
2828+
MC_DLLEXPORT bool MCErrorCreateWithMessageV(MCErrorRef& r_error,
2829+
MCTypeInfoRef typeinfo,
2830+
MCStringRef message,
2831+
va_list args);
2832+
28192833
MC_DLLEXPORT bool MCErrorUnwind(MCErrorRef error, MCValueRef target, uindex_t row, uindex_t column);
28202834

28212835
MC_DLLEXPORT MCNameRef MCErrorGetDomain(MCErrorRef error);

libfoundation/src/foundation-error.cpp

Lines changed: 112 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,79 @@ MCErrorCreateWithMessage (MCTypeInfoRef p_typeinfo,
154154
return true;
155155
}
156156

157+
MC_DLLEXPORT_DEF
158+
bool MCErrorCreateWithMessageV(MCErrorRef& r_error,
159+
MCTypeInfoRef p_error_type,
160+
MCStringRef p_message,
161+
va_list p_args)
162+
{
163+
__MCAssertIsErrorTypeInfo(p_error_type);
164+
__MCAssertIsString(p_message);
165+
MCAutoArrayRef t_info;
166+
if (!MCArrayCreateMutable(&t_info))
167+
return false;
168+
169+
for(;;)
170+
{
171+
const char *t_key;
172+
t_key = va_arg(p_args, const char *);
173+
if (t_key == nil)
174+
break;
175+
176+
MCValueRef t_value;
177+
t_value = va_arg(p_args, MCValueRef);
178+
179+
// If a value is nil, then it means don't include it.
180+
if (t_value == nil)
181+
continue;
182+
183+
MCNewAutoNameRef t_name;
184+
if (!MCNameCreateWithNativeChars((const char_t *)t_key,
185+
strlen(t_key),
186+
&t_name))
187+
return false;
188+
189+
if (!MCArrayStoreValue(*t_info,
190+
true,
191+
*t_name,
192+
t_value))
193+
{
194+
return false;
195+
}
196+
}
197+
198+
if (!MCErrorCreateWithMessage(p_error_type,
199+
p_message,
200+
*t_info,
201+
r_error))
202+
{
203+
return false;
204+
}
205+
206+
return true;
207+
}
208+
209+
MC_DLLEXPORT_DEF
210+
bool MCErrorCreateWithMessageS(MCErrorRef& r_error,
211+
MCTypeInfoRef p_error_type,
212+
MCStringRef p_message,
213+
...)
214+
{
215+
va_list t_args;
216+
va_start(t_args, p_message);
217+
218+
bool t_result;
219+
t_result = MCErrorCreateWithMessageV(r_error,
220+
p_error_type,
221+
p_message,
222+
t_args);
223+
224+
va_end(t_args);
225+
226+
return t_result;
227+
228+
}
229+
157230
MC_DLLEXPORT_DEF
158231
bool MCErrorCreate(MCTypeInfoRef p_typeinfo, MCArrayRef p_info, MCErrorRef& r_error)
159232
{
@@ -163,6 +236,35 @@ bool MCErrorCreate(MCTypeInfoRef p_typeinfo, MCArrayRef p_info, MCErrorRef& r_er
163236
r_error);
164237
}
165238

239+
MC_DLLEXPORT_DEF
240+
bool MCErrorCreateV(MCErrorRef& r_error,
241+
MCTypeInfoRef p_typeinfo,
242+
va_list p_args)
243+
{
244+
return MCErrorCreateWithMessageV(r_error,
245+
p_typeinfo,
246+
MCErrorTypeInfoGetMessage (p_typeinfo),
247+
p_args);
248+
}
249+
250+
MC_DLLEXPORT_DEF
251+
bool MCErrorCreateS(MCErrorRef& r_error,
252+
MCTypeInfoRef p_typeinfo,
253+
...)
254+
{
255+
va_list t_args;
256+
va_start(t_args, p_typeinfo);
257+
258+
bool t_result;
259+
t_result = MCErrorCreateV(r_error,
260+
p_typeinfo,
261+
t_args);
262+
263+
va_end(t_args);
264+
265+
return t_result;
266+
}
267+
166268
MC_DLLEXPORT_DEF
167269
bool MCErrorUnwind(MCErrorRef p_error, MCValueRef p_target, uindex_t p_row, uindex_t p_column)
168270
{
@@ -325,38 +427,16 @@ MCErrorCreateAndThrowWithMessageV (MCTypeInfoRef p_error_type,
325427
MCStringRef p_message,
326428
va_list p_args)
327429
{
328-
__MCAssertIsErrorTypeInfo(p_error_type);
329-
__MCAssertIsString(p_message);
330-
MCAutoArrayRef t_info;
331-
if (!MCArrayCreateMutable(&t_info))
332-
return false;
333-
334-
for(;;)
335-
{
336-
const char *t_key;
337-
t_key = va_arg(p_args, const char *);
338-
if (t_key == nil)
339-
break;
340-
341-
MCValueRef t_value;
342-
t_value = va_arg(p_args, MCValueRef);
343-
344-
// If a value is nil, then it means don't include it.
345-
if (t_value == nil)
346-
continue;
347-
348-
MCNewAutoNameRef t_name;
349-
if (!MCNameCreateWithNativeChars((const char_t *)t_key, strlen(t_key), &t_name))
350-
return false;
351-
352-
if (!MCArrayStoreValue(*t_info, true, *t_name, t_value))
353-
return false;
354-
}
355-
356-
MCAutoErrorRef t_error;
357-
if (!MCErrorCreateWithMessage(p_error_type, p_message, *t_info, &t_error))
358-
return false;
359-
430+
431+
MCAutoErrorRef t_error;
432+
if (!MCErrorCreateWithMessageV(&t_error,
433+
p_error_type,
434+
p_message,
435+
p_args))
436+
{
437+
return false;
438+
}
439+
360440
return MCErrorThrow(*t_error);
361441
}
362442

0 commit comments

Comments
 (0)