LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
attributesubstitutor.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_ATTRIBUTESUBSTITUTOR_H
21 #define LIBREPCB_ATTRIBUTESUBSTITUTOR_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 #include <functional>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 class AttributeProvider;
36 
37 /*******************************************************************************
38  * Class AttributeSubstitutor
39  ******************************************************************************/
40 
62 class AttributeSubstitutor final {
63 public:
64  using FilterFunction = std::function<QString(const QString&)>;
65 
66  // Constructors / Destructor / Operator Overloadings
67  AttributeSubstitutor() = delete;
68  AttributeSubstitutor(const AttributeSubstitutor& other) = delete;
70  ~AttributeSubstitutor() = delete;
71 
72  // General Methods
73 
83  static QString substitute(QString str, const AttributeProvider* ap = nullptr,
84  FilterFunction filter = nullptr) noexcept;
85 
86 private: // Methods
103  static bool searchVariablesInText(const QString& text, int startPos, int& pos,
104  int& length, QStringList& keys) noexcept;
105 
106  static void applyFilter(QString& str, int& start, int& end,
107  FilterFunction filter) noexcept;
108 
109  static bool getValueOfKey(const QString& key, QString& value,
110  const AttributeProvider* ap) noexcept;
111 };
112 
113 /*******************************************************************************
114  * End of File
115  ******************************************************************************/
116 
117 } // namespace librepcb
118 
119 #endif // LIBREPCB_ATTRIBUTESUBSTITUTOR_H
static QString substitute(QString str, const AttributeProvider *ap=nullptr, FilterFunction filter=nullptr) noexcept
Substitute all attribute keys in a string with their attribute values.
Definition: attributesubstitutor.cpp:38
static void applyFilter(QString &str, int &start, int &end, FilterFunction filter) noexcept
Definition: attributesubstitutor.cpp:114
AttributeSubstitutor & operator=(const AttributeSubstitutor &rhs)=delete
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.
Definition: attributesubstitutor.cpp:89
static bool getValueOfKey(const QString &key, QString &value, const AttributeProvider *ap) noexcept
Definition: attributesubstitutor.cpp:122
The AttributeSubstitutor class substitutes attribute keys in strings with their actual values (e...
Definition: attributesubstitutor.h:62
std::function< QString(const QString &)> FilterFunction
Definition: attributesubstitutor.h:64
The AttributeProvider class defines an interface for classes which provides some attributes which can...
Definition: attributeprovider.h:62