1 /* 2 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> 3 2004, 2005 Rob Buis <buis@kde.org> 4 5 This file is part of the KDE project 6 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. 11 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. 16 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. 21 */ 22 23 #ifndef SVGList_h 24 #define SVGList_h 25 26 #if ENABLE(SVG) 27 #include "ExceptionCode.h" 28 #include "SVGListTraits.h" 29 30 #include <wtf/RefCounted.h> 31 #include <wtf/PassRefPtr.h> 32 #include <wtf/Vector.h> 33 34 namespace WebCore { 35 36 class QualifiedName; 37 38 template<typename Item> 39 struct SVGListTypeOperations { nullItemSVGListTypeOperations40 static Item nullItem() 41 { 42 return SVGListTraits<UsesDefaultInitializer<Item>::value, Item>::nullItem(); 43 } 44 }; 45 46 template<typename Item> 47 class SVGList : public RefCounted<SVGList<Item> > { 48 private: 49 typedef SVGListTypeOperations<Item> TypeOperations; 50 51 public: ~SVGList()52 virtual ~SVGList() { } 53 associatedAttributeName()54 const QualifiedName& associatedAttributeName() const { return m_associatedAttributeName; } 55 numberOfItems()56 unsigned int numberOfItems() const { return m_vector.size(); } clear(ExceptionCode &)57 void clear(ExceptionCode &) { m_vector.clear(); } 58 initialize(Item newItem,ExceptionCode & ec)59 Item initialize(Item newItem, ExceptionCode& ec) 60 { 61 clear(ec); 62 return appendItem(newItem, ec); 63 } 64 getFirst()65 Item getFirst() const 66 { 67 ExceptionCode ec = 0; 68 return getItem(0, ec); 69 } 70 getLast()71 Item getLast() const 72 { 73 ExceptionCode ec = 0; 74 return getItem(m_vector.size() - 1, ec); 75 } 76 getItem(unsigned int index,ExceptionCode & ec)77 Item getItem(unsigned int index, ExceptionCode& ec) 78 { 79 if (index >= m_vector.size()) { 80 ec = INDEX_SIZE_ERR; 81 return TypeOperations::nullItem(); 82 } 83 84 return m_vector.at(index); 85 } 86 getItem(unsigned int index,ExceptionCode & ec)87 const Item getItem(unsigned int index, ExceptionCode& ec) const 88 { 89 if (index >= m_vector.size()) { 90 ec = INDEX_SIZE_ERR; 91 return TypeOperations::nullItem(); 92 } 93 94 return m_vector[index]; 95 } 96 insertItemBefore(Item newItem,unsigned int index,ExceptionCode &)97 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode&) 98 { 99 if (index < m_vector.size()) { 100 m_vector.insert(index, newItem); 101 } else { 102 m_vector.append(newItem); 103 } 104 return newItem; 105 } 106 replaceItem(Item newItem,unsigned int index,ExceptionCode & ec)107 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec) 108 { 109 if (index >= m_vector.size()) { 110 ec = INDEX_SIZE_ERR; 111 return TypeOperations::nullItem(); 112 } 113 114 m_vector[index] = newItem; 115 return newItem; 116 } 117 removeItem(unsigned int index,ExceptionCode & ec)118 Item removeItem(unsigned int index, ExceptionCode& ec) 119 { 120 if (index >= m_vector.size()) { 121 ec = INDEX_SIZE_ERR; 122 return TypeOperations::nullItem(); 123 } 124 125 Item item = m_vector[index]; 126 m_vector.remove(index); 127 return item; 128 } 129 appendItem(Item newItem,ExceptionCode &)130 Item appendItem(Item newItem, ExceptionCode&) 131 { 132 m_vector.append(newItem); 133 return newItem; 134 } 135 136 protected: SVGList(const QualifiedName & attributeName)137 SVGList(const QualifiedName& attributeName) 138 : m_associatedAttributeName(attributeName) 139 { 140 } 141 142 private: 143 Vector<Item> m_vector; 144 const QualifiedName& m_associatedAttributeName; 145 }; 146 147 template<typename Item> 148 class SVGPODListItem : public RefCounted<SVGPODListItem<Item> > { 149 public: create()150 static PassRefPtr<SVGPODListItem> create() { return adoptRef(new SVGPODListItem); } copy(const Item & item)151 static PassRefPtr<SVGPODListItem> copy(const Item& item) { return adoptRef(new SVGPODListItem(item)); } 152 153 operator Item&() { return m_item; } 154 operator const Item&() const { return m_item; } 155 156 // Updating facilities, used by JSSVGPODTypeWrapperCreatorForList value()157 Item value() const { return m_item; } setValue(Item newItem)158 void setValue(Item newItem) { m_item = newItem; } 159 160 private: SVGPODListItem()161 SVGPODListItem() : m_item() { } SVGPODListItem(const Item & item)162 SVGPODListItem(const Item& item) : RefCounted<SVGPODListItem<Item> >(), m_item(item) { } 163 164 Item m_item; 165 }; 166 167 template<typename Item> 168 class SVGPODList : public SVGList<RefPtr<SVGPODListItem<Item> > > { 169 public: initialize(Item newItem,ExceptionCode & ec)170 Item initialize(Item newItem, ExceptionCode& ec) 171 { 172 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::initialize(SVGPODListItem<Item>::copy(newItem), ec).get()); 173 if (!ptr) 174 return Item(); 175 176 return static_cast<const Item&>(*ptr); 177 } 178 getFirst()179 Item getFirst() const 180 { 181 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getFirst().get()); 182 if (!ptr) 183 return Item(); 184 185 return static_cast<const Item&>(*ptr); 186 } 187 getLast()188 Item getLast() const 189 { 190 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getLast().get()); 191 if (!ptr) 192 return Item(); 193 194 return static_cast<const Item&>(*ptr); 195 } 196 getItem(unsigned int index,ExceptionCode & ec)197 Item getItem(unsigned int index, ExceptionCode& ec) 198 { 199 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get()); 200 if (!ptr) 201 return Item(); 202 203 return static_cast<const Item&>(*ptr); 204 } 205 getItem(unsigned int index,ExceptionCode & ec)206 const Item getItem(unsigned int index, ExceptionCode& ec) const 207 { 208 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get()); 209 if (!ptr) 210 return Item(); 211 212 return static_cast<const Item&>(*ptr); 213 } 214 insertItemBefore(Item newItem,unsigned int index,ExceptionCode & ec)215 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode& ec) 216 { 217 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::insertItemBefore(SVGPODListItem<Item>::copy(newItem), index, ec).get()); 218 if (!ptr) 219 return Item(); 220 221 return static_cast<const Item&>(*ptr); 222 } 223 replaceItem(Item newItem,unsigned int index,ExceptionCode & ec)224 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec) 225 { 226 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::replaceItem(SVGPODListItem<Item>::copy(newItem), index, ec).get()); 227 if (!ptr) 228 return Item(); 229 230 return static_cast<const Item&>(*ptr); 231 } 232 removeItem(unsigned int index,ExceptionCode & ec)233 Item removeItem(unsigned int index, ExceptionCode& ec) 234 { 235 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::removeItem(index, ec).get()); 236 if (!ptr) 237 return Item(); 238 239 return static_cast<const Item&>(*ptr); 240 } 241 appendItem(Item newItem,ExceptionCode & ec)242 Item appendItem(Item newItem, ExceptionCode& ec) 243 { 244 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::appendItem(SVGPODListItem<Item>::copy(newItem), ec).get()); 245 if (!ptr) 246 return Item(); 247 248 return static_cast<const Item&>(*ptr); 249 } 250 251 protected: SVGPODList(const QualifiedName & attributeName)252 SVGPODList(const QualifiedName& attributeName) 253 : SVGList<RefPtr<SVGPODListItem<Item> > >(attributeName) { } 254 }; 255 256 } // namespace WebCore 257 258 #endif // ENABLE(SVG) 259 #endif // SVGList_h 260