LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
componentprefix.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_LIBRARY_COMPONENTPREFIX_H
21 #define LIBREPCB_LIBRARY_COMPONENTPREFIX_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
27 #include <type_safe/constrained_type.hpp>
28 
29 #include <QtCore>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 namespace library {
36 
37 /*******************************************************************************
38  * Class ComponentPrefix
39  ******************************************************************************/
40 
42  template <typename Value, typename Predicate>
43  static constexpr auto verify(Value&& val, const Predicate& p) ->
44  typename std::decay<Value>::type {
45  return p(val) ? std::forward<Value>(val)
46  : (throw RuntimeError(
47  __FILE__, __LINE__,
48  QString(QApplication::translate(
49  "ComponentPrefix",
50  "Invalid component prefix: '%1'"))
51  .arg(val)),
52  std::forward<Value>(val));
53  }
54 };
55 
57  bool operator()(const QString& value) const noexcept {
58  return QRegularExpression("^[a-zA-Z_]{0,16}$")
59  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
60  .hasMatch();
61  }
62 };
63 
75 using ComponentPrefix =
76  type_safe::constrained_type<QString, ComponentPrefixConstraint,
78 
79 } // namespace library
80 
81 inline bool operator==(const library::ComponentPrefix& lhs,
82  const QString& rhs) noexcept {
83  return (*lhs) == rhs;
84 }
85 inline bool operator==(const QString& lhs,
86  const library::ComponentPrefix& rhs) noexcept {
87  return lhs == (*rhs);
88 }
89 inline bool operator!=(const library::ComponentPrefix& lhs,
90  const QString& rhs) noexcept {
91  return (*lhs) != rhs;
92 }
93 inline bool operator!=(const QString& lhs,
94  const library::ComponentPrefix& rhs) noexcept {
95  return lhs != (*rhs);
96 }
97 inline QString operator%(const library::ComponentPrefix& lhs,
98  const QString& rhs) noexcept {
99  return (*lhs) % rhs;
100 }
101 inline QString operator%(const QString& lhs,
102  const library::ComponentPrefix& rhs) noexcept {
103  return lhs % (*rhs);
104 }
105 
106 template <>
108  return SExpression::createString(*obj);
109 }
110 
111 template <>
113  const SExpression& sexpr, bool throwIfEmpty) {
114  QString str = sexpr.getStringOrToken(throwIfEmpty);
115  return library::ComponentPrefix(str); // can throw
116 }
117 
118 inline QDataStream& operator<<(QDataStream& stream,
119  const library::ComponentPrefix& obj) {
120  stream << *obj;
121  return stream;
122 }
123 
124 inline QDebug operator<<(QDebug stream, const library::ComponentPrefix& obj) {
125  stream << QString("ComponentPrefix('%1'')").arg(*obj);
126  return stream;
127 }
128 
129 inline uint qHash(const library::ComponentPrefix& key, uint seed = 0) noexcept {
130  return ::qHash(*key, seed);
131 }
132 
133 /*******************************************************************************
134  * End of File
135  ******************************************************************************/
136 
137 } // namespace librepcb
138 
139 #endif // LIBREPCB_LIBRARY_COMPONENTPREFIX_H
bool operator()(const QString &value) const noexcept
Definition: componentprefix.h:57
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
uint qHash(const library::ComponentPrefix &key, uint seed=0) noexcept
Definition: componentprefix.h:129
Definition: componentprefix.h:41
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
static SExpression createString(const QString &string)
Definition: sexpression.cpp:284
type_safe::constrained_type< QString, ComponentPrefixConstraint, ComponentPrefixVerifier > ComponentPrefix
Definition: componentprefix.h:77
The RuntimeError class.
Definition: exceptions.h:219
QString operator%(const CircuitIdentifier &lhs, const QString &rhs) noexcept
Definition: circuitidentifier.h:107
Definition: componentprefix.h:56
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
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: componentprefix.h:43
The SExpression class.
Definition: sexpression.h:60
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119