LibrePCB Developers Documentation
Loading...
Searching...
No Matches
fileproofname.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_FILEPROOFNAME_H
21#define LIBREPCB_CORE_FILEPROOFNAME_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 FileProofName
41 ******************************************************************************/
42
43inline static QString cleanFileProofName(const QString& userInput) noexcept {
44 return Toolbox::cleanUserInputString(userInput,
45 QRegularExpression("[^-a-zA-Z0-9_+().]"),
46 true, false, false, "-", 20);
47}
48
50 template <typename Value, typename Predicate>
51 static constexpr auto verify(Value&& val, const Predicate& p) ->
52 typename std::decay<Value>::type {
53 return p(val) ? std::forward<Value>(val)
54 : (throw RuntimeError(
55 __FILE__, __LINE__,
56 QString(QCoreApplication::translate(
57 "FileProofName", "Invalid name: '%1'"))
58 .arg(val)),
59 std::forward<Value>(val));
60 }
61};
62
64 static QRegularExpression regex() noexcept {
65 return QRegularExpression("\\A[-a-zA-Z0-9_+().]{1,20}\\z");
66 }
67
68 bool operator()(const QString& value) const noexcept {
69 return regex()
70 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
71 .hasMatch();
72 }
73};
74
87 type_safe::constrained_type<QString, FileProofNameConstraint,
89
90inline bool operator==(const FileProofName& lhs, const QString& rhs) noexcept {
91 return (*lhs) == rhs;
92}
93inline bool operator==(const QString& lhs, const FileProofName& rhs) noexcept {
94 return lhs == (*rhs);
95}
96inline bool operator!=(const FileProofName& lhs, const QString& rhs) noexcept {
97 return (*lhs) != rhs;
98}
99inline bool operator!=(const QString& lhs, const FileProofName& rhs) noexcept {
100 return lhs != (*rhs);
101}
102inline QString operator%(const FileProofName& lhs,
103 const QString& rhs) noexcept {
104 return (*lhs) % rhs;
105}
106inline QString operator%(const QString& lhs,
107 const FileProofName& rhs) noexcept {
108 return lhs % (*rhs);
109}
111 const FileProofName& rhs) noexcept {
112 return FileProofName((*lhs) % (*rhs)); // always safe, will not throw
113}
114
115template <>
116inline std::unique_ptr<SExpression> serialize(const FileProofName& obj) {
117 return SExpression::createString(*obj);
118}
119
120template <>
122 return FileProofName(node.getValue()); // can throw
123}
124
125inline QDataStream& operator<<(QDataStream& stream, const FileProofName& obj) {
126 stream << *obj;
127 return stream;
128}
129
130inline QDebug operator<<(QDebug stream, const FileProofName& obj) {
131 stream << QString("FileProofName('%1')").arg(*obj);
132 return stream;
133}
134
135inline std::size_t qHash(const FileProofName& key,
136 std::size_t seed = 0) noexcept {
137 return ::qHash(*key, seed);
138}
139
140/*******************************************************************************
141 * End of File
142 ******************************************************************************/
143
144} // namespace librepcb
145
146#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
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:85
static QString cleanFileProofName(const QString &userInput) noexcept
Definition fileproofname.h:43
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition attributekey.h:99
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:91
type_safe::constrained_type< QString, FileProofNameConstraint, FileProofNameVerifier > FileProofName
Definition fileproofname.h:88
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 fileproofname.h:63
bool operator()(const QString &value) const noexcept
Definition fileproofname.h:68
static QRegularExpression regex() noexcept
Definition fileproofname.h:64
Definition fileproofname.h:49
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition fileproofname.h:51