LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
attributekey.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_ATTRIBUTEKEY_H
21 #define LIBREPCB_ATTRIBUTEKEY_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/sexpression.h"
27 #include "../toolbox.h"
28 
29 #include <type_safe/constrained_type.hpp>
30 
31 #include <QtCore>
32 
33 /*******************************************************************************
34  * Namespace / Forward Declarations
35  ******************************************************************************/
36 namespace librepcb {
37 
38 /*******************************************************************************
39  * Class AttributeKey
40  ******************************************************************************/
41 
42 inline static QString cleanAttributeKey(const QString& userInput) noexcept {
44  userInput, QRegularExpression("[^_0-9A-Z]"), true, false, true, "_", 40);
45 }
46 
48  template <typename Value, typename Predicate>
49  static constexpr auto verify(Value&& val, const Predicate& p) ->
50  typename std::decay<Value>::type {
51  return p(val)
52  ? std::forward<Value>(val)
53  : (throw RuntimeError(__FILE__, __LINE__,
54  QString(QApplication::translate(
55  "AttributeKey",
56  "Invalid attribute key: '%1'"))
57  .arg(val)),
58  std::forward<Value>(val));
59  }
60 };
61 
63  bool operator()(const QString& value) const noexcept {
64  return QRegularExpression("^[_0-9A-Z]{1,40}$")
65  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
66  .hasMatch();
67  }
68 };
69 
81 using AttributeKey =
82  type_safe::constrained_type<QString, AttributeKeyConstraint,
84 
85 inline bool operator==(const AttributeKey& lhs, const QString& rhs) noexcept {
86  return (*lhs) == rhs;
87 }
88 inline bool operator==(const QString& lhs, const AttributeKey& rhs) noexcept {
89  return lhs == (*rhs);
90 }
91 inline bool operator!=(const AttributeKey& lhs, const QString& rhs) noexcept {
92  return (*lhs) != rhs;
93 }
94 inline bool operator!=(const QString& lhs, const AttributeKey& rhs) noexcept {
95  return lhs != (*rhs);
96 }
97 
98 template <>
100  return SExpression::createString(*obj);
101 }
102 
103 template <>
105  bool throwIfEmpty) {
106  return AttributeKey(sexpr.getStringOrToken(throwIfEmpty)); // can throw
107 }
108 
109 inline QDataStream& operator<<(QDataStream& stream, const AttributeKey& obj) {
110  stream << *obj;
111  return stream;
112 }
113 
114 inline QDebug operator<<(QDebug stream, const AttributeKey& obj) {
115  stream << QString("AttributeKey('%1'')").arg(*obj);
116  return stream;
117 }
118 
119 inline uint qHash(const AttributeKey& key, uint seed = 0) noexcept {
120  return ::qHash(*key, seed);
121 }
122 
123 /*******************************************************************************
124  * End of File
125  ******************************************************************************/
126 
127 } // namespace librepcb
128 
129 #endif // LIBREPCB_ATTRIBUTEKEY_H
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
Definition: attributekey.h:47
type_safe::constrained_type< QString, AttributeKeyConstraint, AttributeKeyVerifier > AttributeKey
Definition: attributekey.h:83
static QString cleanUserInputString(const QString &input, const QRegularExpression &removeRegex, bool trim=true, bool toLower=false, bool toUpper=false, const QString &spaceReplacement=" ", int maxLength=-1) noexcept
Clean a user input string.
Definition: toolbox.cpp:208
Definition: attributekey.h:62
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: attributekey.h:49
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
static QString cleanAttributeKey(const QString &userInput) noexcept
Definition: attributekey.h:42
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
bool operator()(const QString &value) const noexcept
Definition: attributekey.h:63
static SExpression createString(const QString &string)
Definition: sexpression.cpp:284
The RuntimeError class.
Definition: exceptions.h:219
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:85
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:91
The SExpression class.
Definition: sexpression.h:57
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119