fdset.h

来自「socket的事件分发模型」· C头文件 代码 · 共 66 行

H
66
字号
#ifndef FDSET_H_
#define FDSET_H_

#include "fdbase.h"
/**
 *	@brief	fdbase的集合
 *	@remark	
 *		create/expand/add/remove/clear/set等方法都不会改变集合中元素的相对位置
 */
class ZBCDLL fdset
{
public:
	explicit fdset(size_t bufSize = 0);
	~fdset();

	HRET create(size_t bufSize);		//	创建一个容量为bufSize的集合
	HRET expand(size_t bufSize);		//	调整集合的容量
	HRET add(fdbase& fb);				//	将一个fdbase加入集合
	HRET remove(int idx);				//	从集合中取出索引位idx的fdbase
	void clear();						//	清除所有fdbase,并释放所有资源
	void reset();						//	清除所有fdbase,但不释放缓冲区
	void destroy();						//	删除fdbase内的所有对象
	size_t count() const;				//	集合中fdbase的个数
	size_t capacity() const;			//	集合的容量,总共可以容纳fdbase的个数

	fdbase* get(int idx) const;			//	
	void set(int idx, fdbase* p);		//	
protected:
	size_t m_cnt;						//	fdbase数目
	size_t m_bufSize;					//	fdbase缓冲大小
	fdbase** m_pfds;					//	fdbase指针数组
	int* m_pUnused;						//	已经回收的索引
};

inline fdset::fdset(size_t bufSize) : m_cnt(0), m_bufSize(0), m_pfds(0), m_pUnused(0)
{
	create(bufSize);
}

inline fdset::~fdset()
{
	clear();
}

inline fdbase* fdset::get(int idx) const
{
	return m_pfds[idx];
}

inline void fdset::set(int idx, fdbase* p)
{
	m_pfds[idx] = p;
}

inline size_t fdset::count() const
{
	return m_cnt;
}
	
inline size_t fdset::capacity() const
{
	return m_bufSize;
}

#endif //FDSET_H_

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?