SVGListPropertyTearOff.h [plain text]
#ifndef SVGListPropertyTearOff_h
#define SVGListPropertyTearOff_h
#include "SVGListProperty.h"
namespace WebCore {
template<typename PropertyType>
class SVGListPropertyTearOff : public SVGListProperty<PropertyType> {
public:
typedef SVGListProperty<PropertyType> Base;
typedef SVGListPropertyTearOff<PropertyType> Self;
typedef typename SVGPropertyTraits<PropertyType>::ListItemType ListItemType;
typedef SVGPropertyTearOff<ListItemType> ListItemTearOff;
typedef RefPtr<ListItemTearOff> PtrListItemTearOff;
typedef SVGAnimatedListPropertyTearOff<PropertyType> AnimatedListPropertyTearOff;
typedef typename SVGAnimatedListPropertyTearOff<PropertyType>::ListWrapperCache ListWrapperCache;
using Base::m_role;
using Base::m_values;
using Base::m_wrappers;
static Ref<Self> create(AnimatedListPropertyTearOff* animatedProperty, SVGPropertyRole role, PropertyType& values, ListWrapperCache& wrappers)
{
ASSERT(animatedProperty);
return adoptRef(*new Self(animatedProperty, role, values, wrappers));
}
int findItem(ListItemTearOff* item) const
{
ASSERT(m_values);
ASSERT(m_wrappers);
unsigned size = m_wrappers->size();
ASSERT(size == m_values->size());
for (size_t i = 0; i < size; ++i) {
if (item == m_wrappers->at(i))
return i;
}
return -1;
}
void removeItemFromList(size_t itemIndex, bool shouldSynchronizeWrappers)
{
ASSERT(m_values);
ASSERT(m_wrappers);
ASSERT(m_values->size() == m_wrappers->size());
ASSERT_WITH_SECURITY_IMPLICATION(itemIndex < m_wrappers->size());
RefPtr<ListItemTearOff>& item = m_wrappers->at(itemIndex);
item->detachWrapper();
m_wrappers->remove(itemIndex);
m_values->remove(itemIndex);
if (shouldSynchronizeWrappers)
commitChange();
}
void clear(ExceptionCode& ec)
{
Base::clearValuesAndWrappers(ec);
}
PtrListItemTearOff initialize(PtrListItemTearOff newItem, ExceptionCode& ec)
{
return Base::initializeValuesAndWrappers(newItem, ec);
}
PtrListItemTearOff getItem(unsigned index, ExceptionCode& ec)
{
return Base::getItemValuesAndWrappers(m_animatedProperty.get(), index, ec);
}
PtrListItemTearOff insertItemBefore(PtrListItemTearOff newItem, unsigned index, ExceptionCode& ec)
{
return Base::insertItemBeforeValuesAndWrappers(newItem, index, ec);
}
PtrListItemTearOff replaceItem(PtrListItemTearOff newItem, unsigned index, ExceptionCode& ec)
{
return Base::replaceItemValuesAndWrappers(newItem, index, ec);
}
PtrListItemTearOff removeItem(unsigned index, ExceptionCode& ec)
{
return Base::removeItemValuesAndWrappers(m_animatedProperty.get(), index, ec);
}
PtrListItemTearOff appendItem(PtrListItemTearOff newItem, ExceptionCode& ec)
{
return Base::appendItemValuesAndWrappers(newItem, ec);
}
protected:
SVGListPropertyTearOff(AnimatedListPropertyTearOff* animatedProperty, SVGPropertyRole role, PropertyType& values, ListWrapperCache& wrappers)
: SVGListProperty<PropertyType>(role, values, &wrappers)
, m_animatedProperty(animatedProperty)
{
}
virtual ~SVGListPropertyTearOff()
{
if (m_animatedProperty)
m_animatedProperty->propertyWillBeDeleted(*this);
}
virtual bool isReadOnly() const override
{
if (m_role == AnimValRole)
return true;
if (m_animatedProperty && m_animatedProperty->isReadOnly())
return true;
return false;
}
virtual void commitChange() override
{
ASSERT(m_values);
ASSERT(m_wrappers);
unsigned size = m_wrappers->size();
ASSERT(size == m_values->size());
for (unsigned i = 0; i < size; ++i) {
ListItemTearOff* item = m_wrappers->at(i).get();
if (!item)
continue;
item->setAnimatedProperty(m_animatedProperty.get());
item->setValue(m_values->at(i));
}
m_animatedProperty->commitChange();
}
virtual bool processIncomingListItemValue(const ListItemType&, unsigned*) override
{
ASSERT_NOT_REACHED();
return true;
}
virtual bool processIncomingListItemWrapper(RefPtr<ListItemTearOff>& newItem, unsigned* indexToModify) override
{
SVGAnimatedProperty* animatedPropertyOfItem = newItem->animatedProperty();
if (!animatedPropertyOfItem)
return true;
if (!animatedPropertyOfItem->isAnimatedListTearOff()) {
newItem = ListItemTearOff::create(newItem->propertyReference());
return true;
}
bool livesInOtherList = animatedPropertyOfItem != m_animatedProperty;
AnimatedListPropertyTearOff* propertyTearOff = static_cast<AnimatedListPropertyTearOff*>(animatedPropertyOfItem);
int indexToRemove = propertyTearOff->findItem(newItem.get());
ASSERT(indexToRemove != -1);
if (!livesInOtherList && indexToModify && static_cast<unsigned>(indexToRemove) == *indexToModify)
return false;
propertyTearOff->removeItemFromList(indexToRemove, true);
if (!indexToModify)
return true;
if (!livesInOtherList) {
unsigned& index = *indexToModify;
if (static_cast<unsigned>(indexToRemove) < index)
--index;
}
return true;
}
RefPtr<AnimatedListPropertyTearOff> m_animatedProperty;
};
}
#endif // SVGListPropertyTearOff_h