LibrePCB Developers Documentation
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_CORE_COMPONENTSYMBOLVARIANTITEMSUFFIX_H
21 #define LIBREPCB_CORE_COMPONENTSYMBOLVARIANTITEMSUFFIX_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 /*******************************************************************************
35  * Namespace / Forward Declarations
36  ******************************************************************************/
37 namespace librepcb {
38 
39 /*******************************************************************************
40  * Class ComponentSymbolVariantItemSuffix
41  ******************************************************************************/
42 
44  const QString& userInput) noexcept {
45  return Toolbox::cleanUserInputString(userInput,
46  QRegularExpression("[^0-9a-zA-Z_]"),
47  true, false, false, "_", 16);
48 }
49 
51  template <typename Value, typename Predicate>
52  static constexpr auto verify(Value&& val, const Predicate& p) ->
53  typename std::decay<Value>::type {
54  return p(val) ? std::forward<Value>(val)
55  : (throw RuntimeError(
56  __FILE__, __LINE__,
57  QString(QCoreApplication::translate(
58  "ComponentSymbolVariantItemSuffix",
59  "Invalid component symbol suffix: '%1'"))
60  .arg(val)),
61  std::forward<Value>(val));
62  }
63 };
64 
66  bool operator()(const QString& value) const noexcept {
67  return QRegularExpression("\\A[0-9a-zA-Z_]{0,16}\\z")
68  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
69  .hasMatch();
70  }
71 };
72 
86  type_safe::constrained_type<QString,
89 
91  const QString& rhs) noexcept {
92  return (*lhs) == rhs;
93 }
94 inline bool operator==(const QString& lhs,
95  const ComponentSymbolVariantItemSuffix& rhs) noexcept {
96  return lhs == (*rhs);
97 }
99  const QString& rhs) noexcept {
100  return (*lhs) != rhs;
101 }
102 inline bool operator!=(const QString& lhs,
103  const ComponentSymbolVariantItemSuffix& rhs) noexcept {
104  return lhs != (*rhs);
105 }
106 inline QString operator%(const ComponentSymbolVariantItemSuffix& lhs,
107  const QString& rhs) noexcept {
108  return (*lhs) % rhs;
109 }
110 inline QString operator%(const QString& lhs,
111  const ComponentSymbolVariantItemSuffix& rhs) noexcept {
112  return lhs % (*rhs);
113 }
114 
115 template <>
117  return SExpression::createString(*obj);
118 }
119 
120 template <>
122  return ComponentSymbolVariantItemSuffix(node.getValue()); // can throw
123 }
124 
125 inline QDataStream& operator<<(QDataStream& stream,
127  stream << *obj;
128  return stream;
129 }
130 
131 inline QDebug operator<<(QDebug stream,
133  stream << QString("ComponentSymbolVariantItemSuffix('%1')").arg(*obj);
134  return stream;
135 }
136 
138  uint seed = 0) noexcept {
139  return ::qHash(*key, seed);
140 }
141 
142 /*******************************************************************************
143  * End of File
144  ******************************************************************************/
145 
146 } // namespace librepcb
147 
148 #endif
Definition: componentsymbolvariantitemsuffix.h:65
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 cleanUserInputString(const QString &input, const QRegularExpression &removeRegex, bool trim=true, bool toLower=false, bool toUpper=false, const QString &spaceReplacement=" ", int maxLength=-1) noexcept
Clean a user input string.
Definition: toolbox.cpp:235
bool operator()(const QString &value) const noexcept
Definition: componentsymbolvariantitemsuffix.h:66
static QString cleanComponentSymbolVariantItemSuffix(const QString &userInput) noexcept
Definition: componentsymbolvariantitemsuffix.h:43
uint qHash(const ComponentSymbolVariantItemSuffix &key, uint seed=0) noexcept
Definition: componentsymbolvariantitemsuffix.h:137
static SExpression createString(const QString &string)
Definition: sexpression.cpp:380
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: componentsymbolvariantitemsuffix.h:52
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
Definition: componentsymbolvariantitemsuffix.h:50
SExpression serialize(const AttributeKey &obj)
Definition: attributekey.h:99
type_safe::constrained_type< QString, ComponentSymbolVariantItemSuffixConstraint, ComponentSymbolVariantItemSuffixVerifier > ComponentSymbolVariantItemSuffix
Definition: componentsymbolvariantitemsuffix.h:88
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