LibrePCB Developers Documentation
attributekey.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_ATTRIBUTEKEY_H
21#define LIBREPCB_CORE_ATTRIBUTEKEY_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../qtcompat.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 AttributeKey
41 ******************************************************************************/
42
43inline static QString cleanAttributeKey(const QString& userInput) noexcept {
45 userInput, QRegularExpression("[^_0-9A-Z]"), true, false, true, "_", 40);
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 "AttributeKey", "Invalid attribute key: '%1'"))
58 .arg(val)),
59 std::forward<Value>(val));
60 }
61};
62
64 bool operator()(const QString& value) const noexcept {
65 return QRegularExpression("\\A[_0-9A-Z]{1,40}\\z")
66 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
67 .hasMatch();
68 }
69};
70
83 type_safe::constrained_type<QString, AttributeKeyConstraint,
85
86inline bool operator==(const AttributeKey& lhs, const QString& rhs) noexcept {
87 return (*lhs) == rhs;
88}
89inline bool operator==(const QString& lhs, const AttributeKey& rhs) noexcept {
90 return lhs == (*rhs);
91}
92inline bool operator!=(const AttributeKey& lhs, const QString& rhs) noexcept {
93 return (*lhs) != rhs;
94}
95inline bool operator!=(const QString& lhs, const AttributeKey& rhs) noexcept {
96 return lhs != (*rhs);
97}
98
99template <>
100inline std::unique_ptr<SExpression> serialize(const AttributeKey& obj) {
101 return SExpression::createString(*obj);
102}
103
104template <>
106 return AttributeKey(node.getValue()); // can throw
107}
108
109inline QDataStream& operator<<(QDataStream& stream, const AttributeKey& obj) {
110 stream << *obj;
111 return stream;
112}
113
114inline QDebug operator<<(QDebug stream, const AttributeKey& obj) {
115 stream << QString("AttributeKey('%1')").arg(*obj);
116 return stream;
117}
118
120 QtCompat::Hash seed = 0) noexcept {
121 return ::qHash(*key, seed);
122}
123
124/*******************************************************************************
125 * End of File
126 ******************************************************************************/
127
128} // namespace librepcb
129
130#endif
uint Hash
Return type of Qt's qHash() function.
Definition: qtcompat.h:58
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:242
Definition: occmodel.cpp:77
type_safe::constrained_type< QString, AttributeKeyConstraint, AttributeKeyVerifier > AttributeKey
Definition: attributekey.h:84
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:86
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition: attributekey.h:100
static QString cleanAttributeKey(const QString &userInput) noexcept
Definition: attributekey.h:43
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:92
AttributeKey deserialize(const SExpression &node)
Definition: attributekey.h:105
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
Definition: attributekey.h:63
bool operator()(const QString &value) const noexcept
Definition: attributekey.h:64
Definition: attributekey.h:48
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: attributekey.h:50