LibrePCB Developers Documentation
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_CORE_COMPONENTPREFIX_H
21 #define LIBREPCB_CORE_COMPONENTPREFIX_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../../exceptions.h"
27 #include "../../qtcompat.h"
28 #include "../../serialization/sexpression.h"
29 #include "../../utils/toolbox.h"
30 
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 ComponentPrefix
42  ******************************************************************************/
43 
44 inline static QString cleanComponentPrefix(const QString& userInput) noexcept {
46  userInput, QRegularExpression("[^a-zA-Z_]"), true, false, false, "_", 16);
47 }
48 
50  template <typename Value, typename Predicate>
51  static constexpr auto verify(Value&& val, const Predicate& p) ->
52  typename std::decay<Value>::type {
53  return p(val)
54  ? std::forward<Value>(val)
55  : (throw RuntimeError(
56  __FILE__, __LINE__,
57  QString(QCoreApplication::translate(
58  "ComponentPrefix", "Invalid component prefix: '%1'"))
59  .arg(val)),
60  std::forward<Value>(val));
61  }
62 };
63 
65  bool operator()(const QString& value) const noexcept {
66  return QRegularExpression("\\A[a-zA-Z_]{0,16}\\z")
67  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
68  .hasMatch();
69  }
70 };
71 
83 using ComponentPrefix =
84  type_safe::constrained_type<QString, ComponentPrefixConstraint,
86 
87 inline bool operator==(const ComponentPrefix& lhs,
88  const QString& rhs) noexcept {
89  return (*lhs) == rhs;
90 }
91 inline bool operator==(const QString& lhs,
92  const ComponentPrefix& rhs) noexcept {
93  return lhs == (*rhs);
94 }
95 inline bool operator!=(const ComponentPrefix& lhs,
96  const QString& rhs) noexcept {
97  return (*lhs) != rhs;
98 }
99 inline bool operator!=(const QString& lhs,
100  const ComponentPrefix& rhs) noexcept {
101  return lhs != (*rhs);
102 }
103 inline QString operator%(const ComponentPrefix& lhs,
104  const QString& rhs) noexcept {
105  return (*lhs) % rhs;
106 }
107 inline QString operator%(const QString& lhs,
108  const ComponentPrefix& rhs) noexcept {
109  return lhs % (*rhs);
110 }
111 
112 template <>
113 inline std::unique_ptr<SExpression> serialize(const ComponentPrefix& obj) {
114  return SExpression::createString(*obj);
115 }
116 
117 template <>
119  return ComponentPrefix(node.getValue()); // can throw
120 }
121 
122 inline QDataStream& operator<<(QDataStream& stream,
123  const ComponentPrefix& obj) {
124  stream << *obj;
125  return stream;
126 }
127 
128 inline QDebug operator<<(QDebug stream, const ComponentPrefix& obj) {
129  stream << QString("ComponentPrefix('%1')").arg(*obj);
130  return stream;
131 }
132 
134  QtCompat::Hash seed = 0) noexcept {
135  return ::qHash(*key, seed);
136 }
137 
138 /*******************************************************************************
139  * End of File
140  ******************************************************************************/
141 
142 } // namespace librepcb
143 
144 #endif
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
QString operator%(const ComponentPrefix &lhs, const QString &rhs) noexcept
Definition: componentprefix.h:103
Definition: occmodel.cpp:77
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:242
type_safe::constrained_type< QString, ComponentPrefixConstraint, ComponentPrefixVerifier > ComponentPrefix
Definition: componentprefix.h:85
Definition: componentprefix.h:49
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition: attributekey.h:100
static QString cleanComponentPrefix(const QString &userInput) noexcept
Definition: componentprefix.h:44
The RuntimeError class.
Definition: exceptions.h:216
Definition: componentprefix.h:64
AttributeKey deserialize(const SExpression &node)
Definition: attributekey.h:105
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:86
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:92
static std::unique_ptr< SExpression > createString(const QString &string)
Definition: sexpression.cpp:405
QtCompat::Hash qHash(const ComponentPrefix &key, QtCompat::Hash seed=0) noexcept
Definition: componentprefix.h:133
uint Hash
Return type of Qt&#39;s qHash() function.
Definition: qtcompat.h:58
bool operator()(const QString &value) const noexcept
Definition: componentprefix.h:65
const QString & getValue() const
Definition: sexpression.cpp:71
The SExpression class.
Definition: sexpression.h:69
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: componentprefix.h:51