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 
42 class SignalRole final {
43  Q_DECLARE_TR_FUNCTIONS(SignalRole)
44 
45 public:
46  // Constructors / Destructor
47  SignalRole() noexcept;
48  SignalRole(const SignalRole& other) noexcept;
49  ~SignalRole() noexcept;
50 
51  // Getters
52 
58  QString toStr() const noexcept { return mRole; }
59 
65  const QString& getNameTr() const noexcept { return mName; }
66 
67  // Operator Overloadings
68  SignalRole& operator=(const SignalRole& rhs) noexcept;
69  bool operator==(const SignalRole& rhs) const noexcept {
70  return mRole == rhs.mRole;
71  }
72  bool operator!=(const SignalRole& rhs) const noexcept {
73  return mRole != rhs.mRole;
74  }
75 
76  // Static Methods
77 
83  static const QList<SignalRole>& getAllRoles() noexcept;
84 
86  static const SignalRole& passive() noexcept {
87  static SignalRole role("passive", tr("Passive"));
88  return role;
89  }
90 
92  static const SignalRole& power() noexcept {
93  static SignalRole role("power", tr("Power"));
94  return role;
95  }
96 
98  static const SignalRole& input() noexcept {
99  static SignalRole role("input", tr("Input"));
100  return role;
101  }
102 
104  static const SignalRole& output() noexcept {
105  static SignalRole role("output", tr("Output"));
106  return role;
107  }
108 
110  static const SignalRole& inout() noexcept {
111  static SignalRole role("inout", tr("I/O"));
112  return role;
113  }
114 
116  static const SignalRole& opendrain() noexcept {
117  static SignalRole role("opendrain", tr("Open Drain"));
118  return role;
119  }
120 
121 private: // Methods
122  SignalRole(const QString& role, const QString& name) noexcept;
123 
124 private: // Data
125  QString mRole;
126  QString mName;
127 };
128 
129 /*******************************************************************************
130  * Non-Member Functions
131  ******************************************************************************/
132 
133 template <>
135  return SExpression::createToken(obj.toStr());
136 }
137 
138 template <>
140  bool throwIfEmpty) {
141  QString str = sexpr.getStringOrToken(throwIfEmpty);
142  foreach (const SignalRole& role, SignalRole::getAllRoles()) {
143  if (role.toStr() == str) {
144  return role;
145  }
146  }
147  throw RuntimeError(
148  __FILE__, __LINE__,
149  QString(SignalRole::tr("Invalid signal role: \"%1\"")).arg(str));
150 }
151 
152 /*******************************************************************************
153  * End of File
154  ******************************************************************************/
155 
156 } // namespace librepcb
157 
158 #endif // LIBREPCB_SIGNALROLE_H
static const SignalRole & output() noexcept
Output Pins.
Definition: signalrole.h:104
bool operator==(const SignalRole &rhs) const noexcept
Definition: signalrole.h:69
static const SignalRole & inout() noexcept
Input/Output Pins.
Definition: signalrole.h:110
QString toStr() const noexcept
Serialize this object into a string.
Definition: signalrole.h:58
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
QString mRole
used for serialization (DO NOT MODIFY VALUES!)
Definition: signalrole.h:125
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
The SignalRole class provides all supported electrical signal roles.
Definition: signalrole.h:42
bool operator!=(const SignalRole &rhs) const noexcept
Definition: signalrole.h:72
const QString & getNameTr() const noexcept
Get the name of the SignalRole (human readable and translated)
Definition: signalrole.h:65
The RuntimeError class.
Definition: exceptions.h:219
static const SignalRole & input() noexcept
Input Pins.
Definition: signalrole.h:98
QString mName
human readable (translated)
Definition: signalrole.h:126
static const SignalRole & power() noexcept
Power Pins (GND, VCC, VSS,... of Devices)
Definition: signalrole.h:92
static const SignalRole & opendrain() noexcept
Open Collector / Open Drain Pins.
Definition: signalrole.h:116
~SignalRole() noexcept
Definition: signalrole.cpp:47
static const SignalRole & passive() noexcept
Passive Pins (R, C, L)
Definition: signalrole.h:86
SignalRole & operator=(const SignalRole &rhs) noexcept
Definition: signalrole.cpp:54
SignalRole() noexcept
Definition: signalrole.cpp:36
The SExpression class.
Definition: sexpression.h:57
static const QList< SignalRole > & getAllRoles() noexcept
Get a list of all available signal roles.
Definition: signalrole.cpp:64