LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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>
35
36/*******************************************************************************
37 * Namespace / Forward Declarations
38 ******************************************************************************/
39namespace librepcb {
40
41/*******************************************************************************
42 * Class CircuitIdentifier
43 ******************************************************************************/
44
46 template <typename Value, typename Predicate>
47 static constexpr auto verify(Value&& val, const Predicate& p) ->
48 typename std::decay<Value>::type {
49 return p(val)
50 ? std::forward<Value>(val)
51 : (throw RuntimeError(
52 __FILE__, __LINE__,
53 QString(QCoreApplication::translate("CircuitIdentifier",
54 "Invalid identifier: '%1'"))
55 .arg(val)),
56 std::forward<Value>(val));
57 }
58};
59
61 static const constexpr int MAX_LENGTH = 32;
62
63 bool operator()(const QString& value) const noexcept {
64 return QRegularExpression("\\A[-a-zA-Z0-9._+/!?&@#$()]{1,32}\\z")
65 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
66 .hasMatch();
67 }
68};
69
87 type_safe::constrained_type<QString, CircuitIdentifierConstraint,
89
90inline bool operator==(const CircuitIdentifier& lhs,
91 const QString& rhs) noexcept {
92 return (*lhs) == rhs;
93}
94inline bool operator==(const QString& lhs,
95 const CircuitIdentifier& rhs) noexcept {
96 return lhs == (*rhs);
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 QString operator%(const CircuitIdentifier& lhs,
107 const QString& rhs) noexcept {
108 return (*lhs) % rhs;
109}
110inline QString operator%(const QString& lhs,
111 const CircuitIdentifier& rhs) noexcept {
112 return lhs % (*rhs);
113}
114inline QString operator%(const CircuitIdentifier& lhs,
115 const CircuitIdentifier& rhs) noexcept {
116 return (*lhs) % (*rhs);
117}
118
119template <>
120inline std::unique_ptr<SExpression> serialize(const CircuitIdentifier& obj) {
121 return SExpression::createString(*obj);
122}
123
124template <>
126 return CircuitIdentifier(node.getValue()); // can throw
127}
128
129template <>
130inline std::unique_ptr<SExpression> serialize(
131 const std::optional<CircuitIdentifier>& obj) {
132 if (obj) {
133 return serialize(*obj);
134 } else {
135 return SExpression::createString("");
136 }
137}
138
139template <>
140inline std::optional<CircuitIdentifier> deserialize(const SExpression& node) {
141 if (node.getValue().isEmpty()) {
142 return std::nullopt;
143 } else {
144 return deserialize<CircuitIdentifier>(node); // can throw
145 }
146}
147
148inline QDataStream& operator<<(QDataStream& stream,
149 const CircuitIdentifier& obj) {
150 stream << *obj;
151 return stream;
152}
153
154inline QDebug operator<<(QDebug stream, const CircuitIdentifier& obj) {
155 stream << QString("CircuitIdentifier('%1')").arg(*obj);
156 return stream;
157}
158
159inline std::size_t qHash(const CircuitIdentifier& key,
160 std::size_t seed = 0) noexcept {
161 return ::qHash(*key, seed);
162}
163
164inline static QString cleanCircuitIdentifier(
165 const QString& userInput) noexcept {
167 userInput, QRegularExpression("[^-a-zA-Z0-9._+/!?&@#$()]"), true, false,
169}
170
171inline static std::optional<CircuitIdentifier> parseCircuitIdentifier(
172 const QString& name) noexcept {
173 return CircuitIdentifierConstraint()(name)
174 ? CircuitIdentifier(name)
175 : std::optional<CircuitIdentifier>();
176}
177
178/*******************************************************************************
179 * End of File
180 ******************************************************************************/
181
182} // namespace librepcb
183
184#endif
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:407
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:247
Definition occmodel.cpp:77
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:85
type_safe::constrained_type< QString, CircuitIdentifierConstraint, CircuitIdentifierVerifier > CircuitIdentifier
Definition circuitidentifier.h:88
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition attributekey.h:99
static QString cleanCircuitIdentifier(const QString &userInput) noexcept
Definition circuitidentifier.h:164
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:91
static std::optional< CircuitIdentifier > parseCircuitIdentifier(const QString &name) noexcept
Definition circuitidentifier.h:171
AttributeKey deserialize(const SExpression &node)
Definition attributekey.h:104
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108
QString operator%(const ComponentPrefix &lhs, const QString &rhs) noexcept
Definition componentprefix.h:102
Definition circuitidentifier.h:60
bool operator()(const QString &value) const noexcept
Definition circuitidentifier.h:63
static const constexpr int MAX_LENGTH
Definition circuitidentifier.h:61
Definition circuitidentifier.h:45
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition circuitidentifier.h:47