|
C/C++ Library, mos@ua 1.0
cpplib-mos
|
Handle Defensive Programming of C/C++ library functions module. More...
Files | |
| file | hdp.h |
| Handle Defensive Programming module. | |
Macros | |
| #define | MO(func_call) |
| #define | Ne(func_call) |
| #define | Nu(func_call) |
| #define | NZ(func_call) |
| #define | EF(func_call) |
| #define | MOP(func_call) |
| #define | errMO MO |
| #define | errNe Ne |
| #define | errNu Nu |
| #define | errNZ NZ |
| #define | errEF EF |
| #define | errMOP MOP |
Handle Defensive Programming of C/C++ library functions module.
This file handles defensive programming approach of native C/C++ libraries functions, removing the responsibility of handling errors from the function's clients. Error identification uses errno values, although not necessarily using errno global variable (like in NZ macro, used for pthread library functions).
Errors are handled by two possible error handling policies (through the definition of a macro – EXIT_POLICY/EXCEPTION_POLICY – as a compiler option):
EXIT_POLICY (default): describes the failed call in stderr (with the identification of the errno error, and the precise location the call), generates a segmentation fault (enabling a stack trace within a debugger like gdb), and exits program execution (applicable for C and C++);EXCEPTION_POLICY: throws a int exception with the (errno) status error returned by the original function (applicable only to C++).To change the default (EXIT_POLICY) policy just add -DEXCEPTION_POLICY in the compiler options.
Available macros (name directly related with error test):
MO/errMO [Minus One]: test error comparing to -1 (== -1) – error in errnoNe/errNe [Negative]: test error comparing to negative integer result (< 0) – error in errnoNu/errNu [Null]: test error comparing to NULL (== NULL) – error in errnoNZ/errNZ [Not Zero]: test error comparing to non-zero value (!= 0) – error in function resultEF/errEF [End of File]: test error comparing to EOF (== EOF) – error in errnoMOP/errMOP [Minus One Pointer]: test error comparing to (void*)-1 (== (void*)-1) – error in errnoExamples:
To discover the proper macro check for section RETURN VALUE in the function man page.
Consider the definition of the following bash function to make it easier to fetch this information.
Handle Defensive Programming of C/C++ library functions include file.
group of hdp
| #define MO | ( | func_call | ) |
Macro Minus One: applicable to functions returning -1 for error indication (cause stored in errno).
| [in] | func_call | [int] function call (or expression with the result of a library function call) |
Same as errMO.
| #define Ne | ( | func_call | ) |
Macro Negative: applicable to functions returning a negative integer value for error indication (cause stored in errno).
| [in] | func_call | [int] function call (or expression with the result of a library function call) |
Same as errNe.
| #define Nu | ( | func_call | ) |
Macro Null: applicable to functions returning NULL for error indication (cause stored in errno).
| [in] | func_call | [void*] function call (or expression with the result of a library function call) |
Same as errNu.
| #define NZ | ( | func_call | ) |
Macro Not Zero: applicable to functions returning a value different to 0 for error indication (error in function result using errno code values).
| [in] | func_call | [int] function call (or expression with the result of a library function call) |
Same as errNZ.
| #define EF | ( | func_call | ) |
Macro End of File: applicable to functions returning EOF for error indication (cause stored in errno).
| [in] | func_call | [int] function call (or expression with the result of a library function call) |
Same as errEF.
| #define MOP | ( | func_call | ) |
Macro Minus One Pointer: applicable to functions returning (void*)-1 for error indication (cause stored in errno).
| [in] | func_call | [void*] function call (or expression with the result of a library function call) |
Same as errMOP.