KHTML
SVGList.h
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> 00003 2004, 2005 Rob Buis <buis@kde.org> 00004 00005 This file is part of the KDE project 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #ifndef SVGList_h 00024 #define SVGList_h 00025 00026 #if ENABLE(SVG) 00027 #include "ExceptionCode.h" 00028 #include "SVGListTraits.h" 00029 #include "Document.h" 00030 00031 #include <wtf/RefCounted.h> 00032 #include <wtf/PassRefPtr.h> 00033 #include <wtf/Vector.h> 00034 00035 namespace WebCore { 00036 00037 //class QualifiedName; 00038 00039 template<typename Item> 00040 struct SVGListTypeOperations { 00041 static Item nullItem() 00042 { 00043 return SVGListTraits<UsesDefaultInitializer<Item>::value, Item>::nullItem(); 00044 } 00045 }; 00046 00047 template<typename Item> 00048 class SVGList : public RefCounted<SVGList<Item> > { 00049 private: 00050 typedef SVGListTypeOperations<Item> TypeOperations; 00051 00052 public: 00053 virtual ~SVGList() { } 00054 00055 const QualifiedName& associatedAttributeName() const { return m_associatedAttributeName; } 00056 00057 unsigned int numberOfItems() const { return m_vector.size(); } 00058 void clear(ExceptionCode &) { m_vector.clear(); } 00059 00060 Item initialize(Item newItem, ExceptionCode& ec) 00061 { 00062 clear(ec); 00063 return appendItem(newItem, ec); 00064 } 00065 00066 Item getFirst() const 00067 { 00068 ExceptionCode ec = 0; 00069 return getItem(0, ec); 00070 } 00071 00072 Item getLast() const 00073 { 00074 ExceptionCode ec = 0; 00075 return getItem(m_vector.size() - 1, ec); 00076 } 00077 00078 Item getItem(unsigned int index, ExceptionCode& ec) 00079 { 00080 if (index >= m_vector.size()) { 00081 ec = DOMException::INDEX_SIZE_ERR; 00082 return TypeOperations::nullItem(); 00083 } 00084 00085 return m_vector.at(index); 00086 } 00087 00088 const Item getItem(unsigned int index, ExceptionCode& ec) const 00089 { 00090 if (index >= m_vector.size()) { 00091 ec = DOMException::INDEX_SIZE_ERR; 00092 return TypeOperations::nullItem(); 00093 } 00094 00095 return m_vector[index]; 00096 } 00097 00098 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode&) 00099 { 00100 if (index < m_vector.size()) { 00101 m_vector.insert(index, newItem); 00102 } else { 00103 m_vector.append(newItem); 00104 } 00105 return newItem; 00106 } 00107 00108 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec) 00109 { 00110 if (index >= m_vector.size()) { 00111 ec = DOMException::INDEX_SIZE_ERR; 00112 return TypeOperations::nullItem(); 00113 } 00114 00115 m_vector[index] = newItem; 00116 return newItem; 00117 } 00118 00119 Item removeItem(unsigned int index, ExceptionCode& ec) 00120 { 00121 if (index >= m_vector.size()) { 00122 ec = DOMException::INDEX_SIZE_ERR; 00123 return TypeOperations::nullItem(); 00124 } 00125 00126 Item item = m_vector[index]; 00127 m_vector.remove(index); 00128 return item; 00129 } 00130 00131 Item appendItem(Item newItem, ExceptionCode&) 00132 { 00133 m_vector.append(newItem); 00134 return newItem; 00135 } 00136 00137 protected: 00138 SVGList(const QualifiedName& attributeName) 00139 : m_associatedAttributeName(attributeName) 00140 { 00141 } 00142 00143 private: 00144 Vector<Item> m_vector; 00145 const QualifiedName& m_associatedAttributeName; 00146 }; 00147 00148 template<typename Item> 00149 class SVGPODListItem : public RefCounted<SVGPODListItem<Item> > { 00150 public: 00151 static PassRefPtr<SVGPODListItem> create() { return adoptRef(new SVGPODListItem); } 00152 static PassRefPtr<SVGPODListItem> copy(const Item& item) { return adoptRef(new SVGPODListItem(item)); } 00153 00154 operator Item&() { return m_item; } 00155 operator const Item&() const { return m_item; } 00156 00157 // Updating facilities, used by JSSVGPODTypeWrapperCreatorForList 00158 Item value() const { return m_item; } 00159 void setValue(Item newItem) { m_item = newItem; } 00160 00161 private: 00162 SVGPODListItem() : m_item() { } 00163 SVGPODListItem(const Item& item) : RefCounted<SVGPODListItem<Item> >(), m_item(item) { } 00164 00165 Item m_item; 00166 }; 00167 00168 template<typename Item> 00169 class SVGPODList : public SVGList<RefPtr<SVGPODListItem<Item> > > 00170 { 00171 public: 00172 Item initialize(Item newItem, ExceptionCode& ec) 00173 { 00174 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::initialize(SVGPODListItem<Item>::copy(newItem), ec).get()); 00175 if (!ptr) 00176 return Item(); 00177 00178 return static_cast<const Item&>(*ptr); 00179 } 00180 00181 Item getFirst() const 00182 { 00183 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getFirst().get()); 00184 if (!ptr) 00185 return Item(); 00186 00187 return static_cast<const Item&>(*ptr); 00188 } 00189 00190 Item getLast() const 00191 { 00192 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getLast().get()); 00193 if (!ptr) 00194 return Item(); 00195 00196 return static_cast<const Item&>(*ptr); 00197 } 00198 00199 Item getItem(unsigned int index, ExceptionCode& ec) 00200 { 00201 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get()); 00202 if (!ptr) 00203 return Item(); 00204 00205 return static_cast<const Item&>(*ptr); 00206 } 00207 00208 const Item getItem(unsigned int index, ExceptionCode& ec) const 00209 { 00210 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get()); 00211 if (!ptr) 00212 return Item(); 00213 00214 return static_cast<const Item&>(*ptr); 00215 } 00216 00217 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode& ec) 00218 { 00219 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::insertItemBefore(SVGPODListItem<Item>::copy(newItem), index, ec).get()); 00220 if (!ptr) 00221 return Item(); 00222 00223 return static_cast<const Item&>(*ptr); 00224 } 00225 00226 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec) 00227 { 00228 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::replaceItem(SVGPODListItem<Item>::copy(newItem), index, ec).get()); 00229 if (!ptr) 00230 return Item(); 00231 00232 return static_cast<const Item&>(*ptr); 00233 } 00234 00235 Item removeItem(unsigned int index, ExceptionCode& ec) 00236 { 00237 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::removeItem(index, ec).get()); 00238 if (!ptr) 00239 return Item(); 00240 00241 return static_cast<const Item&>(*ptr); 00242 } 00243 00244 Item appendItem(Item newItem, ExceptionCode& ec) 00245 { 00246 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::appendItem(SVGPODListItem<Item>::copy(newItem), ec).get()); 00247 if (!ptr) 00248 return Item(); 00249 00250 return static_cast<const Item&>(*ptr); 00251 } 00252 00253 protected: 00254 SVGPODList(const QualifiedName& attributeName) 00255 : SVGList<RefPtr<SVGPODListItem<Item> > >(attributeName) { } 00256 }; 00257 00258 } // namespace WebCore 00259 00260 #endif // ENABLE(SVG) 00261 #endif // SVGList_h
KDE 4.6 API Reference