1/*
2 * Copyright (C) 2006 Apple Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#ifndef AtomicStringImpl_h
22#define AtomicStringImpl_h
23
24#include <wtf/text/UniquedStringImpl.h>
25
26namespace WTF {
27
28class AtomicStringTable;
29
30class AtomicStringImpl : public UniquedStringImpl {
31public:
32 static RefPtr<AtomicStringImpl> lookUp(LChar* characters, unsigned length)
33 {
34 return lookUpInternal(characters, length);
35 }
36 static RefPtr<AtomicStringImpl> lookUp(UChar* characters, unsigned length)
37 {
38 return lookUpInternal(characters, length);
39 }
40 static RefPtr<AtomicStringImpl> lookUp(StringImpl* string)
41 {
42 if (!string || string->isAtomic())
43 return static_cast<AtomicStringImpl*>(string);
44 return lookUpSlowCase(*string);
45 }
46
47 static void remove(AtomicStringImpl*);
48
49 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> add(const LChar*);
50 ALWAYS_INLINE static RefPtr<AtomicStringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); };
51 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> add(const LChar*, unsigned length);
52 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> add(const UChar*, unsigned length);
53 ALWAYS_INLINE static RefPtr<AtomicStringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); };
54 WTF_EXPORT_STRING_API static Ref<AtomicStringImpl> add(const UChar*, unsigned length, unsigned existingHash);
55 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> add(const UChar*);
56 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> add(StringImpl*, unsigned offset, unsigned length);
57 ALWAYS_INLINE static RefPtr<AtomicStringImpl> add(StringImpl* string)
58 {
59 if (!string)
60 return static_cast<AtomicStringImpl*>(string);
61 return add(*string);
62 }
63 WTF_EXPORT_STRING_API static Ref<AtomicStringImpl> addLiteral(const char* characters, unsigned length);
64
65 // Returns null if the input data contains an invalid UTF-8 sequence.
66 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> addUTF8(const char* start, const char* end);
67#if USE(CF)
68 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> add(CFStringRef);
69#endif
70
71 template<typename StringTableProvider>
72 ALWAYS_INLINE static RefPtr<AtomicStringImpl> addWithStringTableProvider(StringTableProvider& stringTableProvider, StringImpl* string)
73 {
74 if (!string)
75 return nullptr;
76 return add(*stringTableProvider.atomicStringTable(), *string);
77 }
78
79#if !ASSERT_DISABLED
80 WTF_EXPORT_STRING_API static bool isInAtomicStringTable(StringImpl*);
81#endif
82
83private:
84 AtomicStringImpl() = delete;
85
86 ALWAYS_INLINE static Ref<AtomicStringImpl> add(StringImpl& string)
87 {
88 if (string.isAtomic()) {
89 ASSERT_WITH_MESSAGE(!string.length() || isInAtomicStringTable(&string), "The atomic string comes from an other thread!");
90 return static_cast<AtomicStringImpl&>(string);
91 }
92 return addSlowCase(string);
93 }
94
95 ALWAYS_INLINE static Ref<AtomicStringImpl> add(AtomicStringTable& stringTable, StringImpl& string)
96 {
97 if (string.isAtomic()) {
98 ASSERT_WITH_MESSAGE(!string.length() || isInAtomicStringTable(&string), "The atomic string comes from an other thread!");
99 return static_cast<AtomicStringImpl&>(string);
100 }
101 return addSlowCase(stringTable, string);
102 }
103
104 WTF_EXPORT_STRING_API static Ref<AtomicStringImpl> addSlowCase(StringImpl&);
105 WTF_EXPORT_STRING_API static Ref<AtomicStringImpl> addSlowCase(AtomicStringTable&, StringImpl&);
106
107 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> lookUpSlowCase(StringImpl&);
108
109 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> lookUpInternal(const LChar*, unsigned length);
110 WTF_EXPORT_STRING_API static RefPtr<AtomicStringImpl> lookUpInternal(const UChar*, unsigned length);
111};
112
113#if !ASSERT_DISABLED
114// AtomicStringImpls created from StaticASCIILiteral will ASSERT
115// in the generic ValueCheck<T>::checkConsistency
116// as they are not allocated by fastMalloc.
117// We don't currently have any way to detect that case
118// so we ignore the consistency check for all AtomicStringImpls*.
119template<> struct
120ValueCheck<AtomicStringImpl*> {
121 static void checkConsistency(const AtomicStringImpl*) { }
122};
123
124template<> struct
125ValueCheck<const AtomicStringImpl*> {
126 static void checkConsistency(const AtomicStringImpl*) { }
127};
128#endif
129
130}
131
132using WTF::AtomicStringImpl;
133
134#endif
135