SVGPropertyTearOff.h [plain text]
#pragma once
#include "ExceptionOr.h"
#include "SVGAnimatedProperty.h"
#include "SVGProperty.h"
#include <wtf/WeakPtr.h>
namespace WebCore {
class SVGElement;
class SVGPropertyTearOffBase : public SVGProperty {
public:
virtual void detachWrapper() = 0;
};
template<typename T>
class SVGPropertyTearOff : public SVGPropertyTearOffBase {
public:
using PropertyType = T;
using Self = SVGPropertyTearOff<PropertyType>;
static Ref<Self> create(SVGAnimatedProperty& animatedProperty, SVGPropertyRole role, PropertyType& value)
{
return adoptRef(*new Self(animatedProperty, role, value));
}
static Ref<Self> create(const PropertyType& initialValue)
{
return adoptRef(*new Self(initialValue));
}
static Ref<Self> create(const PropertyType* initialValue)
{
return adoptRef(*new Self(initialValue));
}
template<typename U> static ExceptionOr<Ref<Self>> create(ExceptionOr<U>&& initialValue)
{
if (initialValue.hasException())
return initialValue.releaseException();
return create(initialValue.releaseReturnValue());
}
virtual PropertyType& propertyReference() { return *m_value; }
SVGAnimatedProperty* animatedProperty() const { return m_animatedProperty.get(); }
virtual void setValue(PropertyType& value)
{
if (m_valueIsCopy) {
detachChildren();
delete m_value;
}
m_valueIsCopy = false;
m_value = &value;
}
void setAnimatedProperty(SVGAnimatedProperty* animatedProperty)
{
m_animatedProperty = animatedProperty;
}
SVGElement* contextElement() const
{
if (!m_animatedProperty || m_valueIsCopy)
return nullptr;
return m_animatedProperty->contextElement();
}
void addChild(WeakPtr<SVGPropertyTearOffBase> child)
{
m_childTearOffs.append(child);
}
void detachWrapper() override
{
if (m_valueIsCopy)
return;
detachChildren();
m_value = new PropertyType(*m_value);
m_valueIsCopy = true;
m_animatedProperty = nullptr;
}
void commitChange() override
{
if (!m_animatedProperty || m_valueIsCopy)
return;
m_animatedProperty->commitChange();
}
bool isReadOnly() const override
{
if (m_role == AnimValRole)
return true;
if (m_animatedProperty && m_animatedProperty->isReadOnly())
return true;
return false;
}
WeakPtr<SVGPropertyTearOff> createWeakPtr() const
{
return m_weakPtrFactory.createWeakPtr(*const_cast<SVGPropertyTearOff*>(this));
}
protected:
SVGPropertyTearOff(SVGAnimatedProperty* animatedProperty, SVGPropertyRole role, PropertyType& value)
: m_animatedProperty(animatedProperty)
, m_role(role)
, m_value(&value)
, m_valueIsCopy(false)
{
}
SVGPropertyTearOff(const PropertyType& initialValue)
: SVGPropertyTearOff(&initialValue)
{
}
SVGPropertyTearOff(const PropertyType* initialValue)
: m_animatedProperty(nullptr)
, m_role(UndefinedRole)
, m_value(initialValue ? new PropertyType(*initialValue) : nullptr)
, m_valueIsCopy(true)
{
}
virtual ~SVGPropertyTearOff()
{
if (m_valueIsCopy) {
detachChildren();
delete m_value;
}
}
void detachChildren()
{
for (const auto& childTearOff : m_childTearOffs) {
if (childTearOff.get())
childTearOff.get()->detachWrapper();
}
m_childTearOffs.clear();
}
RefPtr<SVGAnimatedProperty> m_animatedProperty;
SVGPropertyRole m_role;
PropertyType* m_value;
Vector<WeakPtr<SVGPropertyTearOffBase>> m_childTearOffs;
WeakPtrFactory<SVGPropertyTearOff> m_weakPtrFactory;
bool m_valueIsCopy;
};
}