#pragma once
#include "ExceptionOr.h"
#include "SVGProperty.h"
namespace WebCore {
template<typename ItemType>
class SVGList : public SVGProperty {
public:
unsigned length() const { return numberOfItems(); }
unsigned numberOfItems() const
{
return m_items.size();
}
ExceptionOr<void> clear()
{
auto result = canAlterList();
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
clearItems();
commitChange();
return { };
}
ExceptionOr<ItemType> getItem(unsigned index)
{
auto result = canGetItem(index);
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
return at(index);
}
ExceptionOr<ItemType> initialize(ItemType&& newItem)
{
auto result = canAlterList();
if (result.hasException())
return result.releaseException();
clearItems();
auto item = append(WTFMove(newItem));
commitChange();
return item;
}
ExceptionOr<ItemType> insertItemBefore(ItemType&& newItem, unsigned index)
{
auto result = canAlterList();
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
if (index > numberOfItems())
index = numberOfItems();
auto item = insert(index, WTFMove(newItem));
commitChange();
return item;
}
ExceptionOr<ItemType> replaceItem(ItemType&& newItem, unsigned index)
{
auto result = canReplaceItem(index);
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
auto item = replace(index, WTFMove(newItem));
commitChange();
return item;
}
ExceptionOr<ItemType> removeItem(unsigned index)
{
auto result = canRemoveItem(index);
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
auto item = remove(index);
commitChange();
return item;
}
ExceptionOr<ItemType> appendItem(ItemType&& newItem)
{
auto result = canAlterList();
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
auto item = append(WTFMove(newItem));
commitChange();
return item;
}
ExceptionOr<void> setItem(unsigned index, ItemType&& newItem)
{
auto result = replaceItem(WTFMove(newItem), index);
if (result.hasException())
return result.releaseException();
return { };
}
Vector<ItemType>& items() { return m_items; }
const Vector<ItemType>& items() const { return m_items; }
size_t size() const { return m_items.size(); }
bool isEmpty() const { return m_items.isEmpty(); }
void clearItems()
{
detachItems();
m_items.clear();
}
protected:
using SVGProperty::SVGProperty;
ExceptionOr<bool> canAlterList() const
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
return true;
}
ExceptionOr<bool> canGetItem(unsigned index)
{
if (index >= m_items.size())
return Exception { IndexSizeError };
return true;
}
ExceptionOr<bool> canReplaceItem(unsigned index)
{
auto result = canAlterList();
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
if (index >= m_items.size())
return Exception { IndexSizeError };
return true;
}
ExceptionOr<bool> canRemoveItem(unsigned index)
{
auto result = canAlterList();
if (result.hasException())
return result.releaseException();
ASSERT(result.releaseReturnValue());
if (index >= m_items.size())
return Exception { IndexSizeError };
return true;
}
virtual void detachItems() { }
virtual ItemType at(unsigned index) const = 0;
virtual ItemType insert(unsigned index, ItemType&&) = 0;
virtual ItemType replace(unsigned index, ItemType&&) = 0;
virtual ItemType remove(unsigned index) = 0;
virtual ItemType append(ItemType&&) = 0;
Vector<ItemType> m_items;
};
}