c++ - `std::shared_ptr` 的智能指针模拟,带有用于将回调绑定(bind)到引用计数修改事件的 API,例如释放/保留……这是一回事吗?

标签 c++ pointers c++11 shared-ptr smart-pointers

我需要一个智能指针结构 - 类似于 std::shared_ptr - 它为我提供了某种带有公开 Hook 的 API,回调到引用计数修改事件(例如释放/保留,又名 refcout 增量/减量)可以绑定(bind)。

我要么想自己实现,要么使用现成的东西,如果它存在的话。

比如,我希望在定义这个假定的 shared_ptr-ish 智能指针(就像delete-expressions 和 deleter functor 分别在 shared_ptrunique_ptr 定义中使用。


编辑(来 self 下面的评论)——这就是我想要这个的原因:我目前有一个 Image 类模板,在它的核心,有一个 std::shared_ptr 持有一个(可能很大)连续的堆分配内存块。 Image 具有实现 hyperslab 迭代器(暴露例如颜色平面、CBIR 哈希数据等)的内部类,这些迭代器使用 std::weak_ptr 来引用所属 Image 实例的内存块。我想为特定的 refcounted 运行时扩展 Image——例如 Python c-api——并且绑定(bind)到现有的 std::shared_ptr refcounter 设备比保持两个不同的系统同步更可取。

最佳答案

shared_ptr 的默认实现有 shared_ptr,它们共享一个对象,所有对象都指向所谓的控制 block 。正是这个控制 block 保存了引用计数器,因此是可以更自然地对引用计数变化使用react的对象。

但是如果你真的想通过智能指针来完成它,下面的方法可能会起作用:

using std::shared_ptr;

template<class T> class sharedX_ptr;

// the type of function to call when a refcount change happens
template<class T>
void inc_callback(const sharedX_ptr<T>& p)
{
  std::cout << "Increasing refcount via " << &p
            << ", for object: " << p.get() << ", use_count: " << p.use_count()
            << std::endl;
}

template<class T>
void dec_callback(const sharedX_ptr<T>& p)
{
  std::cout << "About to decrease refcount via " << &p
            << ", for object: " << p.get() << ", use_count: " << p.use_count()
            << std::endl;
}

template<class T>
class sharedX_ptr : public shared_ptr<T>
{
  typedef void (*callback)(const sharedX_ptr<T>&);
  callback inc, dec;
public:
  typedef shared_ptr<T> base;

  sharedX_ptr(const sharedX_ptr& p) : base(p), inc(p.inc), dec(p.dec)
  { if (this->get()) inc(*this); }

  template<class U>
  sharedX_ptr(sharedX_ptr<U>&& p) : base(std::move(p)), inc(p.inc), dec(p.dec)
  { /*if (this->get()) inc(*this);*/ }

  template<class U>
  sharedX_ptr(shared_ptr<U> p, callback i = inc_callback<T>, callback d = dec_callback<T>)
    : shared_ptr<T>(std::move(p)), inc(i), dec(d)
    { if (this->get()) inc(*this); }

  sharedX_ptr& operator=(sharedX_ptr&& p) {
    if (this != &p) {
      if (this->get()) dec(*this);
      base::operator=(std::move(p)); inc = p.inc; dec = p.dec;
      /* if (this->get()) inc(*this); */
    }
    return *this;
  }

  template<class U>
  sharedX_ptr& operator=(const sharedX_ptr& p) {
    if (this != &p) {
      if (this->get()) dec(*this);
      base::operator=(p); inc = p.inc; dec = p.dec;
      if (this->get()) inc(*this);
    }
    return *this;
  }

  void reset() { if (this->get()) dec(*this); shared_ptr<T>::reset();}

  ~sharedX_ptr() { if (this->get()) dec(*this); }
};

关于c++ - `std::shared_ptr` 的智能指针模拟,带有用于将回调绑定(bind)到引用计数修改事件的 API,例如释放/保留……这是一回事吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283409/

相关文章:

c++ - shared_ptr 分配 : order of reference counting

c++ - 在 Sun Studio (NetBeans) 中组织大型 C++ 系统

c++ - 如何在 C++ Visual Studios 中将文本文件写入桌面?

c++ - 如何在 C++ 中打开文件(记事本除外)

C++ 类型转换

c++ - 递增指针打印垃圾?

ios - Objective-C 中的所有对象基本上都是指针吗?

c - printf ("%d","%u","%p",ptr,ptr,ptr) 和 printf ("%d %u %p",ptr,ptr,ptr) 有什么区别?

C++0x lambda 按值捕获总是 const?

c++ - 如何声明包含整数数组的对象