#ifndef RINGBUFF_HDR_H#define RINGBUFF_HDR_H #ifdef __cplusplus extern "C" { #endif #include <string.h> #include <stdint.h> /** * \defgroup RINGBUFF Ring buffer * \brief Generic ring buffer manager * \{ */ /* --- Buffer unique part starts --- */ /** * \brief Buffer function/typedef prefix string * * It is used to change function names in zero time to easily re-use same library between applications. * Use `#define BUF_PREF(x) my_prefix_ ## x` to change all function names to (for example) `my_prefix_buff_init` * * \note Modification of this macro must be done in header and source file aswell */ #define BUF_PREF(x) ring ## x /* --- Buffer unique part ends --- */ /** * \brief Buffer structure */ typedef struct { uint8_t* buff; /*!< Pointer to buffer data. Buffer is considered initialized when `buff != NULL` and `size` */ size_t size; /*!< Size of buffer data. Size of actual buffer is `1` byte less than value holds */ size_t r; /*!< Next read pointer. Buffer is considered empty when `r == w` and full when `w == r - 1` */ size_t w; /*!< Next write pointer. Buffer is considered empty when `r == w` and full when `w == r - 1` */ } BUF_PREF(buff_t); uint8_t BUF_PREF(buff_init)(BUF_PREF(buff_t)* buff, void* buffdata, size_t size); void BUF_PREF(buff_free)(BUF_PREF(buff_t)* buff); void BUF_PREF(buff_reset)(BUF_PREF(buff_t)* buff); /* Read/Write functions */ size_t BUF_PREF(buff_write)(BUF_PREF(buff_t)* buff, const void* data, size_t btw); size_t BUF_PREF(buff_read)(BUF_PREF(buff_t)* buff, void* data, size_t btr); size_t BUF_PREF(buff_peek)(BUF_PREF(buff_t)* buff, size_t skip_count, void* data, size_t btp); /* Buffer size information */ size_t BUF_PREF(buff_get_free)(BUF_PREF(buff_t)* buff); size_t BUF_PREF(buff_get_full)(BUF_PREF(buff_t)* buff); /* Read data block management */ void * BUF_PREF(buff_get_linear_block_read_address)(BUF_PREF(buff_t)* buff); size_t BUF_PREF(buff_get_linear_block_read_length)(BUF_PREF(buff_t)* buff); size_t BUF_PREF(buff_skip)(BUF_PREF(buff_t)* buff, size_t len); /* Write data block management */ void * BUF_PREF(buff_get_linear_block_write_address)(BUF_PREF(buff_t)* buff); size_t BUF_PREF(buff_get_linear_block_write_length)(BUF_PREF(buff_t)* buff); size_t BUF_PREF(buff_advance)(BUF_PREF(buff_t)* buff, size_t len); #undef BUF_PREF /* Prefix not needed anymore */ /** * \} */ #ifdef __cplusplus } #endif #endif /* RINGBUFF_HDR_H */
复制代码BUF_PREF(buffer_init)BUF_PREF(buff_free) BUF_PREF(buff_write) BUF_PREF(buff_read) 等等。。。
复制代码#define BUF_PREF(x) ring ## x
复制代码ring_buffer_initring_buffer_free ring_buffer_write ring_buffer_read 等等。。。
复制代码void led_device_open(void);void led_device_close(void); uint8_t led_device_read(void); uint8_t led_device_write(uint8_t status); 。。。
复制代码#define LED_CLASS(x) led_device_ ## xvoid LED_CLASS(open)(void); void LED_CLASS(close)(void); uint8_t LED_CLASS(read)(void); uint8_t LED_CLASS(write)(uint8_t status);
复制代码#define FLASH_CLASS(x) flash_device_ ## xvoid FLASH_CLASS(open)(void); void FLASH_CLASS(close)(void); uint8_t FLASH_CLASS(read)(void); uint8_t FLASH_CLASS(write)(uint8_t status);
复制代码##刚刚已经说了,是黏贴字符串
而#表示的是将参数转换为字符串
下面写一个跟#相关的例子:
#include <stdio.h>#define Print(x) printf("%s %d\n",#x,x); int main(void) { Print(100); return 0 ; }
复制代码来源:https://blog.csdn.net/morixinguan/article/details/100188149