LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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#include "../../utils/toolbox.h"
29
30#include <type_safe/constrained_type.hpp>
31
32#include <QtCore>
33
34/*******************************************************************************
35 * Namespace / Forward Declarations
36 ******************************************************************************/
37namespace librepcb {
38
39/*******************************************************************************
40 * Class ComponentPrefix
41 ******************************************************************************/
42
43inline static QString cleanComponentPrefix(const QString& userInput) noexcept {
45 userInput, QRegularExpression("[^a-zA-Z_]"), true, false, false, "_", 16);
46}
47
49 template <typename Value, typename Predicate>
50 static constexpr auto verify(Value&& val, const Predicate& p) ->
51 typename std::decay<Value>::type {
52 return p(val)
53 ? std::forward<Value>(val)
54 : (throw RuntimeError(
55 __FILE__, __LINE__,
56 QString(QCoreApplication::translate(
57 "ComponentPrefix", "Invalid component prefix: '%1'"))
58 .arg(val)),
59 std::forward<Value>(val));
60 }
61};
62
64 bool operator()(const QString& value) const noexcept {
65 return QRegularExpression("\\A[a-zA-Z_]{0,16}\\z")
66 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
67 .hasMatch();
68 }
69};
70
83 type_safe::constrained_type<QString, ComponentPrefixConstraint,
85
86inline bool operator==(const ComponentPrefix& lhs,
87 const QString& rhs) noexcept {
88 return (*lhs) == rhs;
89}
90inline bool operator==(const QString& lhs,
91 const ComponentPrefix& rhs) noexcept {
92 return lhs == (*rhs);
93}
94inline bool operator!=(const ComponentPrefix& lhs,
95 const QString& rhs) noexcept {
96 return (*lhs) != rhs;
97}
98inline bool operator!=(const QString& lhs,
99 const ComponentPrefix& rhs) noexcept {
100 return lhs != (*rhs);
101}
102inline QString operator%(const ComponentPrefix& lhs,
103 const QString& rhs) noexcept {
104 return (*lhs) % rhs;
105}
106inline QString operator%(const QString& lhs,
107 const ComponentPrefix& rhs) noexcept {
108 return lhs % (*rhs);
109}
110
111template <>
112inline std::unique_ptr<SExpression> serialize(const ComponentPrefix& obj) {
113 return SExpression::createString(*obj);
114}
115
116template <>
118 return ComponentPrefix(node.getValue()); // can throw
119}
120
121inline QDataStream& operator<<(QDataStream& stream,
122 const ComponentPrefix& obj) {
123 stream << *obj;
124 return stream;
125}
126
127inline QDebug operator<<(QDebug stream, const ComponentPrefix& obj) {
128 stream << QString("ComponentPrefix('%1')").arg(*obj);
129 return stream;
130}
131
132inline std::size_t qHash(const ComponentPrefix& key,
133 std::size_t seed = 0) noexcept {
134 return ::qHash(*key, seed);
135}
136
137/*******************************************************************************
138 * End of File
139 ******************************************************************************/
140
141} // namespace librepcb
142
143#endif
The RuntimeError class.
Definition exceptions.h:218
The SExpression class.
Definition sexpression.h:69
static std::unique_ptr< SExpression > createString(const QString &string)
Definition sexpression.cpp:407
const QString & getValue() const
Definition sexpression.cpp:71
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:247
Definition occmodel.cpp:76
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
static QString cleanComponentPrefix(const QString &userInput) noexcept
Definition componentprefix.h:43
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:85
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition attributekey.h:99
type_safe::constrained_type< QString, ComponentPrefixConstraint, ComponentPrefixVerifier > ComponentPrefix
Definition componentprefix.h:84
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:91
AttributeKey deserialize(const SExpression &node)
Definition attributekey.h:104
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108
QString operator%(const ComponentPrefix &lhs, const QString &rhs) noexcept
Definition componentprefix.h:102
Definition componentprefix.h:63
bool operator()(const QString &value) const noexcept
Definition componentprefix.h:64
Definition componentprefix.h:48
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition componentprefix.h:50