LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_ELEMENTNAME_H
21 #define LIBREPCB_ELEMENTNAME_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 ElementName
40  ******************************************************************************/
41 
42 inline static QString cleanElementName(const QString& userInput) noexcept {
43  QString ret = userInput.trimmed();
44  for (int i = ret.length() - 1; i >= 0; --i) {
45  if (!ret[i].isPrint()) {
46  ret.remove(i, 1);
47  }
48  }
49  ret.truncate(70);
50  return ret;
51 }
52 
54  template <typename Value, typename Predicate>
55  static constexpr auto verify(Value&& val, const Predicate& p) ->
56  typename std::decay<Value>::type {
57  return p(val) ? std::forward<Value>(val)
58  : (throw RuntimeError(
59  __FILE__, __LINE__,
60  QString(QApplication::translate("ElementName",
61  "Invalid name: '%1'"))
62  .arg(val)),
63  std::forward<Value>(val));
64  }
65 };
66 
68  bool operator()(const QString& value) const noexcept {
69  if (value.isEmpty()) return false;
70  if (value.length() > 70) return false;
71  if (value != value.trimmed()) return false;
72  foreach (const QChar& c, value) {
73  if (!c.isPrint()) return false;
74  }
75  return true;
76  }
77 };
78 
91 using ElementName = type_safe::constrained_type<QString, ElementNameConstraint,
93 
94 inline bool operator==(const ElementName& lhs, const QString& rhs) noexcept {
95  return (*lhs) == rhs;
96 }
97 inline bool operator==(const QString& lhs, const ElementName& rhs) noexcept {
98  return lhs == (*rhs);
99 }
100 inline bool operator!=(const ElementName& lhs, const QString& rhs) noexcept {
101  return (*lhs) != rhs;
102 }
103 inline bool operator!=(const QString& lhs, const ElementName& rhs) noexcept {
104  return lhs != (*rhs);
105 }
106 inline QString operator%(const ElementName& lhs, const QString& rhs) noexcept {
107  return (*lhs) % rhs;
108 }
109 inline QString operator%(const QString& lhs, const ElementName& rhs) noexcept {
110  return lhs % (*rhs);
111 }
113  const ElementName& rhs) noexcept {
114  return ElementName((*lhs) % (*rhs)); // always safe, will not throw
115 }
116 
117 template <>
119  return SExpression::createString(*obj);
120 }
121 
122 template <>
124  bool throwIfEmpty) {
125  QString str = sexpr.getStringOrToken(throwIfEmpty);
126  // backward compatibility - remove this some time!
127  str = str.trimmed();
128  str.truncate(70);
129  return ElementName(str); // can throw
130 }
131 
132 inline QDataStream& operator<<(QDataStream& stream, const ElementName& obj) {
133  stream << *obj;
134  return stream;
135 }
136 
137 inline QDebug operator<<(QDebug stream, const ElementName& obj) {
138  stream << QString("ElementName('%1'')").arg(*obj);
139  return stream;
140 }
141 
142 inline uint qHash(const ElementName& 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_ELEMENTNAME_H
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: elementname.h:55
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:114
static QString cleanElementName(const QString &userInput) noexcept
Definition: elementname.h:42
Definition: elementname.h:53
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
bool operator()(const QString &value) const noexcept
Definition: elementname.h:68
uint qHash(const ElementName &key, uint seed=0) noexcept
Definition: elementname.h:142
static SExpression createString(const QString &string)
Definition: sexpression.cpp:278
Definition: elementname.h:67
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:60
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:124
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition: elementname.h:92