第十六章‘把工作都交给了PStash

清除工作: //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;
} ///:~


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫云的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值