[programming art] design C language interface in handle form

Welcome to my official account, and get more of my notes.

  O_o   >_<   o_O   O_o   ~_~   o_O

  this paper introduces how to design C language interface in the form of handle.

  interface design is essential in the implementation of collaborative development projects. For C + +, there are many interfaces using class member functions. For C, I think it is elegant to use handle to design interfaces, and there are not many relevant introduction resources on the network, so share it.

1. What is handle

  handle is also called handle, which originates from Handle-C. To understand handle, we must first understand the pointer. The pointer is like a person's ID card. We can find the corresponding real object through it. In C language, there is a saying that everything is an address. Of course, the pointer is also an address, which points to the head position of the corresponding object. The handle is a pointer to the pointer. All methods of the object can be operated through the handle. This may be more abstract. For example, the handle is like a handbag. When you pick up the handbag, the computer, mobile phone, pen, book... These structures in the bag are all picked up by you. You can access the photos in your mobile phone, access the vscode in your computer through the mobile phone in your handbag at any time, and even remotely access your computer through the sunflower of the mobile phone in your handbag, Yes, that's what handles do.


2. Designing interface with handle

  it's very elegant to design an interface with a handle. Here's how to design an interface with a handle.

/*====================== D E F I N E S ================*/ 
/* Channel Create Param */ 
typedef struct ImgChannelParam 
{ 
    int32_t width;          ///< original image width
    int32_t height;         ///< original image height
    int32_t reserved[32];   ///< reserved fields
}ImgChannelParam; 

/* Config Param */ 
typedef struct ConfigParam 
{ 
    int32_t minThresh;       ///< resu lt judgment low threshold
    int32_t maxThresh;       ///< resu lt judgment high threshold
}ConfigParam; 

/* Process Input Param */ 
typedef struct ProcessParam 
{ 
    cv::Mat img;            ///< original input image
    cv::Rect roi;           ///< roi 
    int32_t reserved[32]; 
}ProcessParam; 

/* Process Output Param */ 
typedef struct ProcessResult 
{ 
    uint8_t *threshImg;    ///< binary graph
    uint8_t isOk;          ///< qualified
    int32_t reserved[32]; 
}ProcessResult; 

/*************************************************************** 
* @brief Init Memory 
* 
* @param [in] handle 
* @param [in] param 
* @return API_EXPORT int32_t  
* @note Module memory allocation and initialization
****************************************************************/ 
API_EXPORT int32_t CALL_METHOD Create(void **handle, ChannelParam *param); 

/*************************************************************** 
* @brief Get Config Param 
* 
* @param [in] handle 
* @param [in] param 
* @return API_EXPORT int32_t 
* @note Module current configuration parameter acquisition
****************************************************************/ 
API_EXPORT int32_t CALL_METHOD GetConfig(void *handle, ConfigParam *param); 

/*************************************************************** 
* @brief Set Config Param 
* 
* @param [in] handle 
* @param [in] param  
* @return API_EXPORT int32_t 
* @note Module configuration parameter setting
****************************************************************/ 
API_EXPORT int32_t CALL_METHOD SetConfig(void *handle, ConfigParam *param); 

/*************************************************************** 
* @brief Process Algorithm 
*
* @param [in] handle 
* @param [in] param 
* @return API_EXPORT int32_t 
* @note Module main function
****************************************************************/ 
API_EXPORT int32_t CALL_METHOD Process(void *handle, ProcessParam *param, ProcessResult *result); 

/*************************************************************** 
* @brief Exit Algorithm 
* 
* @param [in] handle 
* @param [in] param 
* @return API_EXPORT int32_t 
* @note Module exit
****************************************************************/ 
API_EXPORT int32_t CALL_METHOD Release(void *handle); 

  to explain, Process is the main algorithm processing interface. Create is the memory development interface. Memory related development is only done in create. There is no memory development action in Process. Only the memory applied in create is operated, and the memory is released in Release. GetConfig and SetConfig are used to obtain and configure algorithm parameters. This code is very clear and versatile. I personally like it very much.


  that's all for sharing about handle. In fact, I didn't say much. I mainly focused on showing a set of handle interface design, hoping to help students in need.


[official account transmission]

[programming art] designing C language interface in handle form



Scan the bottom two dimensional code to focus on my WeChat official account, and get more AI experience sharing. Let's welcome AI with the ultimate + geek mentality.

Keywords: C Programming pointer api Code Style

Added by dror_israel on Fri, 15 Oct 2021 00:55:16 +0300