LibrePCB Developers Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AttributeSubstitutor Class Referencefinal

The AttributeSubstitutor class substitutes attribute keys in strings with their actual values (e.g. replace "{{NAME}}" by "U42", a component's name) More...

#include <attributesubstitutor.h>

+ Collaboration diagram for AttributeSubstitutor:

Public Types

using LookupFunction = std::function< QString(const QString &)>
 
using FilterFunction = std::function< QString(const QString &)>
 

Public Member Functions

 AttributeSubstitutor ()=delete
 
 AttributeSubstitutor (const AttributeSubstitutor &other)=delete
 
AttributeSubstitutoroperator= (const AttributeSubstitutor &rhs)=delete
 
 ~AttributeSubstitutor ()=delete
 

Static Public Member Functions

static QString substitute (QString str, LookupFunction lookup=nullptr, FilterFunction filter=nullptr) noexcept
 Substitute all attribute keys in a string with their attribute values.
 

Static Private Member Functions

static bool searchVariablesInText (const QString &text, int startPos, int &pos, int &length, QStringList &keys) noexcept
 Search the next variables (e.g. "{{KEY or FALLBACK}}") in a given text.
 
static void applyFilter (QString &str, int &start, int &end, FilterFunction filter) noexcept
 
static bool getValueOfKey (const QString &key, QString &value, LookupFunction lookup) noexcept
 

Detailed Description

The AttributeSubstitutor class substitutes attribute keys in strings with their actual values (e.g. replace "{{NAME}}" by "U42", a component's name)

Please read the documentation about the Attributes System to get an idea how the Attributes System works in detail.

See also
librepcb::ProjectAttributeLookup
Attributes System
Todo:

Fix side-effect of the endless loop detection ("{{FOO}} {{FOO}}" is currently substituted by "{{FOO}} " because of the endless loop detection, even if there is actually no endless loop).

Properly implement multiple key substitution ("{{FOO or BAR}}" is currently substituted by "{{FOO}}", even if the attribute {{FOO}} indirectly evaluates to an empty string).

Member Typedef Documentation

◆ LookupFunction

using LookupFunction = std::function<QString(const QString&)>

◆ FilterFunction

using FilterFunction = std::function<QString(const QString&)>

Constructor & Destructor Documentation

◆ AttributeSubstitutor() [1/2]

◆ AttributeSubstitutor() [2/2]

AttributeSubstitutor ( const AttributeSubstitutor other)
delete

◆ ~AttributeSubstitutor()

~AttributeSubstitutor ( )
delete

Member Function Documentation

◆ operator=()

AttributeSubstitutor & operator= ( const AttributeSubstitutor rhs)
delete

◆ substitute()

QString substitute ( QString  str,
LookupFunction  lookup = nullptr,
FilterFunction  filter = nullptr 
)
staticnoexcept

Substitute all attribute keys in a string with their attribute values.

Parameters
strA string which can contain variables ("{{NAME}}"). The attributes will be substituted directly in this string.
lookupThe attribute lookup function (key -> value).
filterIf a function is passed here, the substituted values will be passed to this function first. This allows for example to remove invalid characters if the resulting string is used for a file path.
Returns
True if str was modified in some way, false if not
+ Here is the caller graph for this function:

◆ searchVariablesInText()

bool searchVariablesInText ( const QString &  text,
int  startPos,
int &  pos,
int &  length,
QStringList &  keys 
)
staticprivatenoexcept

Search the next variables (e.g. "{{KEY or FALLBACK}}") in a given text.

Parameters
textA text which can contain variables
startPosThe search start index (use 0 to search in the whole text)
posThe index of the next variable in the specified text (index of the first '{' character, or -1 if no variable found) will be written into this variable.
lengthIf a variable is found, the length (incl. '{{}}') will be written into this variable.
keysThe variable key names (text between '{{' and '}}', split by ' or ')
Returns
true if a variable is found, false if not

◆ applyFilter()

void applyFilter ( QString &  str,
int &  start,
int &  end,
FilterFunction  filter 
)
staticprivatenoexcept

◆ getValueOfKey()

bool getValueOfKey ( const QString &  key,
QString &  value,
LookupFunction  lookup 
)
staticprivatenoexcept

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