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