LibrePCB Developers Documentation
Loading...
Searching...
No Matches
simplestring.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_SIMPLESTRING_H
21#define LIBREPCB_CORE_SIMPLESTRING_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../exceptions.h"
27#include "../serialization/sexpression.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 SimpleString
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(__FILE__, __LINE__,
49 QString(QCoreApplication::translate(
50 "SimpleString", "Invalid name: '%1'"))
51 .arg(val)),
52 std::forward<Value>(val));
53 }
54};
55
57 bool operator()(const QString& value) const noexcept {
58 if (value != value.trimmed()) return false;
59 foreach (const QChar& c, value) {
60 if (!c.isPrint()) return false;
61 }
62 return true;
63 }
64};
65
78 type_safe::constrained_type<QString, SimpleStringConstraint,
80
81inline bool operator==(const SimpleString& lhs, const QString& rhs) noexcept {
82 return (*lhs) == rhs;
83}
84inline bool operator==(const QString& lhs, const SimpleString& rhs) noexcept {
85 return lhs == (*rhs);
86}
87inline bool operator!=(const SimpleString& lhs, const QString& rhs) noexcept {
88 return (*lhs) != rhs;
89}
90inline bool operator!=(const QString& lhs, const SimpleString& rhs) noexcept {
91 return lhs != (*rhs);
92}
93inline QString operator%(const SimpleString& lhs, const QString& rhs) noexcept {
94 return (*lhs) % rhs;
95}
96inline QString operator%(const QString& lhs, const SimpleString& rhs) noexcept {
97 return lhs % (*rhs);
98}
100 const SimpleString& rhs) noexcept {
101 return SimpleString((*lhs) % (*rhs)); // always safe, will not throw
102}
103
105 const QString& userInput) noexcept {
106 QString ret = userInput.simplified();
107 for (int i = ret.length() - 1; i >= 0; --i) {
108 if (!ret[i].isPrint()) {
109 ret.remove(i, 1);
110 }
111 }
112 return SimpleString(ret);
113}
114
115template <>
116inline std::unique_ptr<SExpression> serialize(const SimpleString& obj) {
117 return SExpression::createString(*obj);
118}
119
120template <>
122 return SimpleString(node.getValue()); // can throw
123}
124
125inline QDataStream& operator<<(QDataStream& stream, const SimpleString& obj) {
126 stream << *obj;
127 return stream;
128}
129
130inline QDebug operator<<(QDebug stream, const SimpleString& obj) {
131 stream << QString("SimpleString('%1')").arg(*obj);
132 return stream;
133}
134
135inline std::size_t qHash(const SimpleString& 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
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
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
static SimpleString cleanSimpleString(const QString &userInput) noexcept
Definition simplestring.h:104
type_safe::constrained_type< QString, SimpleStringConstraint, SimpleStringVerifier > SimpleString
Definition simplestring.h:79
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 simplestring.h:56
bool operator()(const QString &value) const noexcept
Definition simplestring.h:57
Definition simplestring.h:42
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition simplestring.h:44