LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
signalrole.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_SIGNALROLE_H
21 #define LIBREPCB_SIGNALROLE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "fileio/sexpression.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class SignalRole
37  ******************************************************************************/
38 
45 class SignalRole final {
46  Q_DECLARE_TR_FUNCTIONS(SignalRole)
47 
48 public:
49  // Constructors / Destructor
50  SignalRole() noexcept;
51  SignalRole(const SignalRole& other) noexcept;
52  ~SignalRole() noexcept;
53 
54  // Getters
55 
61  QString toStr() const noexcept { return mRole; }
62 
68  const QString& getNameTr() const noexcept { return mName; }
69 
70  // Operator Overloadings
71  SignalRole& operator=(const SignalRole& rhs) noexcept;
72  bool operator==(const SignalRole& rhs) const noexcept {
73  return mRole == rhs.mRole;
74  }
75  bool operator!=(const SignalRole& rhs) const noexcept {
76  return mRole != rhs.mRole;
77  }
78 
79  // Static Methods
80 
86  static const QList<SignalRole>& getAllRoles() noexcept;
87 
89  static const SignalRole& passive() noexcept {
90  static SignalRole role("passive", tr("Passive"));
91  return role;
92  }
93 
95  static const SignalRole& power() noexcept {
96  static SignalRole role("power", tr("Power"));
97  return role;
98  }
99 
101  static const SignalRole& input() noexcept {
102  static SignalRole role("input", tr("Input"));
103  return role;
104  }
105 
107  static const SignalRole& output() noexcept {
108  static SignalRole role("output", tr("Output"));
109  return role;
110  }
111 
113  static const SignalRole& inout() noexcept {
114  static SignalRole role("inout", tr("I/O"));
115  return role;
116  }
117 
119  static const SignalRole& opendrain() noexcept {
120  static SignalRole role("opendrain", tr("Open Drain"));
121  return role;
122  }
123 
124 private: // Methods
125  SignalRole(const QString& role, const QString& name) noexcept;
126 
127 private: // Data
128  QString mRole;
129  QString mName;
130 };
131 
132 /*******************************************************************************
133  * Non-Member Functions
134  ******************************************************************************/
135 
136 template <>
138  return SExpression::createToken(obj.toStr());
139 }
140 
141 template <>
143  bool throwIfEmpty) {
144  QString str = sexpr.getStringOrToken(throwIfEmpty);
145  foreach (const SignalRole& role, SignalRole::getAllRoles()) {
146  if (role.toStr() == str) {
147  return role;
148  }
149  }
150  throw RuntimeError(
151  __FILE__, __LINE__,
152  QString(SignalRole::tr("Invalid signal role: \"%1\"")).arg(str));
153 }
154 
155 /*******************************************************************************
156  * End of File
157  ******************************************************************************/
158 
159 } // namespace librepcb
160 
161 #endif // LIBREPCB_SIGNALROLE_H
static const SignalRole & output() noexcept
Output Pins.
Definition: signalrole.h:107
bool operator==(const SignalRole &rhs) const noexcept
Definition: signalrole.h:72
static const SignalRole & inout() noexcept
Input/Output Pins.
Definition: signalrole.h:113
QString toStr() const noexcept
Serialize this object into a string.
Definition: signalrole.h:61
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
QString mRole
used for serialization (DO NOT MODIFY VALUES!)
Definition: signalrole.h:128
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:274
The SignalRole class provides all supported electrical signal roles.
Definition: signalrole.h:45
bool operator!=(const SignalRole &rhs) const noexcept
Definition: signalrole.h:75
const QString & getNameTr() const noexcept
Get the name of the SignalRole (human readable and translated)
Definition: signalrole.h:68
The RuntimeError class.
Definition: exceptions.h:219
static const SignalRole & input() noexcept
Input Pins.
Definition: signalrole.h:101
QString mName
human readable (translated)
Definition: signalrole.h:129
static const SignalRole & power() noexcept
Power Pins (GND, VCC, VSS,... of Devices)
Definition: signalrole.h:95
static const SignalRole & opendrain() noexcept
Open Collector / Open Drain Pins.
Definition: signalrole.h:119
~SignalRole() noexcept
Definition: signalrole.cpp:47
static const SignalRole & passive() noexcept
Passive Pins (R, C, L)
Definition: signalrole.h:89
SignalRole & operator=(const SignalRole &rhs) noexcept
Definition: signalrole.cpp:54
SignalRole() noexcept
Definition: signalrole.cpp:36
The SExpression class.
Definition: sexpression.h:60
static const QList< SignalRole > & getAllRoles() noexcept
Get a list of all available signal roles.
Definition: signalrole.cpp:64