#pragma once
#include "JSExportMacros.h"
#include <cstddef>
#include <wtf/HashTraits.h>
#include <wtf/Noncopyable.h>
#include <wtf/VectorTraits.h>
namespace JSC {
class WeakImpl;
class WeakHandleOwner;
JS_EXPORT_PRIVATE void weakClearSlowCase(WeakImpl*&);
template<typename T> class Weak {
WTF_MAKE_NONCOPYABLE(Weak);
public:
Weak() = default;
constexpr Weak(std::nullptr_t)
: Weak()
{
}
Weak(T*, WeakHandleOwner* = nullptr, void* context = nullptr);
bool isHashTableDeletedValue() const;
Weak(WTF::HashTableDeletedValueType);
constexpr bool isHashTableEmptyValue() const { return !m_impl; }
Weak(Weak&&);
~Weak()
{
clear();
}
inline void swap(Weak&);
inline Weak& operator=(Weak&&);
inline bool operator!() const;
inline T* operator->() const;
inline T& operator*() const;
inline T* get() const;
inline bool was(T*) const;
inline explicit operator bool() const;
inline WeakImpl* leakImpl() WARN_UNUSED_RETURN;
WeakImpl* unsafeImpl() const { return m_impl; }
void clear()
{
if (!m_impl)
return;
weakClearSlowCase(m_impl);
}
private:
static inline WeakImpl* hashTableDeletedValue();
WeakImpl* m_impl { nullptr };
};
}
namespace WTF {
template<typename T> struct VectorTraits<JSC::Weak<T>> : SimpleClassVectorTraits {
static constexpr bool canCompareWithMemcmp = false;
};
template<typename T> struct HashTraits<JSC::Weak<T>> : SimpleClassHashTraits<JSC::Weak<T>> {
typedef JSC::Weak<T> StorageType;
typedef std::nullptr_t EmptyValueType;
static EmptyValueType emptyValue() { return nullptr; }
static constexpr bool hasIsEmptyValueFunction = true;
static bool isEmptyValue(const JSC::Weak<T>& value) { return value.isHashTableEmptyValue(); }
typedef T* PeekType;
static PeekType peek(const StorageType& value) { return value.get(); }
static PeekType peek(EmptyValueType) { return PeekType(); }
};
}