Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > C API Design
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 1084 of 1099
Post > Topic >>

C API Design

by Al <two@[EMAIL PROTECTED] > Apr 21, 2008 at 01:19 PM

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.




 5 Posts in Topic:
C API Design
Al <two@[EMAIL PROTECT  2008-04-21 13:19:37 
Re: C API Design
Jack Klein <jackklein@  2008-05-05 16:01:11 
Re: C API Design
Markus Elfring <Markus  2008-05-05 16:01:38 
Re: C API Design
Brian Inglis <Brian.In  2008-05-05 16:01:41 
Re: C API Design
Markus Elfring <Markus  2008-05-05 16:01:43 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 17:11:54 CDT 2008.