How to convert exceptions to errors and error messages when wrapping C++ classes API for C consumption? -


the problem

say you've got c++ class.

say want export constructor(s) , methods c.

say followed advices given in solution "wrapping c++ class api c consumption" question.

now class constructor can throw 1 or more exceptions, want catch on c++ side before returning c world, whilst still giving c user chance information error happened.

first approach

i check myclass_new() returned null pointer , retrieve "last error message" occurred in library function, when constructor fails there's no object provide right context error retrieval, therefore global variable should used hold last error undermines reentrancy of library.

second approach

another possibility can think of, which solution came with, class trap exception inside constructor itself, convert error message , put object "error state" of sort.

after each

myclass myobj = myclass_new(); 

the following check performed

if (myclass_isinerrorstate(myobj)) 

and error message retrieved next statement

const char *error = myclass_getlasterror(myobj); 

third approach

another possibility can think of pass pointer buffer of given size each function can fail, filled error message, looks messy.

how it? there de facto standard way of dealing such issues?

replying own question.

i think go modified second approach.

the constructor return bool other functions can fail , accept myclass * out parameter set meaningful value in case of error, myclass_getlasterror(myobj) used.

this gets rid of myclass_isinerrorstate() function.

myclass on c side opaque pointer , on c++ side pointer structure holding pointer, possibly null, class' object , buffer of sorts contains error string.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -