KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_netchain.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 KiCad Developers
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (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 <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef SCH_NETCHAIN_H
21#define SCH_NETCHAIN_H
22
23#include <cstdint>
24#include <set>
25#include <type_traits>
26#include <utility>
27#include <wx/string.h>
28#include <gal/color4d.h>
29#include <kiid.h>
30
36{
37public:
40 static constexpr wxStringCharType SYNTHETIC_NET_PREFIX[] = wxS( "__SG_" );
41
42 static wxString MakeKey( const wxString& aName, uint32_t aComponent );
43
45 static bool IsPersistableNet( const wxString& aNet )
46 {
47 return !aNet.IsEmpty() && !aNet.StartsWith( SYNTHETIC_NET_PREFIX );
48 }
49
51
52 friend void swap( SCH_NETCHAIN& aLeft, SCH_NETCHAIN& aRight ) noexcept
53 {
54 const auto swapMember = []<typename T>( T& a, T& b ) noexcept
55 {
56 static_assert( std::is_nothrow_swappable_v<T> );
57 using std::swap;
58 swap( a, b );
59 };
60
61 swapMember( aLeft.m_name, aRight.m_name );
62 swapMember( aLeft.m_nets, aRight.m_nets );
63 swapMember( aLeft.m_symbols, aRight.m_symbols );
64 swapMember( aLeft.m_terminalPins, aRight.m_terminalPins );
65 swapMember( aLeft.m_terminalPaths, aRight.m_terminalPaths );
66 swapMember( aLeft.m_terminalRef, aRight.m_terminalRef );
67 swapMember( aLeft.m_terminalPinNum, aRight.m_terminalPinNum );
68 swapMember( aLeft.m_netClass, aRight.m_netClass );
69 swapMember( aLeft.m_color, aRight.m_color );
70 }
71
72 void SetName( const wxString& aName ) { m_name = aName; }
73 const wxString& GetName() const { return m_name; }
74
75 static bool IsValidName( const wxString& aName )
76 {
77 if( aName.IsEmpty() )
78 return false;
79
80 for( wxUniChar c : aName )
81 if( c == '"' || c == '\'' || c == '(' || c == ')' || c == ' ' )
82 return false;
83
84 return true;
85 }
86
87 void AddNet( const wxString& aNet )
88 {
89 m_nets.insert( aNet );
90 }
91
92 void RemoveNet( const wxString& aNet )
93 {
94 m_nets.erase( aNet );
95 }
96
97 void ReplaceNets( const std::set<wxString>& aNew )
98 {
99 m_nets = aNew;
100 }
101
102 // Track a symbol that participates in this chain (2-pin passthrough component).
103 void AddSymbol( class SCH_SYMBOL* aSymbol )
104 {
105 m_symbols.insert( aSymbol );
106 }
107
108 const std::set<class SCH_SYMBOL*>& GetSymbols() const { return m_symbols; }
109
110 void AbsorbSymbolsFrom( const SCH_NETCHAIN& aOther )
111 {
112 m_symbols.insert( aOther.m_symbols.begin(), aOther.m_symbols.end() );
113 }
114
115 // The symbol set holds non-owning raw pointers to schematic items. Callers must invoke
116 // this before any pass that may free those items.
118 {
119 m_symbols.clear();
120 }
121
122 const std::set<wxString>& GetNets() const { return m_nets; }
123
124 void SetTerminalPins( const KIID& aPinA, const KIID& aPinB )
125 {
126 m_terminalPins[0] = aPinA;
127 m_terminalPins[1] = aPinB;
128 }
129
130 const KIID& GetTerminalPinA() const { return m_terminalPins[0]; }
131 const KIID& GetTerminalPinB() const { return m_terminalPins[1]; }
132
133 const KIID_PATH& GetTerminalPath( int aIdx ) const { return m_terminalPaths[aIdx]; }
134
135 void SetTerminalPaths( const KIID_PATH& aPathA, const KIID_PATH& aPathB )
136 {
137 m_terminalPaths[0] = aPathA;
138 m_terminalPaths[1] = aPathB;
139 }
140
141 bool IsTerminal( const KIID& aPin, const KIID_PATH& aSheet ) const
142 {
143 return ( m_terminalPins[0] == aPin && m_terminalPaths[0] == aSheet )
144 || ( m_terminalPins[1] == aPin && m_terminalPaths[1] == aSheet );
145 }
146
152 void SetNetClass( const wxString& aNetClass ) { m_netClass = aNetClass; }
153 const wxString& GetNetClass() const { return m_netClass; }
154
161 void SetColor( const KIGFX::COLOR4D& aColor ) { m_color = aColor; }
162 const KIGFX::COLOR4D& GetColor() const { return m_color; }
163
164 void SetTerminalRefs( const wxString& aRefA, const wxString& aPinA, const wxString& aRefB, const wxString& aPinB )
165 {
166 m_terminalRef[0] = aRefA;
167 m_terminalPinNum[0] = aPinA;
168 m_terminalRef[1] = aRefB;
169 m_terminalPinNum[1] = aPinB;
170 }
171
172 const wxString& GetTerminalRef( int aIdx ) const
173 {
174 wxASSERT( aIdx >= 0 && aIdx < 2 );
175 return m_terminalRef[aIdx];
176 }
177
178 const wxString& GetTerminalPinNum( int aIdx ) const
179 {
180 wxASSERT( aIdx >= 0 && aIdx < 2 );
181 return m_terminalPinNum[aIdx];
182 }
183
184private:
185 wxString m_name;
186 std::set<wxString> m_nets;
187 std::set<class SCH_SYMBOL*> m_symbols;
190 wxString m_terminalRef[2];
191 wxString m_terminalPinNum[2];
192 wxString m_netClass;
194};
195
196#endif
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:399
Definition kiid.h:46
const KIID & GetTerminalPinB() const
static wxString MakeKey(const wxString &aName, uint32_t aComponent)
const std::set< wxString > & GetNets() const
void AddSymbol(class SCH_SYMBOL *aSymbol)
std::set< wxString > m_nets
const wxString & GetTerminalRef(int aIdx) const
const wxString & GetNetClass() const
const KIGFX::COLOR4D & GetColor() const
const wxString & GetName() const
KIID_PATH m_terminalPaths[2]
static bool IsValidName(const wxString &aName)
KIID m_terminalPins[2]
wxString m_name
wxString m_terminalPinNum[2]
std::set< class SCH_SYMBOL * > m_symbols
KIGFX::COLOR4D m_color
void SetTerminalPins(const KIID &aPinA, const KIID &aPinB)
void ClearSymbols()
friend void swap(SCH_NETCHAIN &aLeft, SCH_NETCHAIN &aRight) noexcept
void RemoveNet(const wxString &aNet)
wxString m_netClass
void ReplaceNets(const std::set< wxString > &aNew)
const KIID_PATH & GetTerminalPath(int aIdx) const
const std::set< class SCH_SYMBOL * > & GetSymbols() const
void SetNetClass(const wxString &aNetClass)
Net chains may override the netclass applied to every member net.
static constexpr wxStringCharType SYNTHETIC_NET_PREFIX[]
Prefix used when synthesising net names for unnamed subgraphs.
wxString m_terminalRef[2]
void AbsorbSymbolsFrom(const SCH_NETCHAIN &aOther)
void SetTerminalPaths(const KIID_PATH &aPathA, const KIID_PATH &aPathB)
static bool IsPersistableNet(const wxString &aNet)
Synthetic keys do not survive a reload, so only named nets are written out.
const wxString & GetTerminalPinNum(int aIdx) const
void SetColor(const KIGFX::COLOR4D &aColor)
Optional display color for the chain.
const KIID & GetTerminalPinA() const
bool IsTerminal(const KIID &aPin, const KIID_PATH &aSheet) const
void AddNet(const wxString &aNet)
void SetName(const wxString &aName)
void SetTerminalRefs(const wxString &aRefA, const wxString &aPinA, const wxString &aRefB, const wxString &aPinB)
Schematic symbol object.
Definition sch_symbol.h:75