class ReadMostlyWeakPtr;
template <typename T, typename RefCount>
class ReadMostlySharedPtr;
+template <typename RefCount>
+class ReadMostlyMainPtrDeleter;
using DefaultRefCount = TLRefCount;
private:
friend class ReadMostlyMainPtr<T, RefCount>;
+ friend class ReadMostlyMainPtrDeleter<RefCount>;
explicit ReadMostlySharedPtrCore(std::shared_ptr<T> ptr) :
ptrRaw_(ptr.get()),
private:
friend class ReadMostlyWeakPtr<T, RefCount>;
friend class ReadMostlySharedPtr<T, RefCount>;
+ friend class ReadMostlyMainPtrDeleter<RefCount>;
detail::ReadMostlySharedPtrCore<T, RefCount>* impl_{nullptr};
};
detail::ReadMostlySharedPtrCore<T, RefCount>* impl_{nullptr};
};
+/**
+ * This can be used to destroy multiple ReadMostlyMainPtrs at once.
+ */
+template <typename RefCount = DefaultRefCount>
+class ReadMostlyMainPtrDeleter {
+ public:
+ ~ReadMostlyMainPtrDeleter() noexcept {
+ RefCount::useGlobal(refCounts_);
+ for (auto& decref : decrefs_) {
+ decref();
+ }
+ }
+
+ template <typename T>
+ void add(ReadMostlyMainPtr<T, RefCount> ptr) noexcept {
+ if (!ptr.impl_) {
+ return;
+ }
+
+ refCounts_.push_back(&ptr.impl_->count_);
+ refCounts_.push_back(&ptr.impl_->weakCount_);
+ decrefs_.push_back([impl = ptr.impl_] { impl->decref(); });
+ ptr.impl_ = nullptr;
+ }
+
+ private:
+ std::vector<RefCount*> refCounts_;
+ std::vector<folly::Function<void()>> decrefs_;
+};
}