CachedResourceHandle.h [plain text]
#pragma once
#include <wtf/Forward.h>
namespace WebCore {
class CachedResource;
class WEBCORE_EXPORT CachedResourceHandleBase {
public:
~CachedResourceHandleBase();
CachedResource* get() const { return m_resource; }
bool operator!() const { return !m_resource; }
typedef CachedResource* CachedResourceHandleBase::*UnspecifiedBoolType;
operator UnspecifiedBoolType() const { return m_resource ? &CachedResourceHandleBase::m_resource : 0; }
protected:
CachedResourceHandleBase();
CachedResourceHandleBase(CachedResource*);
CachedResourceHandleBase(const CachedResourceHandleBase&);
void setResource(CachedResource*);
private:
CachedResourceHandleBase& operator=(const CachedResourceHandleBase&) { return *this; }
friend class CachedResource;
CachedResource* m_resource;
};
template <class R> class CachedResourceHandle : public CachedResourceHandleBase {
public:
CachedResourceHandle() { }
CachedResourceHandle(R* res) : CachedResourceHandleBase(res) { }
CachedResourceHandle(const CachedResourceHandle<R>& o) : CachedResourceHandleBase(o) { }
template<typename U> CachedResourceHandle(const CachedResourceHandle<U>& o) : CachedResourceHandleBase(o.get()) { }
R* get() const { return reinterpret_cast<R*>(CachedResourceHandleBase::get()); }
R* operator->() const { return get(); }
R& operator*() const { ASSERT(get()); return *get(); }
CachedResourceHandle& operator=(R* res) { setResource(res); return *this; }
CachedResourceHandle& operator=(const CachedResourceHandle& o) { setResource(o.get()); return *this; }
template<typename U> CachedResourceHandle& operator=(const CachedResourceHandle<U>& o) { setResource(o.get()); return *this; }
bool operator==(const CachedResourceHandleBase& o) const { return get() == o.get(); }
bool operator!=(const CachedResourceHandleBase& o) const { return get() != o.get(); }
};
template <class R, class RR> bool operator==(const CachedResourceHandle<R>& h, const RR* res)
{
return h.get() == res;
}
template <class R, class RR> bool operator==(const RR* res, const CachedResourceHandle<R>& h)
{
return h.get() == res;
}
template <class R, class RR> bool operator!=(const CachedResourceHandle<R>& h, const RR* res)
{
return h.get() != res;
}
template <class R, class RR> bool operator!=(const RR* res, const CachedResourceHandle<R>& h)
{
return h.get() != res;
}
}