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