LibrePCB Developers Documentation
Loading...
Searching...
No Matches
elementname.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_ELEMENTNAME_H
21#define LIBREPCB_CORE_ELEMENTNAME_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 ElementName
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(__FILE__, __LINE__,
52 QString(QCoreApplication::translate(
53 "ElementName", "Invalid name: '%1'"))
54 .arg(val)),
55 std::forward<Value>(val));
56 }
57};
58
60 bool operator()(const QString& value) const noexcept {
61 if (value.isEmpty()) return false;
62 if (value.length() > 70) return false;
63 if (value != value.trimmed()) return false;
64 foreach (const QChar& c, value) {
65 if (!c.isPrint()) return false;
66 }
67 return true;
68 }
69};
70
83using ElementName = type_safe::constrained_type<QString, ElementNameConstraint,
85
86inline bool operator==(const ElementName& lhs, const QString& rhs) noexcept {
87 return (*lhs) == rhs;
88}
89inline bool operator==(const QString& lhs, const ElementName& rhs) noexcept {
90 return lhs == (*rhs);
91}
92inline bool operator!=(const ElementName& lhs, const QString& rhs) noexcept {
93 return (*lhs) != rhs;
94}
95inline bool operator!=(const QString& lhs, const ElementName& rhs) noexcept {
96 return lhs != (*rhs);
97}
98inline QString operator%(const ElementName& lhs, const QString& rhs) noexcept {
99 return (*lhs) % rhs;
100}
101inline QString operator%(const QString& lhs, const ElementName& rhs) noexcept {
102 return lhs % (*rhs);
103}
105 const ElementName& rhs) noexcept {
106 return ElementName((*lhs) % (*rhs)); // always safe, will not throw
107}
108
109template <>
110inline std::unique_ptr<SExpression> serialize(const ElementName& obj) {
111 return SExpression::createString(*obj);
112}
113
114template <>
116 return ElementName(node.getValue()); // can throw
117}
118
119template <>
120inline std::unique_ptr<SExpression> serialize(
121 const std::optional<ElementName>& obj) {
122 return SExpression::createString(obj ? **obj : "");
123}
124
125template <>
126inline std::optional<ElementName> deserialize(const SExpression& node) {
127 const QString str = node.getValue();
128 return str.isEmpty() ? std::nullopt
129 : std::make_optional(ElementName(str)); // can throw
130}
131
132inline QDataStream& operator<<(QDataStream& stream, const ElementName& obj) {
133 stream << *obj;
134 return stream;
135}
136
137inline QDebug operator<<(QDebug stream, const ElementName& obj) {
138 stream << QString("ElementName('%1')").arg(*obj);
139 return stream;
140}
141
142inline std::size_t qHash(const ElementName& key,
143 std::size_t seed = 0) noexcept {
144 return ::qHash(*key, seed);
145}
146
147inline static QString cleanElementName(const QString& userInput) noexcept {
148 QString ret = userInput.trimmed();
149 for (int i = ret.length() - 1; i >= 0; --i) {
150 if (!ret[i].isPrint()) {
151 ret.remove(i, 1);
152 }
153 }
154 ret.truncate(70);
155 return ret;
156}
157
158inline static ElementName elementNameFromTr(const char* context,
159 const char* textNoTr) noexcept {
160 Q_ASSERT(ElementNameConstraint()(textNoTr)); // textNoTr must be valid!!!
161 const QString textTr =
162 cleanElementName(QCoreApplication::translate(context, textNoTr));
163 if (ElementNameConstraint()(textTr)) {
164 return ElementName(textTr);
165 } else {
166 return ElementName(textNoTr);
167 }
168}
169
170/*******************************************************************************
171 * End of File
172 ******************************************************************************/
173
174} // namespace librepcb
175
176#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
Definition occmodel.cpp:76
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
static QString cleanElementName(const QString &userInput) noexcept
Definition elementname.h:147
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:85
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition attributekey.h:99
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:91
static ElementName elementNameFromTr(const char *context, const char *textNoTr) noexcept
Definition elementname.h:158
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
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition elementname.h:84
Definition elementname.h:59
bool operator()(const QString &value) const noexcept
Definition elementname.h:60
Definition elementname.h:45
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition elementname.h:47