]> git.saurik.com Git - iphone-api.git/blob - WebCore/AtomicString.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / AtomicString.h
1 /*
2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
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 AtomicString_h
22 #define AtomicString_h
23
24 #include "AtomicStringImpl.h"
25 #include "PlatformString.h"
26
27 namespace WebCore {
28
29 struct AtomicStringHash;
30
31 class AtomicString {
32 public:
33 static void init();
34
35 AtomicString() { }
36 AtomicString(const char* s) : m_string(add(s)) { }
37 AtomicString(const UChar* s, int length) : m_string(add(s, length)) { }
38 AtomicString(const UChar* s) : m_string(add(s)) { }
39 #if USE(JSC)
40 AtomicString(const JSC::UString& s) : m_string(add(s)) { }
41 AtomicString(const JSC::Identifier& s) : m_string(add(s)) { }
42 #endif
43 AtomicString(StringImpl* imp) : m_string(add(imp)) { }
44 AtomicString(AtomicStringImpl* imp) : m_string(imp) { }
45 AtomicString(const String& s) : m_string(add(s.impl())) { }
46
47 // Hash table deleted values, which are only constructed and never copied or destroyed.
48 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
49 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); }
50
51 #if USE(JSC)
52 static AtomicStringImpl* find(const JSC::Identifier&);
53 #endif
54
55 operator const String&() const { return m_string; }
56 const String& string() const { return m_string; };
57
58 #if USE(JSC)
59 operator JSC::UString() const;
60 #endif
61
62 AtomicStringImpl* impl() const { return static_cast<AtomicStringImpl *>(m_string.impl()); }
63
64 const UChar* characters() const { return m_string.characters(); }
65 unsigned length() const { return m_string.length(); }
66
67 UChar operator[](unsigned int i) const { return m_string[i]; }
68
69 bool contains(UChar c) const { return m_string.contains(c); }
70 bool contains(const AtomicString& s, bool caseSensitive = true) const
71 { return m_string.contains(s.string(), caseSensitive); }
72
73 int find(UChar c, int start = 0) const { return m_string.find(c, start); }
74 int find(const AtomicString& s, int start = 0, bool caseSentitive = true) const
75 { return m_string.find(s.string(), start, caseSentitive); }
76
77 bool startsWith(const AtomicString& s, bool caseSensitive = true) const
78 { return m_string.startsWith(s.string(), caseSensitive); }
79 bool endsWith(const AtomicString& s, bool caseSensitive = true) const
80 { return m_string.endsWith(s.string(), caseSensitive); }
81
82 int toInt(bool* ok = 0) const { return m_string.toInt(ok); }
83 double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); }
84 float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); }
85 bool percentage(int& p) const { return m_string.percentage(p); }
86
87 bool isNull() const { return m_string.isNull(); }
88 bool isEmpty() const { return m_string.isEmpty(); }
89
90 static void remove(StringImpl*);
91
92 #if PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN))
93 AtomicString(CFStringRef s) : m_string(add(String(s).impl())) { }
94 CFStringRef createCFString() const { return m_string.createCFString(); }
95 #endif
96 #ifdef __OBJC__
97 AtomicString(NSString* s) : m_string(add(String(s).impl())) { }
98 operator NSString*() const { return m_string; }
99 #endif
100 #if PLATFORM(QT)
101 AtomicString(const QString& s) : m_string(add(String(s).impl())) { }
102 operator QString() const { return m_string; }
103 #endif
104
105 private:
106 String m_string;
107
108 static PassRefPtr<StringImpl> add(const char*);
109 static PassRefPtr<StringImpl> add(const UChar*, int length);
110 static PassRefPtr<StringImpl> add(const UChar*);
111 static PassRefPtr<StringImpl> add(StringImpl*);
112 #if USE(JSC)
113 static PassRefPtr<StringImpl> add(const JSC::UString&);
114 static PassRefPtr<StringImpl> add(const JSC::Identifier&);
115 #endif
116 };
117
118 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); }
119 bool operator==(const AtomicString& a, const char* b);
120 inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); }
121 inline bool operator==(const char* a, const AtomicString& b) { return b == a; }
122 inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); }
123
124 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); }
125 inline bool operator!=(const AtomicString& a, const char *b) { return !(a == b); }
126 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); }
127 inline bool operator!=(const char* a, const AtomicString& b) { return !(b == a); }
128 inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); }
129
130 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
131 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), b); }
132 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
133 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); }
134 inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
135
136 // Define external global variables for the commonly used atomic strings.
137 // These are only usable from the main thread.
138 #ifndef ATOMICSTRING_HIDE_GLOBALS
139 extern const AtomicString nullAtom;
140 extern const AtomicString emptyAtom;
141 extern const AtomicString textAtom;
142 extern const AtomicString commentAtom;
143 extern const AtomicString starAtom;
144 #endif
145
146 } // namespace WebCore
147
148
149 namespace WTF {
150
151 // AtomicStringHash is the default hash for AtomicString
152 template<typename T> struct DefaultHash;
153 template<> struct DefaultHash<WebCore::AtomicString> {
154 typedef WebCore::AtomicStringHash Hash;
155 };
156
157 } // namespace WTF
158
159 #endif // AtomicString_h