智能指针

来源:互联网 发布:asp手机考试系统源码 编辑:程序博客网 时间:2024/05/16 16:20

resmgr.h文件


#pragma once



#include <iostream>
#include <stdlib.h>


class Resmanager
{


private:
/*
类共享成员,管理所有资源
使用记录可使用空间的动态数据对资源进行记录,空间不够的时候进行扩展
*/
struct Count
{
int data;
int next;
};
static Count *res_count;//为当前管理的资源分配的计数器数组
static int res_count_used;//计数器数组的当前已使用空间
static int res_count_max;//计数器数组的最大可使用空间
/***********************************************************/


/*对象自身成员,管理自身对象*/
int *res;//资源的地址
int cur_res_index;//资源对应的计数器数组的下标


public:
Resmanager();
Resmanager(int *res);
Resmanager(const Resmanager &tmp);
~Resmanager();


Resmanager &operator=(const Resmanager &tmp);


static void ShowCount();

};


************************************************************************************************************************************************************

resmgr.cpp文件


#include "resmgr.h"

Resmanager::Count *Resmanager::res_count = nullptr;
int Resmanager::res_count_max;
int Resmanager::res_count_used;


Resmanager::Resmanager():res(nullptr)
{

}


//将新资源添加进类资源管理器
Resmanager::Resmanager(int *res)
{
//计数器数组的空间首次分配
if (0 == res_count_max || nullptr == res_count)
{
res_count = (Count*)malloc(1024 * sizeof(Count));


if (!res_count)
return;


memset(res_count, 0, 1024 * sizeof(Count));


//方便扩展所以那最后一个可用空间置为扩展后的第一个可用空间的下标
for (int i = 0; i < 1024; ++i)
res_count[i].next = i + 1;


res_count_max = 1024;
res_count_used = 0;
}
//计数器数组不够用的时候扩展空间
if (res_count_used == res_count_max)
{
Count *p_tmp = (Count*)realloc(res_count, 2 * res_count_max);


if (!p_tmp)
return;


for (int i = 0; i < res_count_max; ++i)
res_count[res_count_max + i].next = res_count_max + 1;


res_count_max *= 2;


}




this->res = res;
//摘除数组的一个可用空间,并且把下一个可用空间推给空闲列表
cur_res_index = res_count[0].next;
res_count[0].next = res_count[cur_res_index].next;
++res_count_used;


//初始化新资源的个数
res_count[cur_res_index].data = 1;


}


Resmanager::Resmanager(const Resmanager &tmp):res(tmp.res), cur_res_index(tmp.cur_res_index)
{
//指向该资源的对象增加了一个,该资源计数器数组对应的位置就加1
++res_count[cur_res_index].data;


std::cout << "Resmanager::Resmanager====>>>" << res_count[cur_res_index].data << std::endl;



}




Resmanager::~Resmanager()
{
--res_count[cur_res_index].data;
//当指向资源的对象不存在的时候,归还这个资源并且把计数器归还给空闲队列
if (0 >= res_count[cur_res_index].data)
{
delete res;
//把已经不使用了的计数器归还回去
res_count[cur_res_index].next = res_count[0].next;
res_count[0].next = cur_res_index;

}
}


Resmanager &Resmanager::operator=(const Resmanager &tmp)
{
if (res == tmp.res)
return *this;


res = tmp.res;
--res_count[cur_res_index].data;


std::cout << "res_count[cur_res_index].data=========>>>>" << res_count[cur_res_index].data << std::endl;
cur_res_index = tmp.cur_res_index;
++res_count[tmp.cur_res_index].data;



if (0 == res_count[cur_res_index].data)
{

//把已经不使用了的计数器归还回去
res_count[cur_res_index].next = res_count[0].next;
res_count[0].next = cur_res_index;
delete res;
}


return *this;
}


void Resmanager::ShowCount()
{
if (nullptr == res_count || 0 == res_count_max)
return;


std::cout << "**************************************************" << std::endl;
for (int i = 0; i < res_count_max; ++i)
std::cout << res_count[i].data << std::endl;
std::cout << "**************************************************" << std::endl;
}


*****************************************************************************************************************************************************


main.h文件


#pragma once


#include "resmgr.h"




*************************************************************************************************************************************************



测试用例man.cpp文件


#include "main.h"




void main(void)
{


Resmanager test(new int);
Resmanager test1(new int);
Resmanager test2(test);
Resmanager test3(test);
Resmanager test4(new int);



//test1 = test;


Resmanager::ShowCount();




system("pause");


}





原创粉丝点击