1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
|
|
4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
|
|
5 **
|
|
6 ** This file is part of Qt Jambi.
|
|
7 **
|
|
8 ** * Commercial Usage
|
|
9 * Licensees holding valid Qt Commercial licenses may use this file in
|
|
10 * accordance with the Qt Commercial License Agreement provided with the
|
|
11 * Software or, alternatively, in accordance with the terms contained in
|
|
12 * a written agreement between you and Nokia.
|
|
13 *
|
|
14 *
|
|
15 * GNU General Public License Usage
|
|
16 * Alternatively, this file may be used under the terms of the GNU
|
|
17 * General Public License versions 2.0 or 3.0 as published by the Free
|
|
18 * Software Foundation and appearing in the file LICENSE.GPL included in
|
|
19 * the packaging of this file. Please review the following information
|
|
20 * to ensure GNU General Public Licensing requirements will be met:
|
|
21 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
22 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
|
23 * exception, Nokia gives you certain additional rights. These rights
|
|
24 * are described in the Nokia Qt GPL Exception version 1.2, included in
|
|
25 * the file GPL_EXCEPTION.txt in this package.
|
|
26 *
|
|
27 * Qt for Windows(R) Licensees
|
|
28 * As a special exception, Nokia, as the sole copyright holder for Qt
|
|
29 * Designer, grants users of the Qt/Eclipse Integration plug-in the
|
|
30 * right for the Qt/Eclipse Integration to link to functionality
|
|
31 * provided by Qt Designer and its related libraries.
|
|
32 *
|
|
33 *
|
|
34 * If you are unsure which license is appropriate for your use, please
|
|
35 * contact the sales department at qt-sales@nokia.com.
|
|
36
|
|
37 **
|
|
38 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
39 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
40 **
|
|
41 ****************************************************************************/
|
|
42
|
|
43
|
|
44 #ifndef SYMBOL_H
|
|
45 #define SYMBOL_H
|
|
46
|
|
47 #include <QtCore/QString>
|
|
48 #include <cstring>
|
|
49
|
|
50 #include <QtCore/QHash>
|
|
51 #include <QtCore/QPair>
|
|
52
|
|
53 struct NameSymbol
|
|
54 {
|
|
55 const char *data;
|
|
56 std::size_t count;
|
|
57
|
|
58 inline QString as_string() const
|
|
59 {
|
|
60 return QString::fromUtf8(data, (int) count);
|
|
61 }
|
|
62
|
|
63 inline bool operator == (const NameSymbol &other) const
|
|
64 {
|
|
65 return count == other.count
|
|
66 && std::strncmp(data, other.data, count) == 0;
|
|
67 }
|
|
68
|
|
69 protected:
|
|
70 inline NameSymbol() {}
|
|
71 inline NameSymbol(const char *d, std::size_t c)
|
|
72 : data(d), count(c) {}
|
|
73
|
|
74 private:
|
|
75 void operator = (const NameSymbol &);
|
|
76
|
|
77 friend class NameTable;
|
|
78 };
|
|
79
|
|
80 inline uint qHash(const NameSymbol &r)
|
|
81 {
|
|
82 uint hash_value = 0;
|
|
83
|
|
84 for (std::size_t i=0; i<r.count; ++i)
|
|
85 hash_value = (hash_value << 5) - hash_value + r.data[i];
|
|
86
|
|
87 return hash_value;
|
|
88 }
|
|
89
|
|
90 inline uint qHash(const QPair<const char*, std::size_t> &r)
|
|
91 {
|
|
92 uint hash_value = 0;
|
|
93
|
|
94 for (std::size_t i=0; i<r.second; ++i)
|
|
95 hash_value = (hash_value << 5) - hash_value + r.first[i];
|
|
96
|
|
97 return hash_value;
|
|
98 }
|
|
99
|
|
100 class NameTable
|
|
101 {
|
|
102 public:
|
|
103 typedef QPair<const char *, std::size_t> KeyType;
|
|
104 typedef QHash<KeyType, NameSymbol*> ContainerType;
|
|
105
|
|
106 public:
|
|
107 NameTable() {}
|
|
108
|
|
109 ~NameTable()
|
|
110 {
|
|
111 qDeleteAll(_M_storage);
|
|
112 }
|
|
113
|
|
114 inline const NameSymbol *findOrInsert(const char *str, std::size_t len)
|
|
115 {
|
|
116 KeyType key(str, len);
|
|
117
|
|
118 NameSymbol *name = _M_storage.value(key);
|
|
119 if (!name)
|
|
120 {
|
|
121 name = new NameSymbol(str, len);
|
|
122 _M_storage.insert(key, name);
|
|
123 }
|
|
124
|
|
125 return name;
|
|
126 }
|
|
127
|
|
128 inline std::size_t count() const
|
|
129 {
|
|
130 return _M_storage.size();
|
|
131 }
|
|
132
|
|
133 private:
|
|
134 ContainerType _M_storage;
|
|
135
|
|
136 private:
|
|
137 NameTable(const NameTable &other);
|
|
138 void operator = (const NameTable &other);
|
|
139 };
|
|
140
|
|
141 #endif // SYMBOL_H
|
|
142
|
|
143 // kate: space-indent on; indent-width 2; replace-tabs on;
|