support for creating template instance classes
[lqt/mk.git] / cpptoxml / parser / rpp / pp-environment.h
blobd203629ad2aadd9f6bd33614939864e5bdc246f6
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
5 **
6 ** This file is part of $PRODUCT$.
7 **
8 ** $CPP_LICENSE$
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 #ifndef PP_ENVIRONMENT_H
16 #define PP_ENVIRONMENT_H
18 #include <vector>
19 #include <cstring>
21 namespace rpp {
23 class pp_environment
25 public:
26 typedef std::vector<pp_macro*>::const_iterator const_iterator;
28 public:
29 pp_environment ():
30 current_line (0),
31 _M_hash_size (4093)
33 _M_base = (pp_macro **) memset (new pp_macro* [_M_hash_size], 0, _M_hash_size * sizeof (pp_macro*));
36 ~pp_environment ()
38 for (std::size_t i = 0; i < _M_macros.size (); ++i)
39 delete _M_macros [i];
41 delete [] _M_base;
44 const_iterator first_macro () const { return _M_macros.begin (); }
45 const_iterator last_macro () const { return _M_macros.end (); }
47 inline void bind (pp_fast_string const *__name, pp_macro const &__macro)
49 std::size_t h = hash_code (*__name) % _M_hash_size;
50 pp_macro *m = new pp_macro (__macro);
51 m->name = __name;
52 m->next = _M_base [h];
53 m->hash_code = h;
54 _M_base [h] = m;
56 _M_macros.push_back (m);
58 if (_M_macros.size() == _M_hash_size)
59 rehash();
62 inline void unbind (pp_fast_string const *__name)
64 if (pp_macro *m = resolve (__name))
65 m->f.hidden = true;
68 inline void unbind (char const *__s, std::size_t __size)
70 pp_fast_string __tmp (__s, __size);
71 unbind (&__tmp);
74 inline pp_macro *resolve (pp_fast_string const *__name) const
76 std::size_t h = hash_code (*__name) % _M_hash_size;
77 pp_macro *it = _M_base [h];
79 while (it && it->name && it->hash_code == h && (*it->name != *__name || it->f.hidden))
80 it = it->next;
82 return it;
85 inline pp_macro *resolve (char const *__data, std::size_t __size) const
87 pp_fast_string const __tmp (__data, __size);
88 return resolve (&__tmp);
91 std::string current_file;
92 int current_line;
94 private:
95 inline std::size_t hash_code (pp_fast_string const &s) const
97 std::size_t hash_value = 0;
99 for (std::size_t i = 0; i < s.size (); ++i)
100 hash_value = (hash_value << 5) - hash_value + s.at (i);
102 return hash_value;
105 void rehash()
107 delete[] _M_base;
109 _M_hash_size <<= 1;
111 _M_base = (pp_macro **) memset (new pp_macro* [_M_hash_size], 0, _M_hash_size * sizeof(pp_macro*));
112 for (std::size_t index = 0; index < _M_macros.size (); ++index)
114 pp_macro *elt = _M_macros [index];
115 std::size_t h = hash_code (*elt->name) % _M_hash_size;
116 elt->next = _M_base [h];
117 elt->hash_code = h;
118 _M_base [h] = elt;
122 private:
123 std::vector<pp_macro*> _M_macros;
124 pp_macro **_M_base;
125 std::size_t _M_hash_size;
128 } // namespace rpp
130 #endif // PP_ENVIRONMENT_H
132 // kate: space-indent on; indent-width 2; replace-tabs on;