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 "../../serialization/sexpression.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 ComponentPrefix
40  ******************************************************************************/
41 
43  template <typename Value, typename Predicate>
44  static constexpr auto verify(Value&& val, const Predicate& p) ->
45  typename std::decay<Value>::type {
46  return p(val)
47  ? std::forward<Value>(val)
48  : (throw RuntimeError(
49  __FILE__, __LINE__,
50  QString(QCoreApplication::translate(
51  "ComponentPrefix", "Invalid component prefix: '%1'"))
52  .arg(val)),
53  std::forward<Value>(val));
54  }
55 };
56 
58  bool operator()(const QString& value) const noexcept {
59  return QRegularExpression("\\A[a-zA-Z_]{0,16}\\z")
60  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
61  .hasMatch();
62  }
63 };
64 
76 using ComponentPrefix =
77  type_safe::constrained_type<QString, ComponentPrefixConstraint,
79 
80 inline bool operator==(const ComponentPrefix& lhs,
81  const QString& rhs) noexcept {
82  return (*lhs) == rhs;
83 }
84 inline bool operator==(const QString& lhs,
85  const ComponentPrefix& rhs) noexcept {
86  return lhs == (*rhs);
87 }
88 inline bool operator!=(const ComponentPrefix& lhs,
89  const QString& rhs) noexcept {
90  return (*lhs) != rhs;
91 }
92 inline bool operator!=(const QString& lhs,
93  const ComponentPrefix& rhs) noexcept {
94  return lhs != (*rhs);
95 }
96 inline QString operator%(const ComponentPrefix& lhs,
97  const QString& rhs) noexcept {
98  return (*lhs) % rhs;
99 }
100 inline QString operator%(const QString& lhs,
101  const ComponentPrefix& rhs) noexcept {
102  return lhs % (*rhs);
103 }
104 
105 template <>
107  return SExpression::createString(*obj);
108 }
109 
110 template <>
112  return ComponentPrefix(node.getValue()); // can throw
113 }
114 
115 inline QDataStream& operator<<(QDataStream& stream,
116  const ComponentPrefix& obj) {
117  stream << *obj;
118  return stream;
119 }
120 
121 inline QDebug operator<<(QDebug stream, const ComponentPrefix& obj) {
122  stream << QString("ComponentPrefix('%1')").arg(*obj);
123  return stream;
124 }
125 
126 inline uint qHash(const ComponentPrefix& key, uint seed = 0) noexcept {
127  return ::qHash(*key, seed);
128 }
129 
130 /*******************************************************************************
131  * End of File
132  ******************************************************************************/
133 
134 } // namespace librepcb
135 
136 #endif
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
type_safe::constrained_type< QString, ComponentPrefixConstraint, ComponentPrefixVerifier > ComponentPrefix
Definition: componentprefix.h:78
Definition: componentprefix.h:42
static SExpression createString(const QString &string)
Definition: sexpression.cpp:380
The RuntimeError class.
Definition: exceptions.h:216
Definition: componentprefix.h:57
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
uint qHash(const ComponentPrefix &key, uint seed=0) noexcept
Definition: componentprefix.h:126
bool operator()(const QString &value) const noexcept
Definition: componentprefix.h:58
SExpression serialize(const AttributeKey &obj)
Definition: attributekey.h:99
const QString & getValue() const
Definition: sexpression.cpp:71
The SExpression class.
Definition: sexpression.h:66
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: componentprefix.h:44
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:118