Hi there,
I haven't kept up with the latest C best practices. In adapting
(wrapping, mostly) a C++ API which throws on error, which of these might
the better alternative, or are there other options I haven't considered?
/* Assume: */
typedef struct { ... } Error;
struct Foo;
/* Parametric (stateless) version: */
Foo* CreateFoo (int ctorparam, Error *const error);
bool PrintFoo (const Foo *const foo, Error *const error);
bool ReleaseFoo(const Foo *const foo, Error *const error);
/* Static (stateful) version: */
Foo* CreateFoo (int ctorparam);
bool PrintFoo (const Foo *const foo);
bool ReleaseFoo(const Foo *const foo);
Error* LastError ();
In both versions, the functions return NULL or false on error. In
addition, the parametric version sets its second parameter appropriately
in case of error. The static version instead sets a global, much like
errno, which can be retrieved using LastError.
Of course, this global is not truly so; it uses thread-local storage to
achieve lock-free thread-safety. The first one is 'cleaner' in one
sense, but also more verbose. The second is simpler, but has the problem
that TLS, though fairly portable, is not part of the language.
A related question is whether there's a consensus on returning true or
false (or non-zero : zero) on error. I've seen different libraries use
both, which is unfortunate.
Any guidance appreciated!
Thanks,
-Al-
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|