Add rpp files
[rpp.git] / src / symbol.h
blob8a61674b35a321da04a14f6a372ad3c8214b5abb
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of Qt Jambi.
6 **
7 ** ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
20 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 ****************************************************************************/
25 /* This file is part of KDevelop
26 Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
28 This library is free software; you can redistribute it and/or
29 modify it under the terms of the GNU Library General Public
30 License version 2 as published by the Free Software Foundation.
32 This library is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 Library General Public License for more details.
37 You should have received a copy of the GNU Library General Public License
38 along with this library; see the file COPYING.LIB. If not, write to
39 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
40 Boston, MA 02110-1301, USA.
43 #ifndef SYMBOL_H
44 #define SYMBOL_H
46 #include <QtCore/QString>
47 #include <cstring>
49 #include <QtCore/QHash>
50 #include <QtCore/QPair>
52 struct NameSymbol
54 const char *data;
55 std::size_t count;
57 inline QString as_string() const
59 return QString::fromUtf8(data, (int) count);
62 inline bool operator == (const NameSymbol &other) const
64 return count == other.count
65 && std::strncmp(data, other.data, count) == 0;
68 protected:
69 inline NameSymbol() {}
70 inline NameSymbol(const char *d, std::size_t c)
71 : data(d), count(c) {}
73 private:
74 void operator = (const NameSymbol &);
76 friend class NameTable;
79 inline uint qHash(const NameSymbol &r)
81 uint hash_value = 0;
83 for (std::size_t i=0; i<r.count; ++i)
84 hash_value = (hash_value << 5) - hash_value + r.data[i];
86 return hash_value;
89 inline uint qHash(const QPair<const char*, std::size_t> &r)
91 uint hash_value = 0;
93 for (std::size_t i=0; i<r.second; ++i)
94 hash_value = (hash_value << 5) - hash_value + r.first[i];
96 return hash_value;
99 class NameTable
101 public:
102 typedef QPair<const char *, std::size_t> KeyType;
103 typedef QHash<KeyType, NameSymbol*> ContainerType;
105 public:
106 NameTable() {}
108 ~NameTable()
110 qDeleteAll(_M_storage);
113 inline const NameSymbol *findOrInsert(const char *str, std::size_t len)
115 KeyType key(str, len);
117 NameSymbol *name = _M_storage.value(key);
118 if (!name)
120 name = new NameSymbol(str, len);
121 _M_storage.insert(key, name);
124 return name;
127 inline std::size_t count() const
129 return _M_storage.size();
132 private:
133 ContainerType _M_storage;
135 private:
136 NameTable(const NameTable &other);
137 void operator = (const NameTable &other);
140 #endif // SYMBOL_H
142 // kate: space-indent on; indent-width 2; replace-tabs on;