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 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, const AttributeProvider *ap=nullptr, FilterFunction filter=nullptr) noexcept
 Substitute all attribute keys in a string with their attribute values. More...
 

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. More...
 
static void applyFilter (QString &str, int &start, int &end, FilterFunction filter) noexcept
 
static bool getValueOfKey (const QString &key, QString &value, const AttributeProvider *ap) 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::AttributeProvider
Attributes System
Author
ubruhin
Date
2017-09-19
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

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

Constructor & Destructor Documentation

AttributeSubstitutor ( const AttributeSubstitutor other)
delete
~AttributeSubstitutor ( )
delete

Member Function Documentation

AttributeSubstitutor& operator= ( const AttributeSubstitutor rhs)
delete
QString substitute ( QString  str,
const AttributeProvider ap = 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.
Returns
True if str was modified in some way, false if not

+ Here is the caller graph for this function:

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
void applyFilter ( QString &  str,
int &  start,
int &  end,
FilterFunction  filter 
)
staticprivatenoexcept
bool getValueOfKey ( const QString &  key,
QString &  value,
const AttributeProvider ap 
)
staticprivatenoexcept

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