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"
|
|
using | Type = T |
| | Type of the property value.
|
|
| | 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.
|
| T | 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.
|
|
|
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 constexpr PropertyCascade | kCascadeMode = kCascade |
| | Cascade mode metadata.
|
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
-
| T | The type of the property value. |
| kCascade | Determines how this property type participates in the cascade, to allow for specific property types to be excluded from inheritance. |
◆ Property()
Property constructor, which is initially unset.
- Parameters
-
| name | Name of the property, such as "color". |
| getInitialFn | Function to get the initial value of the property. |
◆ get()
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()
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
-
| fallback | Value to return when the property does not resolve to a value. |
- Returns
- The resolved value, or
fallback.
◆ getStoredValue()
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()
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
-
| parent | Parent property to inherit into this one. |
| options | Options to control how inheritance is performed, to conditionally disable inheritance. |
- Returns
- Property with the resolved value after inheritance.
◆ isSpecified()
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]
Unset the current value and set the property to a specific state.
- Parameters
-
| newState | New state to set. |
| newSpecificity | Specificity to use. |
◆ set() [2/2]
Set the property to a new value at the given specificity.
- Parameters
-
| newValue | Value to set, or std::nullopt to set to an empty value. |
| newSpecificity | Specificity to use. |
◆ substitute()
Replace the current property's value with a new value at the current specificity.
- Parameters
-
| newValue | Value to use to replace the existing one. |
The documentation for this struct was generated from the following file: