LibrePCB Developers Documentation
circuitidentifier.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_CIRCUITIDENTIFIER_H
21 #define LIBREPCB_CORE_CIRCUITIDENTIFIER_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../exceptions.h"
27 #include "../serialization/sexpression.h"
28 #include "../utils/toolbox.h"
29 
30 #include <type_safe/constrained_type.hpp>
31 
32 #include <QtCore>
33 
34 #include <optional.hpp>
35 
36 /*******************************************************************************
37  * Namespace / Forward Declarations
38  ******************************************************************************/
39 namespace librepcb {
40 
41 /*******************************************************************************
42  * Class CircuitIdentifier
43  ******************************************************************************/
44 
45 inline static QString cleanCircuitIdentifier(
46  const QString& userInput) noexcept {
48  userInput, QRegularExpression("[^-a-zA-Z0-9._+/!?&@#$()]"), true, false,
49  false, "_", 32);
50 }
51 
53  template <typename Value, typename Predicate>
54  static constexpr auto verify(Value&& val, const Predicate& p) ->
55  typename std::decay<Value>::type {
56  return p(val)
57  ? std::forward<Value>(val)
58  : (throw RuntimeError(
59  __FILE__, __LINE__,
60  QString(QCoreApplication::translate("CircuitIdentifier",
61  "Invalid identifier: '%1'"))
62  .arg(val)),
63  std::forward<Value>(val));
64  }
65 };
66 
68  bool operator()(const QString& value) const noexcept {
69  return QRegularExpression("\\A[-a-zA-Z0-9._+/!?&@#$()]{1,32}\\z")
70  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
71  .hasMatch();
72  }
73 };
74 
91 using CircuitIdentifier =
92  type_safe::constrained_type<QString, CircuitIdentifierConstraint,
94 
95 inline bool operator==(const CircuitIdentifier& lhs,
96  const QString& rhs) noexcept {
97  return (*lhs) == rhs;
98 }
99 inline bool operator==(const QString& lhs,
100  const CircuitIdentifier& rhs) noexcept {
101  return lhs == (*rhs);
102 }
103 inline bool operator!=(const CircuitIdentifier& lhs,
104  const QString& rhs) noexcept {
105  return (*lhs) != rhs;
106 }
107 inline bool operator!=(const QString& lhs,
108  const CircuitIdentifier& rhs) noexcept {
109  return lhs != (*rhs);
110 }
111 inline QString operator%(const CircuitIdentifier& lhs,
112  const QString& rhs) noexcept {
113  return (*lhs) % rhs;
114 }
115 inline QString operator%(const QString& lhs,
116  const CircuitIdentifier& rhs) noexcept {
117  return lhs % (*rhs);
118 }
119 inline QString operator%(const CircuitIdentifier& lhs,
120  const CircuitIdentifier& rhs) noexcept {
121  return (*lhs) % (*rhs);
122 }
123 
124 template <>
126  return SExpression::createString(*obj);
127 }
128 
129 template <>
131  return CircuitIdentifier(node.getValue()); // can throw
132 }
133 
134 template <>
135 inline SExpression serialize(const tl::optional<CircuitIdentifier>& obj) {
136  if (obj) {
137  return serialize(*obj);
138  } else {
139  return SExpression::createString("");
140  }
141 }
142 
143 template <>
144 inline tl::optional<CircuitIdentifier> deserialize(const SExpression& node) {
145  if (node.getValue().isEmpty()) {
146  return tl::nullopt;
147  } else {
148  return deserialize<CircuitIdentifier>(node); // can throw
149  }
150 }
151 
152 inline QDataStream& operator<<(QDataStream& stream,
153  const CircuitIdentifier& obj) {
154  stream << *obj;
155  return stream;
156 }
157 
158 inline QDebug operator<<(QDebug stream, const CircuitIdentifier& obj) {
159  stream << QString("CircuitIdentifier('%1')").arg(*obj);
160  return stream;
161 }
162 
163 inline uint qHash(const CircuitIdentifier& key, uint seed = 0) noexcept {
164  return ::qHash(*key, seed);
165 }
166 
167 /*******************************************************************************
168  * End of File
169  ******************************************************************************/
170 
171 } // namespace librepcb
172 
173 #endif
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:108
QString operator%(const ComponentPrefix &lhs, const QString &rhs) noexcept
Definition: componentprefix.h:96
static QString cleanCircuitIdentifier(const QString &userInput) noexcept
Definition: circuitidentifier.h:45
Definition: circuitidentifier.h:67
Definition: occmodel.cpp:76
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: circuitidentifier.h:52
static SExpression createString(const QString &string)
Definition: sexpression.cpp:380
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: circuitidentifier.h:54
uint qHash(const CircuitIdentifier &key, uint seed=0) noexcept
Definition: circuitidentifier.h:163
type_safe::constrained_type< QString, CircuitIdentifierConstraint, CircuitIdentifierVerifier > CircuitIdentifier
Definition: circuitidentifier.h:93
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
bool operator()(const QString &value) const noexcept
Definition: circuitidentifier.h:68