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
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 "AttributeKey", "Invalid attribute key: '%1'"))
52 .arg(val)),
53 std::forward<Value>(val));
54 }
55};
56
58 bool operator()(const QString& value) const noexcept {
59 return QRegularExpression("\\A[_0-9A-Z]{1,40}\\z")
60 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
61 .hasMatch();
62 }
63};
64
77 type_safe::constrained_type<QString, AttributeKeyConstraint,
79
80inline bool operator==(const AttributeKey& lhs, const QString& rhs) noexcept {
81 return (*lhs) == rhs;
82}
83inline bool operator==(const QString& lhs, const AttributeKey& rhs) noexcept {
84 return lhs == (*rhs);
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}
92
93template <>
94inline std::unique_ptr<SExpression> serialize(const AttributeKey& obj) {
95 return SExpression::createString(*obj);
96}
97
98template <>
100 return AttributeKey(node.getValue()); // can throw
101}
102
103inline QDataStream& operator<<(QDataStream& stream, const AttributeKey& obj) {
104 stream << *obj;
105 return stream;
106}
107
108inline QDebug operator<<(QDebug stream, const AttributeKey& obj) {
109 stream << QString("AttributeKey('%1')").arg(*obj);
110 return stream;
111}
112
113inline std::size_t qHash(const AttributeKey& key,
114 std::size_t seed = 0) noexcept {
115 return ::qHash(*key, seed);
116}
117
118inline static QString cleanAttributeKey(const QString& userInput) noexcept {
120 userInput, QRegularExpression("[^_0-9A-Z]"), true, false, true, "_", 40);
121}
122
123inline static std::optional<AttributeKey> parseAttributeKey(
124 const QString& name) noexcept {
125 return AttributeKeyConstraint()(name) ? AttributeKey(name)
126 : std::optional<AttributeKey>();
127}
128
129/*******************************************************************************
130 * End of File
131 ******************************************************************************/
132
133} // namespace librepcb
134
135#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
type_safe::constrained_type< QString, AttributeKeyConstraint, AttributeKeyVerifier > AttributeKey
Definition attributekey.h:78
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
static QString cleanAttributeKey(const QString &userInput) noexcept
Definition attributekey.h:118
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:86
static std::optional< AttributeKey > parseAttributeKey(const QString &name) noexcept
Definition attributekey.h:123
AttributeKey deserialize(const SExpression &node)
Definition attributekey.h:99
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:103
Definition attributekey.h:57
bool operator()(const QString &value) const noexcept
Definition attributekey.h:58
Definition attributekey.h:42
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition attributekey.h:44