OptionalCallbackID.h [plain text]
#pragma once
#include "CallbackID.h"
#include <wtf/HashTraits.h>
#include <wtf/RunLoop.h>
namespace WebKit {
class OptionalCallbackID {
public:
explicit OptionalCallbackID() { }
ALWAYS_INLINE explicit OptionalCallbackID(const CallbackID& otherID)
: m_id(otherID.m_id)
{
}
ALWAYS_INLINE OptionalCallbackID(const OptionalCallbackID& otherID)
: m_id(otherID.m_id)
{
ASSERT(!HashTraits<uint64_t>::isDeletedValue(m_id));
}
ALWAYS_INLINE OptionalCallbackID& operator=(const OptionalCallbackID& otherID)
{
m_id = otherID.m_id;
return *this;
}
uint64_t toInteger() { return m_id; }
CallbackID callbackID()
{
RELEASE_ASSERT(CallbackID::isValidCallbackID(m_id));
return CallbackID(m_id);
}
operator bool() { return m_id; }
ALWAYS_INLINE bool isValid() const { return isValidCallbackID(m_id); }
static ALWAYS_INLINE bool isValidCallbackID(uint64_t rawId)
{
return !HashTraits<uint64_t>::isDeletedValue(rawId);
}
template<class Encoder> void encode(Encoder& encoder) const
{
RELEASE_ASSERT(isValid());
encoder << m_id;
}
template<class Decoder> static WARN_UNUSED_RETURN bool decode(Decoder& decoder, OptionalCallbackID& callbackID)
{
auto result = decoder.decode(callbackID.m_id);
RELEASE_ASSERT(callbackID.isValid());
return result;
}
private:
uint64_t m_id { HashTraits<uint64_t>::emptyValue() };
};
}