LibrePCB Developers Documentation
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 "../qtcompat.h"
28#include "../serialization/sexpression.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 SimpleString
41 ******************************************************************************/
42
44 template <typename Value, typename Predicate>
45 static constexpr auto verify(Value&& val, const Predicate& p) ->
46 typename std::decay<Value>::type {
47 return p(val)
48 ? std::forward<Value>(val)
49 : (throw RuntimeError(__FILE__, __LINE__,
50 QString(QCoreApplication::translate(
51 "SimpleString", "Invalid name: '%1'"))
52 .arg(val)),
53 std::forward<Value>(val));
54 }
55};
56
58 bool operator()(const QString& value) const noexcept {
59 if (value != value.trimmed()) return false;
60 foreach (const QChar& c, value) {
61 if (!c.isPrint()) return false;
62 }
63 return true;
64 }
65};
66
79 type_safe::constrained_type<QString, SimpleStringConstraint,
81
82inline bool operator==(const SimpleString& lhs, const QString& rhs) noexcept {
83 return (*lhs) == rhs;
84}
85inline bool operator==(const QString& lhs, const SimpleString& rhs) noexcept {
86 return lhs == (*rhs);
87}
88inline bool operator!=(const SimpleString& lhs, const QString& rhs) noexcept {
89 return (*lhs) != rhs;
90}
91inline bool operator!=(const QString& lhs, const SimpleString& rhs) noexcept {
92 return lhs != (*rhs);
93}
94inline QString operator%(const SimpleString& lhs, const QString& rhs) noexcept {
95 return (*lhs) % rhs;
96}
97inline QString operator%(const QString& lhs, const SimpleString& rhs) noexcept {
98 return lhs % (*rhs);
99}
101 const SimpleString& rhs) noexcept {
102 return SimpleString((*lhs) % (*rhs)); // always safe, will not throw
103}
104
106 const QString& userInput) noexcept {
107 QString ret = userInput.simplified();
108 for (int i = ret.length() - 1; i >= 0; --i) {
109 if (!ret[i].isPrint()) {
110 ret.remove(i, 1);
111 }
112 }
113 return SimpleString(ret);
114}
115
116template <>
117inline std::unique_ptr<SExpression> serialize(const SimpleString& obj) {
118 return SExpression::createString(*obj);
119}
120
121template <>
123 return SimpleString(node.getValue()); // can throw
124}
125
126inline QDataStream& operator<<(QDataStream& stream, const SimpleString& obj) {
127 stream << *obj;
128 return stream;
129}
130
131inline QDebug operator<<(QDebug stream, const SimpleString& obj) {
132 stream << QString("SimpleString('%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:405
const QString & getValue() const
Definition: sexpression.cpp:71
Definition: occmodel.cpp:77
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:86
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
QtCompat::Hash qHash(const SimpleString &key, QtCompat::Hash seed=0) noexcept
Definition: simplestring.h:136
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:92
static SimpleString cleanSimpleString(const QString &userInput) noexcept
Definition: simplestring.h:105
type_safe::constrained_type< QString, SimpleStringConstraint, SimpleStringVerifier > SimpleString
Definition: simplestring.h:80
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: simplestring.h:57
bool operator()(const QString &value) const noexcept
Definition: simplestring.h:58
Definition: simplestring.h:43
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: simplestring.h:45