1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
|
|
4 ** Copyright 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 #ifndef PP_ENVIRONMENT_H
|
|
44 #define PP_ENVIRONMENT_H
|
|
45
|
|
46 #include <vector>
|
|
47 #include <cstring>
|
|
48
|
|
49 namespace rpp {
|
|
50
|
|
51 class pp_environment
|
|
52 {
|
|
53 public:
|
|
54 typedef std::vector<pp_macro*>::const_iterator const_iterator;
|
|
55
|
|
56 public:
|
|
57 pp_environment ():
|
|
58 current_line (0),
|
|
59 _M_hash_size (4093)
|
|
60 {
|
|
61 _M_base = (pp_macro **) memset (new pp_macro* [_M_hash_size], 0, _M_hash_size * sizeof (pp_macro*));
|
|
62 }
|
|
63
|
|
64 ~pp_environment ()
|
|
65 {
|
|
66 for (std::size_t i = 0; i < _M_macros.size (); ++i)
|
|
67 delete _M_macros [i];
|
|
68
|
|
69 delete [] _M_base;
|
|
70 }
|
|
71
|
|
72 const_iterator first_macro () const { return _M_macros.begin (); }
|
|
73 const_iterator last_macro () const { return _M_macros.end (); }
|
|
74
|
|
75 inline void bind (pp_fast_string const *__name, pp_macro const &__macro)
|
|
76 {
|
|
77 std::size_t h = hash_code (*__name) % _M_hash_size;
|
|
78 pp_macro *m = new pp_macro (__macro);
|
|
79 m->name = __name;
|
|
80 m->next = _M_base [h];
|
|
81 m->hash_code = h;
|
|
82 _M_base [h] = m;
|
|
83
|
|
84 _M_macros.push_back (m);
|
|
85
|
|
86 if (_M_macros.size() == _M_hash_size)
|
|
87 rehash();
|
|
88 }
|
|
89
|
|
90 inline void unbind (pp_fast_string const *__name)
|
|
91 {
|
|
92 if (pp_macro *m = resolve (__name))
|
|
93 m->hidden = true;
|
|
94 }
|
|
95
|
|
96 inline void unbind (char const *__s, std::size_t __size)
|
|
97 {
|
|
98 pp_fast_string __tmp (__s, __size);
|
|
99 unbind (&__tmp);
|
|
100 }
|
|
101
|
|
102 inline pp_macro *resolve (pp_fast_string const *__name) const
|
|
103 {
|
|
104 std::size_t h = hash_code (*__name) % _M_hash_size;
|
|
105 pp_macro *it = _M_base [h];
|
|
106
|
|
107 while (it && it->name && it->hash_code == h && (*it->name != *__name || it->hidden))
|
|
108 it = it->next;
|
|
109
|
|
110 return it;
|
|
111 }
|
|
112
|
|
113 inline pp_macro *resolve (char const *__data, std::size_t __size) const
|
|
114 {
|
|
115 pp_fast_string const __tmp (__data, __size);
|
|
116 return resolve (&__tmp);
|
|
117 }
|
|
118
|
|
119 std::string current_file;
|
|
120 int current_line;
|
|
121
|
|
122 private:
|
|
123 inline std::size_t hash_code (pp_fast_string const &s) const
|
|
124 {
|
|
125 std::size_t hash_value = 0;
|
|
126
|
|
127 for (std::size_t i = 0; i < s.size (); ++i)
|
|
128 hash_value = (hash_value << 5) - hash_value + s.at (i);
|
|
129
|
|
130 return hash_value;
|
|
131 }
|
|
132
|
|
133 void rehash()
|
|
134 {
|
|
135 delete[] _M_base;
|
|
136
|
|
137 _M_hash_size <<= 1;
|
|
138
|
|
139 _M_base = (pp_macro **) memset (new pp_macro* [_M_hash_size], 0, _M_hash_size * sizeof(pp_macro*));
|
|
140 for (std::size_t index = 0; index < _M_macros.size (); ++index)
|
|
141 {
|
|
142 pp_macro *elt = _M_macros [index];
|
|
143 std::size_t h = hash_code (*elt->name) % _M_hash_size;
|
|
144 elt->next = _M_base [h];
|
|
145 elt->hash_code = h;
|
|
146 _M_base [h] = elt;
|
|
147 }
|
|
148 }
|
|
149
|
|
150 private:
|
|
151 std::vector<pp_macro*> _M_macros;
|
|
152 pp_macro **_M_base;
|
|
153 std::size_t _M_hash_size;
|
|
154 };
|
|
155
|
|
156 } // namespace rpp
|
|
157
|
|
158 #endif // PP_ENVIRONMENT_H
|
|
159
|
|
160 // kate: space-indent on; indent-width 2; replace-tabs on;
|