I am going to go against the grain and offer both error codes and exceptions, but only because you are creating a library. Since you say that you are making a library, I assume that the library will be available for code written by people you do not control. Thus, to make your code friendly for different compilers and, possibly, even languages, is good.
So, I would encode the C ++ exception library and provide header files describing your exception classes. I would also code the C interface, which handles exceptions for the user. Now the user can refer to some interface:
#ifdef __cplusplus__ void generate_report(const std::string& rep_number, ostream& output); extern "C" #endif int generate_report(const char* rep_number, const char* outputfilename, int* error_code, char* error_text, int max_error_text_len);
The C implementation calls the C ++ implementation:
extern "C" int generate_report(const char* rep_number, const char* outputfilename, int* error_code, char* error_text, int max_error_text_len) { ofstream os; try { os.open(outputfilename, IOS_WRITE); generate_report(rep_number, os); os.close(); return TRUE; } catch (base_exception& e) { os.close(); if (error_code) *error_code = e.error_code(); if (error_text) strncpy(error_text, e.str(), max_error_text_len); return FALSE; } }
jmucchiello Sep 07 '09 at 21:07 2009-09-07 21:07
source share