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