LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_CIRCUITIDENTIFIER_H
21 #define LIBREPCB_CIRCUITIDENTIFIER_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 CircuitIdentifier
40  ******************************************************************************/
41 
42 inline static QString cleanCircuitIdentifier(
43  const QString& userInput) noexcept {
45  userInput, QRegularExpression("[^-a-zA-Z0-9_+/!?@#$]"), true, false,
46  false, "_", 32);
47 }
48 
50  template <typename Value, typename Predicate>
51  static constexpr auto verify(Value&& val, const Predicate& p) ->
52  typename std::decay<Value>::type {
53  return p(val) ? std::forward<Value>(val)
54  : (throw RuntimeError(__FILE__, __LINE__,
55  QString(QApplication::translate(
56  "CircuitIdentifier",
57  "Invalid identifier: '%1'"))
58  .arg(val)),
59  std::forward<Value>(val));
60  }
61 };
62 
64  bool operator()(const QString& value) const noexcept {
65  return QRegularExpression("^[-a-zA-Z0-9_+/!?@#$]{1,32}$")
66  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
67  .hasMatch();
68  }
69 };
70 
87 using CircuitIdentifier =
88  type_safe::constrained_type<QString, CircuitIdentifierConstraint,
90 
91 inline bool operator==(const CircuitIdentifier& lhs,
92  const QString& rhs) noexcept {
93  return (*lhs) == rhs;
94 }
95 inline bool operator==(const QString& lhs,
96  const CircuitIdentifier& rhs) noexcept {
97  return lhs == (*rhs);
98 }
99 inline bool operator!=(const CircuitIdentifier& lhs,
100  const QString& rhs) noexcept {
101  return (*lhs) != rhs;
102 }
103 inline bool operator!=(const QString& lhs,
104  const CircuitIdentifier& rhs) noexcept {
105  return lhs != (*rhs);
106 }
107 inline QString operator%(const CircuitIdentifier& lhs,
108  const QString& rhs) noexcept {
109  return (*lhs) % rhs;
110 }
111 inline QString operator%(const QString& lhs,
112  const CircuitIdentifier& rhs) noexcept {
113  return lhs % (*rhs);
114 }
115 inline QString operator%(const CircuitIdentifier& lhs,
116  const CircuitIdentifier& rhs) noexcept {
117  return (*lhs) % (*rhs);
118 }
119 
120 template <>
122  return SExpression::createString(*obj);
123 }
124 
125 template <>
127  bool throwIfEmpty) {
128  return CircuitIdentifier(sexpr.getStringOrToken(throwIfEmpty)); // can throw
129 }
130 
131 inline QDataStream& operator<<(QDataStream& stream,
132  const CircuitIdentifier& obj) {
133  stream << *obj;
134  return stream;
135 }
136 
137 inline QDebug operator<<(QDebug stream, const CircuitIdentifier& obj) {
138  stream << QString("CircuitIdentifier('%1'')").arg(*obj);
139  return stream;
140 }
141 
142 inline uint qHash(const CircuitIdentifier& key, uint seed = 0) noexcept {
143  return ::qHash(*key, seed);
144 }
145 
146 /*******************************************************************************
147  * End of File
148  ******************************************************************************/
149 
150 } // namespace librepcb
151 
152 #endif // LIBREPCB_CIRCUITIDENTIFIER_H
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
static QString cleanCircuitIdentifier(const QString &userInput) noexcept
Definition: circuitidentifier.h:42
Definition: circuitidentifier.h:63
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:209
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
Definition: circuitidentifier.h:49
static SExpression createString(const QString &string)
Definition: sexpression.cpp:284
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: circuitidentifier.h:51
uint qHash(const CircuitIdentifier &key, uint seed=0) noexcept
Definition: circuitidentifier.h:142
type_safe::constrained_type< QString, CircuitIdentifierConstraint, CircuitIdentifierVerifier > CircuitIdentifier
Definition: circuitidentifier.h:89
The RuntimeError class.
Definition: exceptions.h:219
QString operator%(const CircuitIdentifier &lhs, const QString &rhs) noexcept
Definition: circuitidentifier.h:107
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
bool operator()(const QString &value) const noexcept
Definition: circuitidentifier.h:64