Talk About Network

Google


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 > Re: C API Desig...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 5 Topic 1084 of 1133
Post > Topic >>

Re: C API Design

by Brian Inglis <Brian.Inglis@[EMAIL PROTECTED] > May 5, 2008 at 04:01 PM

On Mon, 21 Apr 2008 13:19:37 -0500 (CDT) in comp.lang.c.moderated, Al
<two@[EMAIL PROTECTED]
> wrote:

>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 ();

What do formatted output functions return and what value indicates
failure? 
What does fclose() return and what value indicates failure? 
What error indicator is set by I/O function failures? 

>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 ****table, 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.

What do filesystem operation functions return and what value indicates
failure? 

>Any guidance appreciated!

Check out any decent C library reference section on stdio functions and
use that as your guide. 
Also note from Commandment # 6: "thy creativity is better used in
solving problems than in creating beautiful new impediments to
understanding." 

-- 
Thanks. Take care, Brian Inglis 	Calgary, Alberta, Canada

Brian.Inglis@[EMAIL PROTECTED]
 	(Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca)
    fake address		use address above to reply
-- 
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 Thu Jul 24 16:07:29 CDT 2008.