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.
27 #include "ExceptionCode.h"
28 #include "SVGListTraits.h"
31 #include <wtf/RefCounted.h>
32 #include <wtf/PassRefPtr.h>
33 #include <wtf/Vector.h>
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
> > {
50 typedef SVGListTypeOperations
<Item
> TypeOperations
;
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
)
63 return appendItem(newItem
, ec
);
69 return getItem(0, ec
);
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
);
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
;
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
);
127 Item
appendItem(Item newItem
, ExceptionCode
&)
129 m_vector
.append(newItem
);
134 SVGList(const QualifiedName
& attributeName
)
135 : m_associatedAttributeName(attributeName
)
140 Vector
<Item
> m_vector
;
141 const QualifiedName
& m_associatedAttributeName
;
144 template<typename Item
>
145 class SVGPODListItem
: public RefCounted
<SVGPODListItem
<Item
> > {
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
; }
158 SVGPODListItem() : m_item() { }
159 SVGPODListItem(const Item
& item
) : RefCounted
<SVGPODListItem
<Item
> >(), m_item(item
) { }
164 template<typename Item
>
165 class SVGPODList
: public SVGList
<RefPtr
<SVGPODListItem
<Item
> > >
168 Item
initialize(Item newItem
, ExceptionCode
& ec
)
170 SVGPODListItem
<Item
>* ptr(SVGList
<RefPtr
<SVGPODListItem
<Item
> > >::initialize(SVGPODListItem
<Item
>::copy(newItem
), ec
).get());
174 return static_cast<const Item
&>(*ptr
);
177 Item
getFirst() const
179 SVGPODListItem
<Item
>* ptr(SVGList
<RefPtr
<SVGPODListItem
<Item
> > >::getFirst().get());
183 return static_cast<const Item
&>(*ptr
);
188 SVGPODListItem
<Item
>* ptr(SVGList
<RefPtr
<SVGPODListItem
<Item
> > >::getLast().get());
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());
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());
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());
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());
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());
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());
246 return static_cast<const Item
&>(*ptr
);
250 SVGPODList(const QualifiedName
& attributeName
)
251 : SVGList
<RefPtr
<SVGPODListItem
<Item
> > >(attributeName
) { }
254 } // namespace WebCore
256 #endif // ENABLE(SVG)