Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::css::ComponentValue Struct Reference

A CSS component value, which is either a token, or a parsed function or block. More...

#include "donner/css/ComponentValue.h"

Public Types

using Type = std::variant<Token, Function, SimpleBlock>
 The type of the component value, which is either a base Token or a logical group of tokens, Function or SimpleBlock.
 

Public Member Functions

 ComponentValue (Type &&value)
 Construct a new ComponentValue object, taking ownership of a Token.
 
 ~ComponentValue ()
 Destructor.
 
 ComponentValue (const ComponentValue &)=default
 Copy constructor.
 
ComponentValueoperator= (const ComponentValue &)=default
 Move constructor.
 
 ComponentValue (ComponentValue &&) noexcept=default
 Move assignment operator.
 
ComponentValueoperator= (ComponentValue &&) noexcept=default
 Copy assignment operator.
 
bool operator== (const ComponentValue &other) const
 Equality operator.
 
template<typename T >
bool is () const
 Check if the component value is of a given type.
 
template<typename T >
bool isToken () const
 Shorthand for checking if this component value holds a specific token.
 
template<typename T >
const T * tryGetToken () const
 Get the inner token value as a pointer, if the component value is a token and matches the requested type, or nullptr.
 
template<typename T >
T & get () &
 Get the component value as a reference.
 
template<typename T >
const T & get () const &
 Get the component value as a const-reference.
 
template<typename T >
T && get () &&
 Get the component value as an rvalue-reference for move semantics.
 
parser::FileOffset sourceOffset () const
 Get the offset of this component value in the original source.
 

Public Attributes

Type value
 The actual value of the component value.
 

Friends

std::ostream & operator<< (std::ostream &os, const ComponentValue &component)
 Output a human-readable representation of the component value to a stream.
 

Detailed Description

A CSS component value, which is either a token, or a parsed function or block.

This is the second level of parsing, after Token. A Token is a single lexical unit, and ComponentValue groups those into logical function and block groups, as well as wrapping standalone Token.

ComponentValue is the base component traversed when parsing CSS into logical blocks, such as Selector and donner::svg::Property.

Constructor & Destructor Documentation

◆ ComponentValue()

donner::css::ComponentValue::ComponentValue ( ComponentValue::Type && value)

Construct a new ComponentValue object, taking ownership of a Token.

Parameters
valueToken to construct from.

Member Function Documentation

◆ get() [1/3]

template<typename T >
T & donner::css::ComponentValue::get ( ) &
inline

Get the component value as a reference.

Example usage:

SimpleBlock& block = component.get<SimpleBlock>();
block.values.push_back(componentValue);
A CSS simple block, such as a rule block or a parenthesized expression.
Definition ComponentValue.h:62
std::vector< ComponentValue > values
List of component values inside the simple block.
Definition ComponentValue.h:70
Template Parameters
TType to get, one of Type.
Precondition
The component must be of the given type, i.e. is<T>() must be true.

◆ get() [2/3]

template<typename T >
T && donner::css::ComponentValue::get ( ) &&
inline

Get the component value as an rvalue-reference for move semantics.

Example usage:

SimpleBlock block = std::move(component.get<SimpleBlock>());
Template Parameters
TType to get, one of Type.
Precondition
The component must be of the given type, i.e. is<T>() must be true.

◆ get() [3/3]

template<typename T >
const T & donner::css::ComponentValue::get ( ) const &
inline

Get the component value as a const-reference.

Example usage:

const SimpleBlock& block = component.get<SimpleBlock>();
Template Parameters
TType to get, one of Type.
Precondition
The component must be of the given type, i.e. is<T>() must be true.

◆ is()

template<typename T >
bool donner::css::ComponentValue::is ( ) const
inline

Check if the component value is of a given type.

For example:

if (component.is<SimpleBlock>()) {
// ...
}
Template Parameters
TType to check, one of Type.
Returns
True if the component value is of type T.

◆ isToken()

template<typename T >
bool donner::css::ComponentValue::isToken ( ) const
inline

Shorthand for checking if this component value holds a specific token.

For example:

if (component.isToken<Token::Percentage>()) {
<percentage-token>, which represents a percentage such as '50'.
Definition Token.h:417

Which is equivalent to component.is<Token>() && component.get<Token>().is<Token::Percentage>().

Template Parameters
TToken type to check, which must be within the Token::TokenValue list.

◆ sourceOffset()

parser::FileOffset donner::css::ComponentValue::sourceOffset ( ) const
inline

Get the offset of this component value in the original source.

For Function and SimpleBlock, returns the offset of the group opening token.

◆ tryGetToken()

template<typename T >
const T * donner::css::ComponentValue::tryGetToken ( ) const
inline

Get the inner token value as a pointer, if the component value is a token and matches the requested type, or nullptr.

See also Token::tryGet().

Example:

if (const Token::Percentage* percentage = component.tryGetToken<Token::Percentage>()) {
// ...
}
Template Parameters
TToken type to check, which must be within the Token::TokenValue list.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const ComponentValue & component )
friend

Output a human-readable representation of the component value to a stream.

Parameters
osOutput stream.
componentComponent value to output.

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