栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

对象池(C++)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对象池(C++)

版本一
#include
#include
#include
#include//双端队列
using namespace std;
class Point {
private:
	int x, y;
public:
	Point():x(0),y(0) {
		cout << "create" << endl;
	}
	~Point() {
		cout << "delete" << endl;
	}
};
#define LOG(info) 
cout<<__FILE__<<":"<<__LINE__<<""
<<__TIMESTAMP__<<":"<
class ObjectPool {
private:
	bool needClose;//false
	std::dequem_object_queue;
public:
	ObjectPool():needClose(false) {}
	ObjectPool(const ObjectPool&) = delete;//删除方法
	ObjectPool& operator=(const ObjectPool&) = delete;
	~ObjectPool(){
		Close();
	}
	void Close() {
		needClose = true;
		Clear();
	}
	void Clear() {
		for (auto x : m_object_queue) {
			delete x;
		}
	}
	size_t GetNum()const {
		return m_object_queue.size();
	}
	void Init(size_t num) {
		if (needClose) {
			LOG("对象池已经关闭");
			exit(1);
		}
		if (num <= 0 || num > MaxObject) {
			LOG("创建对象数据个数异常");
			return;
		}
		for (size_t i = 0; i < num; ++i) {
			m_object_queue.push_back(new T());//缺省构造函数
		}
	}
public:
	T* BorrowObject() {
		if (needClose) {
			LOG("对象池已经关闭");
			exit(1);//return nullptr
		}
		if (m_object_queue.empty()) {
			return nullptr;
		}
		T* tmp = m_object_queue.front();
		m_object_queue.pop_front();
		return tmp;
	}
	void Return(T* p) {
		if (nullptr == p) {
			LOG("池子里的对象丢失或空指针(nullptr)");
		}
		m_object_queue.push_back(p);
		return;
	}
};
int main() {
	ObjectPool obj;
	obj.Init(5);
	Point* p1 = obj.BorrowObject();
	Point* p2 = obj.BorrowObject();
	obj.Return(p1);
	obj.Return(p2);
	p1 = new Point();
	obj.Return(p1);
	return 0;
}

 缺点:1、对象池中的对象没有达到上限时,无法增加对象

2、丢失的对象会造成内存泄露。借出去的对象没有被析构

3、不知道借出对象的个数和空闲对象的个数。

版本二
#include//(2)
#include
#include
#include//双端队列
using namespace std;
class Point {
private:
	int x, y;
public:
	Point():x(0),y(0) {
		cout << "create" << endl;
	}
	~Point() {
		cout << "delete" << endl;
	}
};
#define LOG(info) 
cout<<__FILE__<<":"<<__LINE__<<""
<<__TIMESTAMP__<<":"<
class ObjectPool {
private:
	bool needClose;//false
	size_t create;//创建对象的个数
	std::dequem_idle_object_queue;//空闲队列
	std::dequem_act_object_queue;//活动队列
public:
	ObjectPool():needClose(false),create(0) {}
	ObjectPool(const ObjectPool&) = delete;
	ObjectPool& operator=(const ObjectPool&) = delete;
	~ObjectPool(){
		Close();
	}
	void Close() {
		needClose = true;
		Clear();
	}
	void Clear() {
		for (auto x : m_idle_object_queue) {
			delete x;
		}
		m_idle_object_queue.clear();
		for (auto x : m_act_object_queue) {
			delete x;
		}
		m_act_object_queue.clear();
	}
	size_t GetNumAct()const {
		return m_act_object_queue.size();
	}
	size_t GetNumIdle()const {
		return m_idle_object_queue.size();
	}
	void Init(size_t num) {
		if (needClose) {
			LOG("对象池已经关闭");
			exit(1);
		}
		if (num <= 0 || num > MaxObject) {
			LOG("创建对象数据个数异常");
			return;
		}
		if (create + num > MaxObject) {
			LOG("已达到上限");
			return;
		}
        create += num;
		for (size_t i = 0; i < num; ++i) {
			m_idle_object_queue.push_back(new T());//缺省构造函数
		}
	}
public:
	void AddObject(size_t num) {
		Init(num);
	}
	T* BorrowObject() {
		if (needClose) {
			LOG("对象池已经关闭");
			exit(1);//return nullptr
		}
		if (m_idle_object_queue.empty()) {
			LOG("没有对象可借");
			if (create >= MaxObject) {
				return nullptr;
			}
			AddObject(1);
		}
		T* tmp = m_idle_object_queue.front();
		m_idle_object_queue.pop_front();
		m_act_object_queue.push_back(tmp);
		return tmp;
	}
	void Return(T* p) {
		if (nullptr == p) {
			LOG("池子里的对象丢失或空指针(nullptr)");
		}
		if(std::_Erase_nodes_if(m_act_object_queue, p)){
            m_idle_object_queue.push_back(p);
            LOG("对象已归还到池子");
        }//注意,这里是C20版本
        else{
            LOG("err");
        }
		return;
	}
};
int main() {
	ObjectPool obj;
	obj.Init(5);
	Point* p1 = obj.BorrowObject();
	Point* p2 = obj.BorrowObject();
	obj.Return(p1);
	obj.Return(p2);
	p1 = new Point();
	obj.Return(p1);
	return 0;
}

 解决了版本一的第二个缺点;

缺点:借出去的对象依旧会被管理(杀死对象与没有归还对象之间的矛盾)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979858.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号