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