国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

More Effective C++之引用計數

2019-11-17 05:28:06
字體:
來源:轉載
供稿:網友

  Reference counting讓我想起了java,當假如想用C++來實現Java的能力的話,那Reference counting必不可少。Reference counting可以節省程序的運行成本,大量的構造、析構、分配、釋放和拷貝的代價被省略。

  實現

classRCObject
{
 public:
  RCObject():refCount(0),shareable(true){}
  RCObject(constRCObject&):refCount(0),shareable(true){}
  RCObject& Operator=(constRCObject& rhs){return *this;}
  virtual ~RCObject()=0;
  void AddReference(){++refCount;}
  void RemoveReference(){if (--refCount == 0) deletethis;}

  void markUnshareable(){shareable = false;}
  bool isShareable() const{returnshareable;}
  bool isShared() const {returnrefCount > 1;}
 PRivate:
  int refCount;
  bool shareable;
};
RCObject::~RCObject(){}

template <classT>
class RCPtr
{
 public:
  RCPtr(T* realPtr = 0):pointee(realPtr){init();}
  RCPtr(constRCPtr& rhs):pointee(rhs.pointee){init();}
  ~RCPtr(){if (pointee) pointee->RemoveReference();}
  RCPtr& operator = (constRCPtr& rhs)
  {
   if (pointee!=rhs.pointee)
   {
    if (pointee)
     pointee->RemoveReference();
     pointee = rhs.pointee;
     init();
   }
   return *this;
  }
  T* operator->() const { returnpointee;}
  T& operator*() const{return *pointee;}
 private:
  T* pointee;
  void init()
  {
   if (pointee == 0)
    return;
   if (pointee->isShareable() == false)
    pointee = newT(*pointee);
   pointee->AddReference();
  }
};

class String
{
 public:
  String(const char* value = ""):value(newStringValue(value)){}
  const char& operator[](intnIndex) const
  {
   return value->data[nIndex];
  }
  char& operator[](intnIndex)
  {
   if (value->isShared())
    value = newStringValue(value->data);
    value->markUnshareable();
    returnvalue->data[nIndex];
  }
 protected:
 private:
  strUCt StringValue:publicRCObject
  {
   char* data;
   String Value(constchar* initValue)
   {
    init(initValue);
   }
   String Value(constStringValue& rhs)
   {
    init(rhs.data);
   }
   void init(constchar * initValue)
   {
    data = newchar[strlen(initValue) + 1];
    strcpy(data,initValue);
   }
   ~String Value()
   {
    delete [] data;
   }
  };
  RCPtr<StringValue> value;
};
  這是Meyers給出的String的實現,然而我的觀點是假如沒有非凡的必要的話,對stirng最好不要使用引用計數,因為在多線程程序中同步的代價要大于引用計數本身的好處,得不償失。

  假如StringValue是一個現成的類,無法修改它的實現,那怎么辦?沒關系可以用委托,下面是一個典型的實現:


classRCObject
{
 public:
  RCObject():refCount(0),shareable(true){}
  RCObject(constRCObject&):refCount(0),shareable(true){}
  RCObject& operator=(constRCObject& rhs){return *this;}
  virtual ~RCObject()=0;
  void AddReference(){++refCount;}
  void RemoveReference(){if (--refCount == 0) deletethis;}

  void markUnshareable(){shareable = false;}
  bool isShareable() const{returnshareable;}
  bool isShared() const {returnrefCount > 1;}
 private:
  int refCount;
  bool shareable;
};
RCObject::~RCObject(){}

template<classT>
class RCiptr
{
 public:
  RCIPtr(T* realPtr = 0):counter(new CountHolder)
  {
   counter->pointee = realPtr;
   init();
  }
  RCIPtr(constRCIPtr& rhs):counter(rhs.counter)
  {
   init();
  }
  ~RCIPtr()
  {
   counter->RemoveReference();
  }
  RCIPtr& operator = (constRCIPtr& rhs)
  {
   if (counter != rhs.counter)
   {
    counter->RemoveReference();
    counter = rhs.counter;
    init();
   }
   return *this;
  }
  constT* operator->()const
  {
   returncounter->pointee;
  }
  T* operator->()
  {
   makeCopy();
   returncounter->pointee;
  }
  constT& operator*() const
  {
   return *(counter->pointee);
  }
  T& operator*()
  {
   makeCopy();
   return *(counter->pointee);
  }
 private:
  struct CountHolder:publicRCObject
  {
   ~Count Holder(){deletepointee;}
   T* pointee;
  };
  Count Holder* counter;
  void init()
  { 
   if (counter->isShareable() == false)
   {
    T* oldValue = counter->pointee;
    counter = newCountHolder;
    counter->pointee = newT(*oldValue);
   }
   counter->AddReference();
  }
  void makeCopy()
  {
   if (counter->isShared())
   {
    T* oldValue = counter->pointee;
    counter->RemoveReference();
    counter = newCountHolder;
    counter->pointee = newT(*oldValue);
    counter->AddReference();
   }
  }
 };
 class Widget
 {
  public:
   Widget(intSize){}
   Widget(constWidget& rhs){}
   ~Widget(){}
   Widget operator=(const Widget& rhs){}
   void doThis(){printf("doThis()/n");return;}
   int showThat() const{printf("showThat()/n"); return 0;}
  protected:
  private:
   inti;
 };

 class RCWidget
 {
  public:
   RCWidget(intsize):value(newWidget(size)){}
   void doThis(){value->doThis();}
   int showThat()const {returnvalue->showThat();}
  protected:
  private:
   RCIPtr<Widget> value;
};
  評估

  實現引用計數是需要有前提的,不是所有的情況下,使用引用計數都是合適的。適合情況如下:

  相對多的對象共享相對少量的實值。

  對象的實值產生或者銷毀的成本很高,或者占用很多內存。

  但是要記住,即使是Java也會有內存泄漏,不要指望小小的引用計數(上面簡單的實現)不會產生同樣的問題。

  引用計數是一項很深奧的技術,想想Java,所以需要很謹慎的對待,但愿它能帶來程序設計上的優化。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿巴嘎旗| 岐山县| 安多县| 深泽县| 双峰县| 邹城市| 铜山县| 米脂县| 徐水县| 新沂市| 三都| 潞城市| 伊金霍洛旗| 合水县| 扬州市| 德阳市| 永泰县| 文安县| 中阳县| 鄂温| 米泉市| 江陵县| 高雄市| 苏尼特左旗| 佛山市| 二手房| 清水河县| 剑河县| 改则县| 中西区| 叶城县| 钟祥市| 白山市| 台中县| 赣榆县| 贞丰县| 都兰县| 兰坪| 乌兰察布市| 简阳市| 云龙县|