LibrePCB Developers Documentation
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_CORE_ATTRIBUTEKEY_H
21 #define LIBREPCB_CORE_ATTRIBUTEKEY_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../serialization/sexpression.h"
27 #include "../utils/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(
54  __FILE__, __LINE__,
55  QString(QCoreApplication::translate(
56  "AttributeKey", "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("\\A[_0-9A-Z]{1,40}\\z")
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 <>
99 inline SExpression serialize(const AttributeKey& obj) {
100  return SExpression::createString(*obj);
101 }
102 
103 template <>
104 inline AttributeKey deserialize(const SExpression& node) {
105  return AttributeKey(node.getValue()); // can throw
106 }
107 
108 inline QDataStream& operator<<(QDataStream& stream, const AttributeKey& obj) {
109  stream << *obj;
110  return stream;
111 }
112 
113 inline QDebug operator<<(QDebug stream, const AttributeKey& obj) {
114  stream << QString("AttributeKey('%1')").arg(*obj);
115  return stream;
116 }
117 
118 inline uint qHash(const AttributeKey& key, uint seed = 0) noexcept {
119  return ::qHash(*key, seed);
120 }
121 
122 /*******************************************************************************
123  * End of File
124  ******************************************************************************/
125 
126 } // namespace librepcb
127 
128 #endif
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:108
Definition: occmodel.cpp:76
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:235
Definition: attributekey.h:62
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: attributekey.h:49
static QString cleanAttributeKey(const QString &userInput) noexcept
Definition: attributekey.h:42
bool operator()(const QString &value) const noexcept
Definition: attributekey.h:63
static SExpression createString(const QString &string)
Definition: sexpression.cpp:380
The RuntimeError class.
Definition: exceptions.h:216
AttributeKey deserialize(const SExpression &node)
Definition: attributekey.h:104
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
SExpression serialize(const AttributeKey &obj)
Definition: attributekey.h:99
const QString & getValue() const
Definition: sexpression.cpp:71
The SExpression class.
Definition: sexpression.h:66
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:118