Skip to content

Latest commit

 

History

History
87 lines (65 loc) · 3.42 KB

README_EN.md

File metadata and controls

87 lines (65 loc) · 3.42 KB

ReturnCodesC

A Collection of Universal Function Return Codes

[stbanana/ReturnCodesC: A series of common C/C++ function return values.](https://github.com/stbanana/ReturnCodesC)

C/C++ Static Badge

简体中文 · English

✨Features

  1. 🔢Zero Overhead: Macro-defined value library with zero overhead
  2. 🐜Resource-Constrained Friendly: Common return codes are within 8 bits, facilitating functions with minimal resource overhead using uint8
  3. 📄Multi-Language Support: Use the _RERURNCC_LANGUAGE_ macro to define the language, making it easy to jump to your preferred language for semantic parsing
  4. 🙋‍♂️Wide Application: Covers most application scenarios, providing a relatively unified return value system for the entire project

Compatible with the return enumeration meanings of the ST HAL library.

🤔示例

/* First define your language version, or define it globally in the compiler/editor
 * 0: English
 * 1: Chinese
 * 2: Traditional Chinese
 */
#define _RERURNCC_LANGUAGE_ 1
#include <ReturnCodesC.h>

/* Simplest function example */
uint8_t IsValueEqual(uint32_t a, uint32_t b)
{
    if(a != b)
        return RETURN_ERROR;
    
    return RETURN_DEFAULT;
}

/* Structure or variable operation example, passing by pointer */
typedef struct
{                      
    uint8_t Buffer[20];
    uint8_t Head;
    uint8_t Tail;
} BufferDef;

uint32_t EnBuffer(BufferDef *pd, uint8_t data)
{
    /* Checkpoint-style checks */
    if(pd->Tail == ((pd->Head + 1) % 20)) 
        return RETURN_BUFFER_FULL;        // Buffer is full, return non-zero status
    // If any calling anomalies are found later, additional exception conditions can be added, for example
    /*
    if(pd == NULL)
        return RETURN_ERR_PARAM;
    */
    
    /* Actual functional logic */
    pd->Buffer[pd->Head] = data;
    pd->Head++;
    pd->Head %= 20;
    
    /* Normal return */
    return RETURN_DEFAULT;
}

👁️My Perspective

In C/C++ programs, functions are responsible for implementing functionality, and the implementation of functionality should have no side effects. A complete function should be able to handle all expected inputs and correspond to a unique output for each input.

Of course, achieving this completely in actual projects is quite challenging, so my view is that all functions must have a return value, and this return value should reflect the operational status of the function. This way, when calling the function, we can first determine that everything is fine with a completely normal return value and write the most basic project framework. Subsequently, adding exception generation and handling will be relatively easy.

In a project, many libraries may be integrated, each with their own API standard return values, which we cannot control or modify. However, in the business processes and middleware you write yourself, you can use this library to achieve a unified function status code system for the project.