LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
graphicslayername.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_GRAPHICSLAYERNAME_H
21 #define LIBREPCB_GRAPHICSLAYERNAME_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/sexpression.h"
27 
28 #include <type_safe/constrained_type.hpp>
29 
30 #include <QtCore>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 /*******************************************************************************
38  * Class GraphicsLayerName
39  ******************************************************************************/
40 
42  template <typename Value, typename Predicate>
43  static constexpr auto verify(Value&& val, const Predicate& p) ->
44  typename std::decay<Value>::type {
45  return p(val) ? std::forward<Value>(val)
46  : (throw RuntimeError(
47  __FILE__, __LINE__,
48  QString(QApplication::translate(
49  "GraphicsLayerName",
50  "Not a valid graphics layer name: '%1'"))
51  .arg(val)),
52  std::forward<Value>(val));
53  }
54 };
55 
57  bool operator()(const QString& value) const noexcept {
58  return QRegularExpression("^[a-z][_0-9a-z]{0,39}$")
59  .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
60  .hasMatch();
61  }
62 };
63 
76 using GraphicsLayerName =
77  type_safe::constrained_type<QString, GraphicsLayerNameConstraint,
79 
80 inline bool operator==(const GraphicsLayerName& lhs,
81  const QString& rhs) noexcept {
82  return (*lhs) == rhs;
83 }
84 inline bool operator==(const QString& lhs,
85  const GraphicsLayerName& rhs) noexcept {
86  return lhs == (*rhs);
87 }
88 inline bool operator!=(const GraphicsLayerName& lhs,
89  const QString& rhs) noexcept {
90  return (*lhs) != rhs;
91 }
92 inline bool operator!=(const QString& lhs,
93  const GraphicsLayerName& rhs) noexcept {
94  return lhs != (*rhs);
95 }
96 
97 template <>
99  return SExpression::createToken(*obj);
100 }
101 
102 template <>
104  bool throwIfEmpty) {
105  QString str = sexpr.getStringOrToken(throwIfEmpty);
106  return GraphicsLayerName(str); // can throw
107 }
108 
109 inline QDataStream& operator<<(QDataStream& stream,
110  const GraphicsLayerName& obj) {
111  stream << *obj;
112  return stream;
113 }
114 
115 inline QDebug operator<<(QDebug stream, const GraphicsLayerName& obj) {
116  stream << QString("GraphicsLayerName('%1'')").arg(*obj);
117  return stream;
118 }
119 
120 inline uint qHash(const GraphicsLayerName& key, uint seed = 0) noexcept {
121  return ::qHash(*key, seed);
122 }
123 
124 /*******************************************************************************
125  * End of File
126  ******************************************************************************/
127 
128 } // namespace librepcb
129 
130 #endif // LIBREPCB_GRAPHICSLAYERNAME_H
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:114
Definition: graphicslayername.h:41
bool operator()(const QString &value) const noexcept
Definition: graphicslayername.h:57
type_safe::constrained_type< QString, GraphicsLayerNameConstraint, GraphicsLayerNameVerifier > GraphicsLayerName
Definition: graphicslayername.h:78
Definition: graphicslayername.h:56
uint qHash(const GraphicsLayerName &key, uint seed=0) noexcept
Definition: graphicslayername.h:120
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:274
The RuntimeError class.
Definition: exceptions.h:219
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:85
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:91
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: graphicslayername.h:43
The SExpression class.
Definition: sexpression.h:60
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:124