KiCad PCB EDA Suite
Loading...
Searching...
No Matches
conn_bus.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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#pragma once
21
22#include "conn_cache.h"
23#include <wx/string.h>
24
25#include <cstddef>
26#include <map>
27#include <memory>
28#include <optional>
29#include <set>
30#include <utility>
31#include <vector>
32
33namespace SCH_CONNECTIVITY
34{
40using BUS_ALIASES = std::map<wxString, std::vector<wxString>>;
41
48{
49 enum class SHAPE
50 {
53 };
54
56 struct LEAF
57 {
58 wxString name;
59 wxString localName;
60
65 std::vector<wxString> groupPath;
66
67 bool operator==( const LEAF& ) const = default;
68 };
69
71 struct NODE
72 {
73 enum class KIND
74 {
78 };
79
82 wxString text;
83 wxString prefix;
84 std::vector<NODE> members;
86 std::optional<size_t> leaf;
87
88 bool operator==( const NODE& ) const = default;
89 };
90
92 wxString prefix;
97 size_t prefixEnd = 0;
98 std::vector<LEAF> leaves;
104 std::set<wxString> aliasesUsed;
109 bool operator==( const BUS_SCHEMA& aOther ) const
110 {
111 return shape == aOther.shape && prefix == aOther.prefix && leaves == aOther.leaves;
112 }
113
121 static std::optional<BUS_SCHEMA> Parse( const wxString& aText, const BUS_ALIASES& aAliases = {} );
122};
123
130{
131public:
132 struct RESULT
133 {
134 std::shared_ptr<const BUS_SCHEMA> schema;
136 std::shared_ptr<const BUS_SCHEMA::NODE> tree;
137 std::set<wxString> aliasesUsed;
138 bool operator==( const RESULT& aOther ) const
139 {
140 return aliasesUsed == aOther.aliasesUsed
141 && ( schema == aOther.schema || ( schema && aOther.schema && *schema == *aOther.schema ) )
142 && ( tree == aOther.tree || ( tree && aOther.tree && *tree == *aOther.tree ) );
143 }
144 };
145
147
148 explicit BUS_PARSE_CACHE( CACHE_VERSIONS& aVersions ) :
149 m_cache( aVersions )
150 {
151 }
152
160 bool SetAliases( const BUS_ALIASES& aAliases );
161
163 void Retain( const std::set<wxString>& aLive );
164
166 const ENTRY& Parse( const wxString& aText );
167
169 std::shared_ptr<const BUS_SCHEMA::NODE> FindTree( const wxString& aText ) const;
170
171private:
173 std::set<wxString> m_dirty;
175};
176
181{
182 std::vector<std::pair<size_t, size_t>> matched;
183 std::vector<size_t> unmappedLeft;
184};
185
192BUS_ALIGNMENT Align( const BUS_SCHEMA& aLeft, const BUS_SCHEMA& aRight );
193} // namespace SCH_CONNECTIVITY
CACHE_TABLE< wxString, RESULT >::ENTRY ENTRY
Definition conn_bus.h:146
CACHE_TABLE< wxString, RESULT > m_cache
Definition conn_bus.h:174
std::set< wxString > m_dirty
Definition conn_bus.h:173
void Retain(const std::set< wxString > &aLive)
Erase the entries whose text is not in aLive.
Definition conn_bus.cpp:258
bool SetAliases(const BUS_ALIASES &aAliases)
Replace the alias table.
Definition conn_bus.cpp:192
std::shared_ptr< const BUS_SCHEMA::NODE > FindTree(const wxString &aText) const
Current tree without cache writes.
Definition conn_bus.cpp:273
BUS_PARSE_CACHE(CACHE_VERSIONS &aVersions)
Definition conn_bus.h:148
const ENTRY & Parse(const wxString &aText)
Return the cached entry, and parse again if the entry is missing or dirty.
Definition conn_bus.cpp:234
One sequence for all cache tables for the lifetime of an engine session.
Definition conn_cache.h:37
Value keys and the key session of the schematic connectivity engine.
std::map< wxString, std::vector< wxString > > BUS_ALIASES
Bus alias table, from alias name to member texts.
Definition conn_bus.h:40
BUS_ALIGNMENT Align(const BUS_SCHEMA &aLeft, const BUS_SCHEMA &aRight)
Match the leaves of aLeft to the leaves of aRight.
Definition conn_bus.cpp:282
Member correspondence between two bus schemas.
Definition conn_bus.h:181
std::vector< size_t > unmappedLeft
Left leaf ordinals without a right partner.
Definition conn_bus.h:183
std::vector< std::pair< size_t, size_t > > matched
Pairs of left and right leaf ordinals.
Definition conn_bus.h:182
std::shared_ptr< const BUS_SCHEMA > schema
Definition conn_bus.h:134
bool operator==(const RESULT &aOther) const
Definition conn_bus.h:138
std::shared_ptr< const BUS_SCHEMA::NODE > tree
Presentation tree.
Definition conn_bus.h:136
wxString name
Member name with all group prefixes, such as I2C.SDA.
Definition conn_bus.h:58
std::vector< wxString > groupPath
Prefixes of the inner named groups.
Definition conn_bus.h:65
wxString localName
Member name without group prefixes, such as SDA.
Definition conn_bus.h:59
bool operator==(const LEAF &) const =default
One node of the presentation tree.
Definition conn_bus.h:72
wxString text
Container label text.
Definition conn_bus.h:82
std::optional< size_t > leaf
Leaf ordinal of a NET node.
Definition conn_bus.h:86
bool operator==(const NODE &) const =default
std::vector< NODE > members
Definition conn_bus.h:84
The parsed form of one bus text.
Definition conn_bus.h:48
std::vector< LEAF > leaves
Member nets in declaration order, with nested buses flattened.
Definition conn_bus.h:98
NODE root
Presentation tree. BUS_PARSE_CACHE stores it apart from the schema.
Definition conn_bus.h:99
@ GROUP
A list such as I2C{SDA SCL}. Members align by name with another group.
Definition conn_bus.h:52
@ VECTOR
A range such as D[0..3]. Members align by ordinal.
Definition conn_bus.h:51
wxString prefix
Root prefix, such as D or I2C. It is empty for an unnamed group.
Definition conn_bus.h:92
std::set< wxString > aliasesUsed
Every member name that the parser looked up in the alias table.
Definition conn_bus.h:104
size_t prefixEnd
End of the root prefix in the escaped input text.
Definition conn_bus.h:97
bool operator==(const BUS_SCHEMA &aOther) const
Compare the electrical schema only.
Definition conn_bus.h:109
static std::optional< BUS_SCHEMA > Parse(const wxString &aText, const BUS_ALIASES &aAliases={})
Parse a vector or group bus text.
Definition conn_bus.cpp:186