清除工作: //for循环清除堆对象 (~PStash()调用析构~AutoCounter,接着析构调用verifier.remove(this);,接着remove调用erase。ok!!)
//: C16:TPStash.h
#ifndef TPSTASH_H
#define TPSTASH_H
template<class T, int incr = 10>
class PStash {
int quantity; // 当前最大容量
int next; // 当前量
T** storage;/指向指针的指针/数据存放点
void inflate(int increase = incr);//重新分配内存空间
public:
PStash() : quantity(0), next(0), storage(0) {}
~PStash();
int add(T* element);
T* operator[](int index) const; //
T* remove(int index);
int count() const { return next; }
};
template<class T, int incr>
int PStash<T, incr>::add(T* element) {
if(next >= quantity)
inflate(incr);
storage[next++] = element;
return(next - 1); /
}
template<class T, int incr>
PStash<T, incr>::~PStash() {//负责清除所指向的堆对象
for(int i = 0; i < next; i++) {
delete storage[i]; //for循环清除堆对象 (调用析构~AutoCounter,接着析构调用verifier.remove(this);,接着remove调用erase。ok!!)
storage[i] = 0; //
}
delete []storage;//最后把storage数组也清除掉
}
template<class T, int incr>
T* PStash<T, incr>::operator[](int index) const {
require(index >= 0,
"PStash::operator[] index negative");
if(index >= next)
return 0; //
require(storage[index] != 0,
"PStash::operator[] returned null pointer");
return storage[index];
}
template<class T, int incr>
T* PStash<T, incr>::remove(int index) {
T* v = operator[](index);
if(v != 0) storage[index] = 0;
return v;
}
template<class T, int incr>
void PStash<T, incr>::inflate(int increase) {//使用内存操作
const int psz = sizeof(T*);//获取单元void*的字节大小
T** st = new T*[quantity + increase];//分配的是存放指针的数组
memset(st, 0, (quantity + increase) * psz);//设置为0
memcpy(st, storage, quantity * psz);//内存复制,字节级别
quantity += increase;
delete []storage; // delete原始数组
storage = st; // 新指向
}
#endif // TPSTASH_H ///:~
/
//: C16:AutoCounter.h
#ifndef AUTOCOUNTER_H
#define AUTOCOUNTER_H
#include "../require.h"
#include <iostream>
#include <set> //
#include <string>
class AutoCounter {
static int count;
int id;
class CleanupCheck {///嵌套类
std::set<AutoCounter*> trace;//存放指向AutoCounter的指针
public:
void add(AutoCounter* ap) {
trace.insert(ap);
}
void remove(AutoCounter* ap) {
require(trace.erase(ap) == 1,erase函数删除AutoCounter堆对象
"Attempt to delete AutoCounter twice");
}
~CleanupCheck() {
std::cout << "~CleanupCheck()"<< std::endl;
require(trace.size() == 0,
"All AutoCounter objects not cleaned up");
}
};
static CleanupCheck verifier;//静态????
AutoCounter() : id(count++) {/private,不能被外部使用了
verifier.add(this); //果然是这句
std::cout << "created[" << id << "]"
<< std::endl;
}
AutoCounter(const AutoCounter&);private,不能被外部使用了
void operator=(const AutoCounter&);//private,不能被外部使用了
public:
static AutoCounter* create()
{ 只有这个static create才能创建对象,必须为static
return new AutoCounter();
}
~AutoCounter() {
std::cout << "destroying[" << id
<< "]" << std::endl;
verifier.remove(this);/调用了erase
}
///重载输出,指针和引用的
friend std::ostream& operator<<(std::ostream& os, const AutoCounter& ac){
return os << "AutoCounter " << ac.id;
}
friend std::ostream& operator<<(std::ostream& os, const AutoCounter* ac){
return os << "AutoCounter " << ac->id;
}
};
#endif // AUTOCOUNTER_H ///:~
/
#include "AutoCounter.h"
#include "TPStash.h"
#include <iostream>
#include <fstream>
using namespace std;
int main() {
PStash<AutoCounter> acStash;
for(int i = 0; i < 10; i++)
acStash.add(AutoCounter::create());
cout << "Remove two 但是没有delete:"<< endl;
cout << acStash.remove(5) << endl;
cout << acStash.remove(6) << endl;
cout << "清除工作交给了PStash的析构函数了"<< endl;
ifstream in("TPStashTest.cpp");
assure(in, "TPStashTest.cpp");
PStash<string> stringStash;
string line;
while(getline(in, line))
stringStash.add(new string(line));
for(int u = 0; stringStash[u]; u++)
cout << "stringStash[" << u << "] = "
<< *stringStash[u] << endl;
} ///:~