#pragma once
#include <wtf/text/UniquedStringImpl.h>
namespace WTF {
class SymbolImpl : public UniquedStringImpl {
private:
static constexpr const unsigned s_flagIsNullSymbol = 1u;
public:
unsigned hashForSymbol() const { return m_hashForSymbol; }
SymbolRegistry* const& symbolRegistry() const { return m_symbolRegistry; }
SymbolRegistry*& symbolRegistry() { return m_symbolRegistry; }
bool isNullSymbol() const { return m_flags & s_flagIsNullSymbol; }
WTF_EXPORT_STRING_API static Ref<SymbolImpl> createNullSymbol();
WTF_EXPORT_STRING_API static Ref<SymbolImpl> create(StringImpl& rep);
Ref<StringImpl> extractFoldedString()
{
ASSERT(substringBuffer());
ASSERT(substringBuffer() == m_owner);
ASSERT(!substringBuffer()->isSymbol());
return createSubstringSharingImpl(*this, 0, length());
}
private:
WTF_EXPORT_PRIVATE static unsigned nextHashForSymbol();
friend class StringImpl;
SymbolImpl(const LChar* characters, unsigned length, Ref<StringImpl>&& base)
: UniquedStringImpl(CreateSymbol, characters, length)
, m_owner(&base.leakRef())
, m_hashForSymbol(nextHashForSymbol())
{
ASSERT(StringImpl::tailOffset<StringImpl*>() == OBJECT_OFFSETOF(SymbolImpl, m_owner));
}
SymbolImpl(const UChar* characters, unsigned length, Ref<StringImpl>&& base)
: UniquedStringImpl(CreateSymbol, characters, length)
, m_owner(&base.leakRef())
, m_hashForSymbol(nextHashForSymbol())
{
ASSERT(StringImpl::tailOffset<StringImpl*>() == OBJECT_OFFSETOF(SymbolImpl, m_owner));
}
SymbolImpl()
: UniquedStringImpl(CreateSymbol)
, m_owner(StringImpl::empty())
, m_hashForSymbol(nextHashForSymbol())
, m_flags(s_flagIsNullSymbol)
{
ASSERT(StringImpl::tailOffset<StringImpl*>() == OBJECT_OFFSETOF(SymbolImpl, m_owner));
}
StringImpl* m_owner;
SymbolRegistry* m_symbolRegistry { nullptr };
unsigned m_hashForSymbol;
unsigned m_flags { 0 };
};
inline unsigned StringImpl::symbolAwareHash() const
{
if (isSymbol())
return static_cast<const SymbolImpl*>(this)->hashForSymbol();
return hash();
}
inline unsigned StringImpl::existingSymbolAwareHash() const
{
if (isSymbol())
return static_cast<const SymbolImpl*>(this)->hashForSymbol();
return existingHash();
}
#if !ASSERT_DISABLED
template<> struct
ValueCheck<SymbolImpl*> {
static void checkConsistency(const SymbolImpl*) { }
};
template<> struct
ValueCheck<const SymbolImpl*> {
static void checkConsistency(const SymbolImpl*) { }
};
#endif
}
using WTF::SymbolImpl;