Donner 0.8.0-pre
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::svg::Property< T, kCascade > Struct Template Reference

Holds a CSS property, which has a name and value, and integrates with inheritance to allow cascading values using the CSS model with specificity. More...

#include "donner/svg/properties/Property.h"

Collaboration diagram for donner::svg::Property< T, kCascade >:
[legend]

Public Types

using Type = T
 Type of the property value.

Public Member Functions

 Property (std::string_view name, GetInitialFn< T > getInitialFn=[]() -> std::optional< T > { return std::nullopt;})
 Property constructor, which is initially unset.
std::optional< T > get () const
 Get the property value, without considering inheritance.
getOr (const T &fallback) const
 Gets the resolved value, or fallback if the property does not resolve to a concrete value (e.g.
const T * getStoredValue () const
 Returns a non-owning pointer to the stored value when the property is explicitly PropertyState::Set, for no-copy access to complex values; returns nullptr otherwise (including the inherit/initial/unset states, which carry no stored value).
void set (std::optional< T > newValue, css::Specificity newSpecificity)
 Set the property to a new value at the given specificity.
void set (PropertyState newState, css::Specificity newSpecificity)
 Unset the current value and set the property to a specific state.
void substitute (std::optional< T > newValue)
 Replace the current property's value with a new value at the current specificity.
void clear ()
 Clear the current property's value.
Property< T, kCascade > inheritFrom (const Property< T, kCascade > &parent, PropertyInheritOptions options=PropertyInheritOptions::All) const
 Inherit the property from the parent element, if the parent has the property set at a higher specificity.
bool isSpecified () const
 Whether the cascade assigned this property any state other than PropertyState::NotSet — i.e.

Public Attributes

std::string_view name
 Property name, such as "color".
std::optional< T > value
 Property value, or std::nullopt if not set.
PropertyState state = PropertyState::NotSet
 Current state of the property, such as set or inherited.
css::Specificity specificity
 Specificity of the property, used for inheritance.
GetInitialFn< T > getInitialFn
 Function which is called to get the initial value of the property.

Static Public Attributes

static constexpr PropertyCascade kCascadeMode = kCascade
 Cascade mode metadata.

Detailed Description

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
struct donner::svg::Property< T, kCascade >

Holds a CSS property, which has a name and value, and integrates with inheritance to allow cascading values using the CSS model with specificity.

Template Parameters
TThe type of the property value.
kCascadeDetermines how this property type participates in the cascade, to allow for specific property types to be excluded from inheritance.

Constructor & Destructor Documentation

◆ Property()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
donner::svg::Property< T, kCascade >::Property ( std::string_view name,
GetInitialFn< T > getInitialFn = []() -> std::optional<T> { return std::nullopt; } )
inline

Property constructor, which is initially unset.

Parameters
nameName of the property, such as "color".
getInitialFnFunction to get the initial value of the property.

Member Function Documentation

◆ get()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
std::optional< T > donner::svg::Property< T, kCascade >::get ( ) const
inline

Get the property value, without considering inheritance.

Returns the initial value if the property has not been set.

Returns
The value if it is set, or the initial value if it is not. Returns std::nullopt if the property is none.

◆ getOr()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
T donner::svg::Property< T, kCascade >::getOr ( const T & fallback) const
inline

Gets the resolved value, or fallback if the property does not resolve to a concrete value (e.g.

it is unset with no initial, or its keyword is inherit/initial/unset with no resolved value). Never aborts.

This is the footgun-free replacement for the former getRequired(): prefer it, or get() and handle std::nullopt, rather than asserting a value is present. For the rare case where an invariant genuinely guarantees presence, get().value() makes the abort explicit and local.

Parameters
fallbackValue to return when the property does not resolve to a value.
Returns
The resolved value, or fallback.

◆ getStoredValue()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
const T * donner::svg::Property< T, kCascade >::getStoredValue ( ) const
inline

Returns a non-owning pointer to the stored value when the property is explicitly PropertyState::Set, for no-copy access to complex values; returns nullptr otherwise (including the inherit/initial/unset states, which carry no stored value).

Footgun-free replacement for the former getRequiredRef(): callers null-check the result instead of relying on an assert. Note this never returns the initial value — use get() if you need initial-value resolution.

Returns
Pointer to the stored value, or nullptr.

◆ inheritFrom()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
Property< T, kCascade > donner::svg::Property< T, kCascade >::inheritFrom ( const Property< T, kCascade > & parent,
PropertyInheritOptions options = PropertyInheritOptions::All ) const
inlinenodiscard

Inherit the property from the parent element, if the parent has the property set at a higher specificity.

Note that this typically inherits "backwards", taking a local property which may already have a value and then overriding it if the parent has a more specific one. This is not required, but doing so is more efficient since we don't need to keep setting the property as the child overrides each parent.

Parameters
parentParent property to inherit into this one.
optionsOptions to control how inheritance is performed, to conditionally disable inheritance.
Returns
Property with the resolved value after inheritance.

◆ isSpecified()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
bool donner::svg::Property< T, kCascade >::isSpecified ( ) const
inline

Whether the cascade assigned this property any state other than PropertyState::NotSet — i.e.

a concrete value OR a CSS-wide keyword (inherit/initial/unset) was specified.

This is a cascade predicate, NOT a guarantee that get() returns a value: for inherit/initial/unset (and Set to none) this is true while get() may be std::nullopt. Do not use it as a guard before reading the value — use get(), getOr(), or getStoredValue() for that.

Returns
true if any cascade state other than NotSet was specified.

◆ set() [1/2]

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
void donner::svg::Property< T, kCascade >::set ( PropertyState newState,
css::Specificity newSpecificity )
inline

Unset the current value and set the property to a specific state.

Parameters
newStateNew state to set.
newSpecificitySpecificity to use.

◆ set() [2/2]

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
void donner::svg::Property< T, kCascade >::set ( std::optional< T > newValue,
css::Specificity newSpecificity )
inline

Set the property to a new value at the given specificity.

Parameters
newValueValue to set, or std::nullopt to set to an empty value.
newSpecificitySpecificity to use.

◆ substitute()

template<typename T, PropertyCascade kCascade = PropertyCascade::None>
void donner::svg::Property< T, kCascade >::substitute ( std::optional< T > newValue)
inline

Replace the current property's value with a new value at the current specificity.

Parameters
newValueValue to use to replace the existing one.

The documentation for this struct was generated from the following file: