C++ ObjectPool_VS2010_非线程安全及线程安全版本

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <memory>
#include <iostream>
#include <stack>
#include <queue>
using namespace std;

#pragma once
#include <memory>
#include <iostream>
#include <stack>
using namespace std;

template <class T> class PoolItemProxy;
template <class T> class PoolItem ;
template <class T> class ObjectPool;
template <class T> class ObjectPoolTS ;
//typedef PoolItem<T> PoolItem<T>;
template <class T> class PoolItemProxy
{
private:
	ObjectPool<T>* PoolPtr;
	T* ItemPtr;

	PoolItemProxy(ObjectPool<T>* poolPtr, T* itemPtr)
	{
		this->PoolPtr = poolPtr;
		this->ItemPtr = itemPtr;
		cout << "object from pool" << endl;

	}

public:
	//PoolItemProxy(PoolItemProxy<T>& item)
	//{
	//	this->PoolPtr = item.m_pFromPool;
	//	this->ItemPtr = item.release();
	//}

	PoolItemProxy(PoolItemProxy<T>& item)
	{
		this->PoolPtr = item.PoolPtr;
		this->ItemPtr = item.ItemPtr;
	}

	virtual ~PoolItemProxy()
	{
		if(PoolPtr && ItemPtr)
		{
			PoolPtr->Release(*this);
			ItemPtr = NULL;
			PoolPtr = NULL;
			cout << "release to pool!" << endl;
		}
	}

	T* get()
	{
		return ItemPtr;
	}



	T* operator->()
	{
		return ItemPtr;
	}


	friend class PoolItemProxy<T>;
	friend class PoolItem<T>;
	friend class ObjectPool<T>;
};


template <class T> class ObjectPool
{

private: 
	int m_nCountLower;
	std::stack<T*> m_pool;
public:
	ObjectPool(int  nCountLower)
	{
		m_nCountLower  = nCountLower;
		for(int i = 0; i < nCountLower; ++i)
		{
			m_pool.push(new T());
		}
	}

	virtual ~ObjectPool()
	{
		while(!m_pool.empty())
		{
			delete m_pool.top();
			m_pool.pop();
		}
	}


	virtual PoolItem<T> Get()
	{
		if(m_pool.size() > 0)
		{
			PoolItem<T> item(new PoolItemProxy<T>((ObjectPool<T>*)this, m_pool.top()));
			m_pool.pop();
			return item;
		}
		else
		{
			PoolItem<T> item(new PoolItemProxy<T>((ObjectPool<T>*)this, new T()));
			return item;
		}
	}

	virtual void Release(PoolItemProxy<T>& item)
	{
		if(m_pool.size() >= m_nCountLower)
		{
			//直接Delete掉
			delete item.ItemPtr;
		}
		else
		{
			m_pool.push(item.ItemPtr);
			
		}
	}




};

template <class T> class PoolItem : public std::auto_ptr<PoolItemProxy<T>>
{
public:
	PoolItem(PoolItemProxy<T>* pitem)
		:auto_ptr<PoolItemProxy<T>>(pitem)
	{

	}

	T* operator->()
	{
		return (auto_ptr<PoolItemProxy<T>>::operator ->())->ItemPtr;
	}

	T* get()
	{
		return (auto_ptr<PoolItemProxy<T>>::operator ->())->ItemPtr;
	}

};




template <class T> class  ObjectPoolTS : public ObjectPool<T>
{
private:
	CCriticalSection m_lock;
public:
	ObjectPoolTS(int nCountLower)
		:ObjectPool<T>(nCountLower),m_lock()
	{

	}

	virtual ~ObjectPoolTS()
	{

	}


	virtual PoolItem<T> Get()
	{

		m_lock.Lock();
		PoolItem<T>& ret = ObjectPool<T>::Get();
		m_lock.Unlock();
		return ret;

	}

	virtual void Release(PoolItemProxy<T>& item)
	{
		m_lock.Lock();
		ObjectPool<T>::Release(item);
		m_lock.Unlock();
	}
};

class Data
{
	static int i;
public:
	Data()
	{
		m_i = ++i;
		cout << "data created" << m_i << endl;
	}

	virtual ~Data()
	{
		cout << "date deleted"  << m_i << endl;
	}

	int m_i;
};

int Data::i = 0;

void TestArgs1(PoolItem<Data>& item)
{
	cout << item->m_i << endl;

}
void TestArgs2(PoolItem<Data> item)
{
	cout << item->m_i << endl;

}
void TestArgs3(Data* item)
{
	cout << item->m_i << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	

	char c;
	{
		ObjectPoolTS<Data> pool(5);
		{
			PoolItem<Data> item = pool.Get();
			PoolItem<Data> item1 = pool.Get();
			PoolItem<Data> item2 = pool.Get();
			PoolItem<Data> item3 = pool.Get();
			PoolItem<Data> item4 = pool.Get();
			PoolItem<Data> item5 = pool.Get();
			cin.get(c);


			PoolItem<Data> item6(item);
			

		}

		{
			PoolItem<Data> item = pool.Get();
			PoolItem<Data> item1 = pool.Get();
			PoolItem<Data> item2 = pool.Get();
			cin.get(c);

		}

		{
			PoolItem<Data> item = pool.Get();
			TestArgs1(item);

			TestArgs3(item.get());  //OK 

			TestArgs2(item);

			__assume(item.get() == NULL);
		}

		{
			//OK
			std::vector<PoolItem<Data>> arrNode;
			arrNode.push_back(pool.Get());
			arrNode.push_back(pool.Get());
			cin.get(c);

		}

		cin.get(c);
	}
	

	cin.get(c);
}

//程序输出

data created1
data created2
data created3
data created4
data created5
object from pool
object from pool
object from pool
object from pool
object from pool
data created6
object from pool

release to pool!
release to pool!
release to pool!
release to pool!
release to pool!
date deleted4
release to pool!
object from pool
object from pool
object from pool

release to pool!
release to pool!
release to pool!
object from pool
3
3
3
release to pool!
object from pool
object from pool

release to pool!
release to pool!


date deleted2
date deleted3
date deleted1
date deleted6
date deleted5

posted @ 2011-05-28 15:45  阿牛  阅读(667)  评论(0编辑  收藏  举报