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