1 | // |
---|
2 | // NamePool.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/XML/src/NamePool.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: XML |
---|
7 | // Package: XML |
---|
8 | // Module: NamePool |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/XML/NamePool.h> |
---|
38 | #include <Poco/Exception.h> |
---|
39 | |
---|
40 | |
---|
41 | namespace Poco { |
---|
42 | namespace XML { |
---|
43 | |
---|
44 | |
---|
45 | class NamePoolItem |
---|
46 | { |
---|
47 | public: |
---|
48 | NamePoolItem(): _used(false) |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 | ~NamePoolItem() |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | bool set(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) |
---|
57 | { |
---|
58 | if (!_used) |
---|
59 | { |
---|
60 | _name.assign(qname, namespaceURI, localName); |
---|
61 | _used = true; |
---|
62 | return true; |
---|
63 | } |
---|
64 | else return _name.equals(qname, namespaceURI, localName); |
---|
65 | } |
---|
66 | |
---|
67 | const Name& get() const |
---|
68 | { |
---|
69 | return _name; |
---|
70 | } |
---|
71 | |
---|
72 | bool used() const |
---|
73 | { |
---|
74 | return _used; |
---|
75 | } |
---|
76 | |
---|
77 | private: |
---|
78 | Name _name; |
---|
79 | bool _used; |
---|
80 | }; |
---|
81 | |
---|
82 | |
---|
83 | NamePool::NamePool(unsigned long size): |
---|
84 | _size(size), |
---|
85 | _rc(1) |
---|
86 | { |
---|
87 | poco_assert (size > 1); |
---|
88 | |
---|
89 | _pItems = new NamePoolItem[size]; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | NamePool::~NamePool() |
---|
94 | { |
---|
95 | delete [] _pItems; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void NamePool::duplicate() |
---|
100 | { |
---|
101 | ++_rc; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | void NamePool::release() |
---|
106 | { |
---|
107 | if (--_rc == 0) |
---|
108 | delete this; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | const Name& NamePool::insert(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) |
---|
113 | { |
---|
114 | unsigned long i = 0; |
---|
115 | unsigned long n = hash(qname, namespaceURI, localName) % _size; |
---|
116 | |
---|
117 | while (!_pItems[n].set(qname, namespaceURI, localName) && i++ < _size) |
---|
118 | n = (n + 1) % _size; |
---|
119 | |
---|
120 | if (i > _size) throw Poco::PoolOverflowException("XML name pool"); |
---|
121 | |
---|
122 | return _pItems[n].get(); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | const Name& NamePool::insert(const Name& name) |
---|
127 | { |
---|
128 | return insert(name.qname(), name.namespaceURI(), name.localName()); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | unsigned long NamePool::hash(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) |
---|
133 | { |
---|
134 | unsigned long h = 0; |
---|
135 | XMLString::const_iterator it = qname.begin(); |
---|
136 | XMLString::const_iterator end = qname.end(); |
---|
137 | while (it != end) h = (h << 5) + h + (unsigned long) *it++; |
---|
138 | it = namespaceURI.begin(); |
---|
139 | end = namespaceURI.end(); |
---|
140 | while (it != end) h = (h << 5) + h + (unsigned long) *it++; |
---|
141 | it = localName.begin(); |
---|
142 | end = localName.end(); |
---|
143 | while (it != end) h = (h << 5) + h + (unsigned long) *it++; |
---|
144 | return h; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | } } // namespace Poco::XML |
---|