LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
componentsymbolvariantitemsuffix.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_COMPONENTSYMBOLVARIANTITEMSUFFIX_H
21 #define LIBREPCB_LIBRARY_COMPONENTSYMBOLVARIANTITEMSUFFIX_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 ComponentSymbolVariantItemSuffix
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  "ComponentSymbolVariantItemSuffix",
50  "Invalid component symbol suffix: '%1'"))
51  .arg(val)),
52  std::forward<Value>(val));
53  }
54 };
55 
57  bool operator()(const QString& value) const noexcept {
58  return QRegularExpression("^[0-9a-zA-Z_]{0,16}$")
59  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
60  .hasMatch();
61  }
62 };
63 
77  type_safe::constrained_type<QString,
80 
81 } // namespace library
82 
84  const QString& rhs) noexcept {
85  return (*lhs) == rhs;
86 }
87 inline bool operator==(
88  const QString& lhs,
89  const library::ComponentSymbolVariantItemSuffix& rhs) noexcept {
90  return lhs == (*rhs);
91 }
93  const QString& rhs) noexcept {
94  return (*lhs) != rhs;
95 }
96 inline bool operator!=(
97  const QString& lhs,
98  const library::ComponentSymbolVariantItemSuffix& rhs) noexcept {
99  return lhs != (*rhs);
100 }
102  const QString& rhs) noexcept {
103  return (*lhs) % rhs;
104 }
105 inline QString operator%(
106  const QString& lhs,
107  const library::ComponentSymbolVariantItemSuffix& rhs) noexcept {
108  return lhs % (*rhs);
109 }
110 
111 template <>
114  return SExpression::createString(*obj);
115 }
116 
117 template <>
119  const SExpression& sexpr, bool throwIfEmpty) {
120  QString str = sexpr.getStringOrToken(throwIfEmpty);
121  // backward compatibility - remove this some time!
122  str.remove(QRegularExpression("[^0-9a-zA-Z_]"));
123  str.truncate(16);
124  return library::ComponentSymbolVariantItemSuffix(str); // can throw
125 }
126 
127 inline QDataStream& operator<<(
128  QDataStream& stream, const library::ComponentSymbolVariantItemSuffix& obj) {
129  stream << *obj;
130  return stream;
131 }
132 
133 inline QDebug operator<<(QDebug stream,
135  stream << QString("ComponentSymbolVariantItemSuffix('%1'')").arg(*obj);
136  return stream;
137 }
138 
140  uint seed = 0) noexcept {
141  return ::qHash(*key, seed);
142 }
143 
144 /*******************************************************************************
145  * End of File
146  ******************************************************************************/
147 
148 } // namespace librepcb
149 
150 #endif // LIBREPCB_LIBRARY_COMPONENTSYMBOLVARIANTITEMSUFFIX_H
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:114
type_safe::constrained_type< QString, ComponentSymbolVariantItemSuffixConstraint, ComponentSymbolVariantItemSuffixVerifier > ComponentSymbolVariantItemSuffix
Definition: componentsymbolvariantitemsuffix.h:79
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
Definition: componentsymbolvariantitemsuffix.h:41
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
static SExpression createString(const QString &string)
Definition: sexpression.cpp:278
uint qHash(const library::ComponentSymbolVariantItemSuffix &key, uint seed=0) noexcept
Definition: componentsymbolvariantitemsuffix.h:139
bool operator()(const QString &value) const noexcept
Definition: componentsymbolvariantitemsuffix.h:57
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
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: componentsymbolvariantitemsuffix.h:43
Definition: componentsymbolvariantitemsuffix.h:56
The SExpression class.
Definition: sexpression.h:60
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:124