使用 C 模仿 C++ 模板的拙劣方法

如下所示,准备两个宏,一个定义类型,一个定义容器大小。

使用时只要先定义这两个宏,然后再包含容器头文件就能生成不同类型和大小的容器了。但是这种方法只允许在源文件中使用,如果在头文件中使用,定义不同类型和大小的容器时会引起宏定义冲突。

cpp 复制代码
#pragma once
#include "cb/cb_define.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#ifdef __cplusplus
extern "C"
{
#endif

	///
	/// @brief 模板参数。队列元素类型。
	///
#ifndef __template_cb_circle_deque_element_type
	#define __template_cb_circle_deque_element_type int
#endif

	///
	/// @brief 模板参数。队列大小。
	///
#ifndef __template_cb_circle_deque_size
	#define __template_cb_circle_deque_size 100
#endif

	///
	/// @brief 循环队列。
	///
	typedef struct cb_circle_deque
	{
		__template_cb_circle_deque_element_type _buffer[__template_cb_circle_deque_size];
		uint32_t _begin;
		uint32_t _end;
		bool _is_full;

	} cb_circle_deque;

	///
	/// @brief 初始化。
	///
	/// @param self
	/// @return
	///
	__cb_force_inline void cb_circle_deque_initialize(cb_circle_deque *self)
	{
		self->_begin = 0;
		self->_end = 0;
		self->_is_full = false;
	}

	///
	/// @brief 队列中的元素个数。
	///
	/// @param self
	/// @return
	///
	__cb_force_inline int cb_circle_deque_count(cb_circle_deque *self)
	{
		if (self->_is_full)
		{
			return __template_cb_circle_deque_size;
		}

		if (self->_end >= self->_begin)
		{
			return self->_end - self->_begin;
		}

		return __template_cb_circle_deque_size - (self->_begin - self->_end);
	}

	///
	/// @brief 队列为空。
	///
	/// @param self
	/// @return
	///
	__cb_force_inline bool cb_circle_deque_is_empty(cb_circle_deque *self)
	{
		return self->_begin == self->_end && !self->_is_full;
	}

	///
	/// @brief 向队列末尾添加元素。
	///
	/// @param self
	/// @param value
	/// @return 添加成功返回 true, 添加失败返回 false.
	///
	__cb_force_inline bool cb_circle_deque_push_back(cb_circle_deque *self,
													 __template_cb_circle_deque_element_type *value)
	{
		if (value == NULL)
		{
			return false;
		}

		if (self->_is_full)
		{
			return false;
		}

		self->_buffer[self->_end] = *value;
		self->_end = (self->_end + 1) % __template_cb_circle_deque_size;

		if (self->_end == self->_begin)
		{
			self->_is_full = true;
		}

		return true;
	}

	///
	/// @brief 向队列头部添加元素。
	///
	/// @param self
	/// @param value
	/// @return 添加成功返回 true, 添加失败返回 false.
	///
	__cb_force_inline bool cb_circle_deque_push_front(cb_circle_deque *self,
													  __template_cb_circle_deque_element_type *value)
	{
		if (value == NULL)
		{
			return false;
		}

		if (self->_is_full)
		{
			return false;
		}

		self->_begin = (self->_begin + __template_cb_circle_deque_size - 1) % __template_cb_circle_deque_size;
		self->_buffer[self->_begin] = *value;

		if (self->_end == self->_begin)
		{
			self->_is_full = true;
		}

		return true;
	}

	///
	/// @brief 弹出队列末尾元素。
	///
	/// @param self
	/// @param out
	/// @return
	///
	__cb_force_inline bool cb_circle_deque_pop_back(cb_circle_deque *self,
													__template_cb_circle_deque_element_type *out)
	{
		if (cb_circle_deque_is_empty(self))
		{
			return false;
		}

		self->_end = (self->_end + __template_cb_circle_deque_size - 1) % __template_cb_circle_deque_size;

		if (out != NULL)
		{
			*out = self->_buffer[self->_end];
		}

		self->_is_full = false;
		return true;
	}

	///
	/// @brief 弹出队列头部元素。
	///
	/// @param self
	/// @param out
	/// @return
	///
	__cb_force_inline bool cb_circle_deque_pop_front(cb_circle_deque *self,
													 __template_cb_circle_deque_element_type *out)
	{
		if (cb_circle_deque_is_empty(self))
		{
			return false;
		}

		if (out != NULL)
		{
			*out = self->_buffer[self->_begin];
		}

		self->_begin = (self->_begin + 1) % __template_cb_circle_deque_size;
		self->_is_full = false;
		return true;
	}

	///
	/// @brief 获取队列中指定索引的元素。
	///
	/// @param self
	/// @param index
	///
	/// @return 如果指定索引处有元素,则返回该元素的指针,否则返回空指针。
	///
	__cb_force_inline __template_cb_circle_deque_element_type *cb_circle_deque_get(cb_circle_deque *self,
																				   int index)
	{
		if (index < 0 || index >= cb_circle_deque_count(self))
		{
			return NULL;
		}

		uint32_t pos = (self->_begin + index) % __template_cb_circle_deque_size;
		return &self->_buffer[pos];
	}

	///
	/// @brief 清空队列。
	///
	/// @param self
	/// @return
	///
	__cb_force_inline void cb_circle_deque_clear(cb_circle_deque *self)
	{
		self->_begin = 0;
		self->_end = 0;
		self->_is_full = false;
	}

#ifdef __cplusplus
}
#endif
相关推荐
端平入洛2 天前
delete又未完全delete
c++
祈安_2 天前
C语言内存函数
c语言·后端
端平入洛2 天前
auto有时不auto
c++
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django