#ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_REF_H_
#define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_REF_H_
#include "base/memory/weak_ptr.h"
namespace content {
template <typename TargetType>
class CacheStorageRef {
public:
CacheStorageRef() = default;
explicit CacheStorageRef(base::WeakPtr<TargetType> target)
: target_(std::move(target)) {
target_->AddHandleRef();
}
CacheStorageRef(CacheStorageRef&& rhs) noexcept
: target_(std::move(rhs.target_)) {}
CacheStorageRef& operator=(CacheStorageRef&& rhs) {
if (target_)
target_->DropHandleRef();
target_ = std::move(rhs.target_);
return *this;
}
CacheStorageRef(const CacheStorageRef&) = delete;
CacheStorageRef& operator=(const CacheStorageRef&) = delete;
~CacheStorageRef() {
if (target_)
target_->DropHandleRef();
}
TargetType* value() const { return target_.get(); }
CacheStorageRef Clone() const { return CacheStorageRef(target_); }
private:
base::WeakPtr<TargetType> target_;
};
}
#endif