fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / khtml / svg / SVGList.h
blob0422fd35f845c51e8d297081d257bf6502d7c9f4
1 /*
2 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005 Rob Buis <buis@kde.org>
5 This file is part of the KDE project
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #ifndef SVGList_h
24 #define SVGList_h
26 #if ENABLE(SVG)
27 #include "ExceptionCode.h"
28 #include "SVGListTraits.h"
29 #include "Document.h"
31 #include <wtf/RefCounted.h>
32 #include <wtf/PassRefPtr.h>
33 #include <wtf/Vector.h>
35 namespace WebCore {
37 //class QualifiedName;
39 template<typename Item>
40 struct SVGListTypeOperations {
41 static Item nullItem()
43 return SVGListTraits<UsesDefaultInitializer<Item>::value, Item>::nullItem();
47 template<typename Item>
48 class SVGList : public RefCounted<SVGList<Item> > {
49 private:
50 typedef SVGListTypeOperations<Item> TypeOperations;
52 public:
53 virtual ~SVGList() { }
55 const QualifiedName& associatedAttributeName() const { return m_associatedAttributeName; }
57 unsigned int numberOfItems() const { return m_vector.size(); }
58 void clear(ExceptionCode &) { m_vector.clear(); }
60 Item initialize(Item newItem, ExceptionCode& ec)
62 clear(ec);
63 return appendItem(newItem, ec);
66 Item getFirst() const
68 ExceptionCode ec = 0;
69 return getItem(0, ec);
72 Item getLast() const
74 ExceptionCode ec = 0;
75 return getItem(m_vector.size() - 1, ec);
78 Item getItem(unsigned int index, ExceptionCode& ec)
80 if (index >= m_vector.size()) {
81 ec = DOMException::INDEX_SIZE_ERR;
82 return TypeOperations::nullItem();
85 return m_vector.at(index);
88 const Item getItem(unsigned int index, ExceptionCode& ec) const
90 if (index >= m_vector.size()) {
91 ec = DOMException::INDEX_SIZE_ERR;
92 return TypeOperations::nullItem();
95 return m_vector[index];
98 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode&)
100 m_vector.insert(index, newItem);
101 return newItem;
104 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec)
106 if (index >= m_vector.size()) {
107 ec = DOMException::INDEX_SIZE_ERR;
108 return TypeOperations::nullItem();
111 m_vector[index] = newItem;
112 return newItem;
115 Item removeItem(unsigned int index, ExceptionCode& ec)
117 if (index >= m_vector.size()) {
118 ec = DOMException::INDEX_SIZE_ERR;
119 return TypeOperations::nullItem();
122 Item item = m_vector[index];
123 m_vector.remove(index);
124 return item;
127 Item appendItem(Item newItem, ExceptionCode&)
129 m_vector.append(newItem);
130 return newItem;
133 protected:
134 SVGList(const QualifiedName& attributeName)
135 : m_associatedAttributeName(attributeName)
139 private:
140 Vector<Item> m_vector;
141 const QualifiedName& m_associatedAttributeName;
144 template<typename Item>
145 class SVGPODListItem : public RefCounted<SVGPODListItem<Item> > {
146 public:
147 static PassRefPtr<SVGPODListItem> create() { return adoptRef(new SVGPODListItem); }
148 static PassRefPtr<SVGPODListItem> copy(const Item& item) { return adoptRef(new SVGPODListItem(item)); }
150 operator Item&() { return m_item; }
151 operator const Item&() const { return m_item; }
153 // Updating facilities, used by JSSVGPODTypeWrapperCreatorForList
154 Item value() const { return m_item; }
155 void setValue(Item newItem) { m_item = newItem; }
157 private:
158 SVGPODListItem() : m_item() { }
159 SVGPODListItem(const Item& item) : RefCounted<SVGPODListItem<Item> >(), m_item(item) { }
161 Item m_item;
164 template<typename Item>
165 class SVGPODList : public SVGList<RefPtr<SVGPODListItem<Item> > >
167 public:
168 Item initialize(Item newItem, ExceptionCode& ec)
170 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::initialize(SVGPODListItem<Item>::copy(newItem), ec).get());
171 if (!ptr)
172 return Item();
174 return static_cast<const Item&>(*ptr);
177 Item getFirst() const
179 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getFirst().get());
180 if (!ptr)
181 return Item();
183 return static_cast<const Item&>(*ptr);
186 Item getLast() const
188 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getLast().get());
189 if (!ptr)
190 return Item();
192 return static_cast<const Item&>(*ptr);
195 Item getItem(unsigned int index, ExceptionCode& ec)
197 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get());
198 if (!ptr)
199 return Item();
201 return static_cast<const Item&>(*ptr);
204 const Item getItem(unsigned int index, ExceptionCode& ec) const
206 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get());
207 if (!ptr)
208 return Item();
210 return static_cast<const Item&>(*ptr);
213 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode& ec)
215 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::insertItemBefore(SVGPODListItem<Item>::copy(newItem), index, ec).get());
216 if (!ptr)
217 return Item();
219 return static_cast<const Item&>(*ptr);
222 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec)
224 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::replaceItem(SVGPODListItem<Item>::copy(newItem), index, ec).get());
225 if (!ptr)
226 return Item();
228 return static_cast<const Item&>(*ptr);
231 Item removeItem(unsigned int index, ExceptionCode& ec)
233 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::removeItem(index, ec).get());
234 if (!ptr)
235 return Item();
237 return static_cast<const Item&>(*ptr);
240 Item appendItem(Item newItem, ExceptionCode& ec)
242 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::appendItem(SVGPODListItem<Item>::copy(newItem), ec).get());
243 if (!ptr)
244 return Item();
246 return static_cast<const Item&>(*ptr);
249 protected:
250 SVGPODList(const QualifiedName& attributeName)
251 : SVGList<RefPtr<SVGPODListItem<Item> > >(attributeName) { }
254 } // namespace WebCore
256 #endif // ENABLE(SVG)
257 #endif // SVGList_h