LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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 "../serialization/sexpression.h"
27#include "../utils/toolbox.h"
28
29#include <type_safe/constrained_type.hpp>
30
31#include <QtCore>
32
33/*******************************************************************************
34 * Namespace / Forward Declarations
35 ******************************************************************************/
36namespace librepcb {
37
38/*******************************************************************************
39 * Class AttributeKey
40 ******************************************************************************/
41
42inline static QString cleanAttributeKey(const QString& userInput) noexcept {
44 userInput, QRegularExpression("[^_0-9A-Z]"), true, false, true, "_", 40);
45}
46
48 template <typename Value, typename Predicate>
49 static constexpr auto verify(Value&& val, const Predicate& p) ->
50 typename std::decay<Value>::type {
51 return p(val)
52 ? std::forward<Value>(val)
53 : (throw RuntimeError(
54 __FILE__, __LINE__,
55 QString(QCoreApplication::translate(
56 "AttributeKey", "Invalid attribute key: '%1'"))
57 .arg(val)),
58 std::forward<Value>(val));
59 }
60};
61
63 bool operator()(const QString& value) const noexcept {
64 return QRegularExpression("\\A[_0-9A-Z]{1,40}\\z")
65 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
66 .hasMatch();
67 }
68};
69
82 type_safe::constrained_type<QString, AttributeKeyConstraint,
84
85inline bool operator==(const AttributeKey& lhs, const QString& rhs) noexcept {
86 return (*lhs) == rhs;
87}
88inline bool operator==(const QString& lhs, const AttributeKey& rhs) noexcept {
89 return lhs == (*rhs);
90}
91inline bool operator!=(const AttributeKey& lhs, const QString& rhs) noexcept {
92 return (*lhs) != rhs;
93}
94inline bool operator!=(const QString& lhs, const AttributeKey& rhs) noexcept {
95 return lhs != (*rhs);
96}
97
98template <>
99inline std::unique_ptr<SExpression> serialize(const AttributeKey& obj) {
100 return SExpression::createString(*obj);
101}
102
103template <>
105 return AttributeKey(node.getValue()); // can throw
106}
107
108inline QDataStream& operator<<(QDataStream& stream, const AttributeKey& obj) {
109 stream << *obj;
110 return stream;
111}
112
113inline QDebug operator<<(QDebug stream, const AttributeKey& obj) {
114 stream << QString("AttributeKey('%1')").arg(*obj);
115 return stream;
116}
117
118inline std::size_t qHash(const AttributeKey& key,
119 std::size_t seed = 0) noexcept {
120 return ::qHash(*key, seed);
121}
122
123/*******************************************************************************
124 * End of File
125 ******************************************************************************/
126
127} // namespace librepcb
128
129#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
type_safe::constrained_type< QString, AttributeKeyConstraint, AttributeKeyVerifier > AttributeKey
Definition attributekey.h:83
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
static QString cleanAttributeKey(const QString &userInput) noexcept
Definition attributekey.h:42
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
Definition attributekey.h:62
bool operator()(const QString &value) const noexcept
Definition attributekey.h:63
Definition attributekey.h:47
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition attributekey.h:49