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 | #pragma once |
22 | |
23 | #include <wtf/text/UniquedStringImpl.h> |
24 | |
25 | namespace WTF { |
26 | |
27 | class AtomStringTable; |
28 | |
29 | class AtomStringImpl : public UniquedStringImpl { |
30 | public: |
31 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> lookUp(const LChar*, unsigned length); |
32 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> lookUp(const UChar*, unsigned length); |
33 | static RefPtr<AtomStringImpl> lookUp(StringImpl* string) |
34 | { |
35 | if (!string || string->isAtom()) |
36 | return static_cast<AtomStringImpl*>(string); |
37 | return lookUpSlowCase(*string); |
38 | } |
39 | |
40 | static void remove(AtomStringImpl*); |
41 | |
42 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const LChar*); |
43 | ALWAYS_INLINE static RefPtr<AtomStringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }; |
44 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const LChar*, unsigned length); |
45 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const UChar*, unsigned length); |
46 | ALWAYS_INLINE static RefPtr<AtomStringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }; |
47 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const UChar*); |
48 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(StringImpl*, unsigned offset, unsigned length); |
49 | ALWAYS_INLINE static RefPtr<AtomStringImpl> add(StringImpl* string) |
50 | { |
51 | if (!string) |
52 | return static_cast<AtomStringImpl*>(string); |
53 | return add(*string); |
54 | } |
55 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const StaticStringImpl*); |
56 | WTF_EXPORT_PRIVATE static Ref<AtomStringImpl> addLiteral(const char* characters, unsigned length); |
57 | |
58 | // Returns null if the input data contains an invalid UTF-8 sequence. |
59 | static RefPtr<AtomStringImpl> addUTF8(const char* start, const char* end); |
60 | |
61 | #if USE(CF) |
62 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(CFStringRef); |
63 | #endif |
64 | |
65 | template<typename StringTableProvider> |
66 | ALWAYS_INLINE static RefPtr<AtomStringImpl> addWithStringTableProvider(StringTableProvider& stringTableProvider, StringImpl* string) |
67 | { |
68 | if (!string) |
69 | return nullptr; |
70 | return add(*stringTableProvider.atomStringTable(), *string); |
71 | } |
72 | |
73 | #if !ASSERT_DISABLED |
74 | WTF_EXPORT_PRIVATE static bool isInAtomStringTable(StringImpl*); |
75 | #endif |
76 | |
77 | private: |
78 | AtomStringImpl() = delete; |
79 | |
80 | ALWAYS_INLINE static Ref<AtomStringImpl> add(StringImpl& string) |
81 | { |
82 | if (string.isAtom()) { |
83 | ASSERT_WITH_MESSAGE(!string.length() || isInAtomStringTable(&string), "The atomic string comes from an other thread!" ); |
84 | return static_cast<AtomStringImpl&>(string); |
85 | } |
86 | return addSlowCase(string); |
87 | } |
88 | |
89 | ALWAYS_INLINE static Ref<AtomStringImpl> add(AtomStringTable& stringTable, StringImpl& string) |
90 | { |
91 | if (string.isAtom()) { |
92 | ASSERT_WITH_MESSAGE(!string.length() || isInAtomStringTable(&string), "The atomic string comes from an other thread!" ); |
93 | return static_cast<AtomStringImpl&>(string); |
94 | } |
95 | return addSlowCase(stringTable, string); |
96 | } |
97 | |
98 | WTF_EXPORT_PRIVATE static Ref<AtomStringImpl> addSlowCase(StringImpl&); |
99 | WTF_EXPORT_PRIVATE static Ref<AtomStringImpl> addSlowCase(AtomStringTable&, StringImpl&); |
100 | |
101 | WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> lookUpSlowCase(StringImpl&); |
102 | }; |
103 | |
104 | #if !ASSERT_DISABLED |
105 | // AtomicStringImpls created from StaticStringImpl will ASSERT |
106 | // in the generic ValueCheck<T>::checkConsistency |
107 | // as they are not allocated by fastMalloc. |
108 | // We don't currently have any way to detect that case |
109 | // so we ignore the consistency check for all AtomicStringImpls*. |
110 | template<> struct |
111 | ValueCheck<AtomStringImpl*> { |
112 | static void checkConsistency(const AtomStringImpl*) { } |
113 | }; |
114 | |
115 | template<> struct |
116 | ValueCheck<const AtomStringImpl*> { |
117 | static void checkConsistency(const AtomStringImpl*) { } |
118 | }; |
119 | #endif |
120 | |
121 | } |
122 | |
123 | using WTF::AtomStringImpl; |
124 | |