zycore-c icon indicating copy to clipboard operation
zycore-c copied to clipboard

Status code extension

Open mochaaP opened this issue 1 year ago • 5 comments

I propose two status code extensions:

/* ---------------------------------------------------------------------------------------------- */
/* Status codes (general purpose) - extensions                                                    */
/* ---------------------------------------------------------------------------------------------- */

/**
 * The operation failed with an unexpected error.
 */
#define ZYAN_STATUS_UNEXPECTED ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0xFFFFu)

/**
 * The operation was not implemented.
 */
#define ZYAN_STATUS_NOT_IMPLEMENTED ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x78u)

p.s. there was also a typo in the comments of Zycore/Status.h:

 /**
- * The operation failed with an generic error.
+ * The operation failed with a generic error.
  */
 #define ZYAN_STATUS_FAILED \
     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x01u)

mochaaP avatar Sep 18 '24 10:09 mochaaP

Where in Zycore would you like to use this?

athre0z avatar Sep 18 '24 15:09 athre0z

UNEXPECTED seems to be the same as the existing FAILED status. Both are used to signal generic failures.

Contrary to exceptions, unexpected errors should not exist with status codes as per design every return value MUST be checked. Not doing that is a bug.

NOT_IMPLEMENTED would be ok to add for me, but is not as useful as a NotImplementedException IMO. It's probably better to call printf() and halt(), abort(), etc in the corpus of an unimplemented function. There is no meaningful runtime action to handle a NOT_IMPLEMENTED code. For the exception case, this is different as it at acts as a shortcut for 1) crashing the program and 2) signaling that the function never returns. In the end, using NOT_IMPLEMENTED or NotImplementedException is always a bug in the code.

flobernd avatar Sep 18 '24 21:09 flobernd

UNEXPECTED is more like a guard (e.g. __builtin_unreachable) but with a defined behavior instead. NOT_IMPLEMENTED is for platform-dependent support. I hope this clears the confusion.

mochaaP avatar Sep 19 '24 05:09 mochaaP

Mhh, what would be the benefit of using UNEXPECTED stats code vs the ZYAN_UNREACHABLE macro? IMO unreachable code should really be unreachable. If it is executed, this is clearly a bug and I don't see a valid runtime strategy that a caller should use to handle this status code (besides terminating the application; but then it can as will be done at the root).

Regarding NOT_IMPLEMENTED: I think we have NOT_SUPPORTED for the mentioned use-case.

flobernd avatar Sep 19 '24 05:09 flobernd

Regarding NOT_IMPLEMENTED: I think we have NOT_SUPPORTED for the mentioned use-case.

Not in Zycore:

image

mochaaP avatar Sep 19 '24 05:09 mochaaP