KHTML
SVGList.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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 m_vector.insert(index, newItem);
00101 return newItem;
00102 }
00103
00104 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec)
00105 {
00106 if (index >= m_vector.size()) {
00107 ec = DOMException::INDEX_SIZE_ERR;
00108 return TypeOperations::nullItem();
00109 }
00110
00111 m_vector[index] = newItem;
00112 return newItem;
00113 }
00114
00115 Item removeItem(unsigned int index, ExceptionCode& ec)
00116 {
00117 if (index >= m_vector.size()) {
00118 ec = DOMException::INDEX_SIZE_ERR;
00119 return TypeOperations::nullItem();
00120 }
00121
00122 Item item = m_vector[index];
00123 m_vector.remove(index);
00124 return item;
00125 }
00126
00127 Item appendItem(Item newItem, ExceptionCode&)
00128 {
00129 m_vector.append(newItem);
00130 return newItem;
00131 }
00132
00133 protected:
00134 SVGList(const QualifiedName& attributeName)
00135 : m_associatedAttributeName(attributeName)
00136 {
00137 }
00138
00139 private:
00140 Vector<Item> m_vector;
00141 const QualifiedName& m_associatedAttributeName;
00142 };
00143
00144 template<typename Item>
00145 class SVGPODListItem : public RefCounted<SVGPODListItem<Item> > {
00146 public:
00147 static PassRefPtr<SVGPODListItem> create() { return adoptRef(new SVGPODListItem); }
00148 static PassRefPtr<SVGPODListItem> copy(const Item& item) { return adoptRef(new SVGPODListItem(item)); }
00149
00150 operator Item&() { return m_item; }
00151 operator const Item&() const { return m_item; }
00152
00153
00154 Item value() const { return m_item; }
00155 void setValue(Item newItem) { m_item = newItem; }
00156
00157 private:
00158 SVGPODListItem() : m_item() { }
00159 SVGPODListItem(const Item& item) : RefCounted<SVGPODListItem<Item> >(), m_item(item) { }
00160
00161 Item m_item;
00162 };
00163
00164 template<typename Item>
00165 class SVGPODList : public SVGList<RefPtr<SVGPODListItem<Item> > >
00166 {
00167 public:
00168 Item initialize(Item newItem, ExceptionCode& ec)
00169 {
00170 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::initialize(SVGPODListItem<Item>::copy(newItem), ec).get());
00171 if (!ptr)
00172 return Item();
00173
00174 return static_cast<const Item&>(*ptr);
00175 }
00176
00177 Item getFirst() const
00178 {
00179 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getFirst().get());
00180 if (!ptr)
00181 return Item();
00182
00183 return static_cast<const Item&>(*ptr);
00184 }
00185
00186 Item getLast() const
00187 {
00188 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getLast().get());
00189 if (!ptr)
00190 return Item();
00191
00192 return static_cast<const Item&>(*ptr);
00193 }
00194
00195 Item getItem(unsigned int index, ExceptionCode& ec)
00196 {
00197 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get());
00198 if (!ptr)
00199 return Item();
00200
00201 return static_cast<const Item&>(*ptr);
00202 }
00203
00204 const Item getItem(unsigned int index, ExceptionCode& ec) const
00205 {
00206 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::getItem(index, ec).get());
00207 if (!ptr)
00208 return Item();
00209
00210 return static_cast<const Item&>(*ptr);
00211 }
00212
00213 Item insertItemBefore(Item newItem, unsigned int index, ExceptionCode& ec)
00214 {
00215 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::insertItemBefore(SVGPODListItem<Item>::copy(newItem), index, ec).get());
00216 if (!ptr)
00217 return Item();
00218
00219 return static_cast<const Item&>(*ptr);
00220 }
00221
00222 Item replaceItem(Item newItem, unsigned int index, ExceptionCode& ec)
00223 {
00224 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::replaceItem(SVGPODListItem<Item>::copy(newItem), index, ec).get());
00225 if (!ptr)
00226 return Item();
00227
00228 return static_cast<const Item&>(*ptr);
00229 }
00230
00231 Item removeItem(unsigned int index, ExceptionCode& ec)
00232 {
00233 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::removeItem(index, ec).get());
00234 if (!ptr)
00235 return Item();
00236
00237 return static_cast<const Item&>(*ptr);
00238 }
00239
00240 Item appendItem(Item newItem, ExceptionCode& ec)
00241 {
00242 SVGPODListItem<Item>* ptr(SVGList<RefPtr<SVGPODListItem<Item> > >::appendItem(SVGPODListItem<Item>::copy(newItem), ec).get());
00243 if (!ptr)
00244 return Item();
00245
00246 return static_cast<const Item&>(*ptr);
00247 }
00248
00249 protected:
00250 SVGPODList(const QualifiedName& attributeName)
00251 : SVGList<RefPtr<SVGPODListItem<Item> > >(attributeName) { }
00252 };
00253
00254 }
00255
00256 #endif // ENABLE(SVG)
00257 #endif // SVGList_h