You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
9.8 KiB

6 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 CERN
  5. * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Jon Evans <jon@craftyjon.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef _SCH_CONNECTION_H
  22. #define _SCH_CONNECTION_H
  23. #include <memory>
  24. #include <unordered_set>
  25. #include <wx/regex.h>
  26. #include <bus_alias.h>
  27. #include <sch_sheet_path.h>
  28. class CONNECTION_GRAPH;
  29. class SCH_ITEM;
  30. class SCH_SHEET_PATH;
  31. class MSG_PANEL_ITEM;
  32. enum class CONNECTION_TYPE
  33. {
  34. NONE, ///< No connection to this item
  35. NET, ///< This item represents a net
  36. BUS, ///< This item represents a bus vector
  37. BUS_GROUP, ///< This item represents a bus group
  38. };
  39. /**
  40. * Each graphical item can have a SCH_CONNECTION describing its logical
  41. * connection (to a bus or net). These are generated when netlisting, or when
  42. * editing operations that can change the netlist are performed.
  43. *
  44. * In hierarchical schematics, a single SCH_ITEM object can refer to multiple
  45. * distinct parts of a design (in the case of a sub-sheet that is instanced
  46. * more than once in a higher level sheet). Because of this, a single item may
  47. * contain more than one SCH_CONNECTION -- each is specific to a sheet.
  48. *
  49. * Symbols contain connections for each of their pins (and for each sheet they
  50. * exist on) but don't use their own connection object.
  51. */
  52. class SCH_CONNECTION
  53. {
  54. public:
  55. SCH_CONNECTION( SCH_ITEM* aParent = nullptr, SCH_SHEET_PATH aPath = SCH_SHEET_PATH() );
  56. SCH_CONNECTION( CONNECTION_GRAPH* aGraph );
  57. ~SCH_CONNECTION()
  58. {}
  59. /**
  60. * Note: the equality operator for SCH_CONNECTION only tests the net
  61. * properties, not the ownership / sheet location!
  62. */
  63. bool operator==( const SCH_CONNECTION& aOther ) const;
  64. bool operator!=( const SCH_CONNECTION& aOther ) const;
  65. void SetGraph( CONNECTION_GRAPH* aGraph )
  66. {
  67. m_graph = aGraph;
  68. }
  69. /**
  70. * Configures the connection given a label.
  71. * For CONNECTION_NET, this just sets the name.
  72. * For CONNECTION_BUS, this will deduce the correct BUS_TYPE and also
  73. * generate a correct list of members.
  74. */
  75. void ConfigureFromLabel( const wxString& aLabel );
  76. /**
  77. * Clears connectivity information
  78. */
  79. void Reset();
  80. /**
  81. * Copies connectivity information (but not parent) from another connection
  82. *
  83. * @param aOther is the connection to clone
  84. */
  85. void Clone( const SCH_CONNECTION& aOther );
  86. SCH_ITEM* Parent() const { return m_parent; }
  87. SCH_ITEM* Driver() const { return m_driver; }
  88. void SetDriver( SCH_ITEM* aItem );
  89. SCH_SHEET_PATH Sheet() const { return m_sheet; }
  90. void SetSheet( SCH_SHEET_PATH aSheet );
  91. SCH_SHEET_PATH LocalSheet() const { return m_local_sheet; }
  92. /**
  93. * Checks if the SCH_ITEM this connection is attached to can drive connections
  94. * Drivers can be labels, sheet pins, or symbol pins.
  95. *
  96. * @return true if the attached items is a driver
  97. */
  98. bool IsDriver() const;
  99. bool IsBus() const
  100. {
  101. return ( m_type == CONNECTION_TYPE::BUS || m_type == CONNECTION_TYPE::BUS_GROUP );
  102. }
  103. bool IsNet() const
  104. {
  105. return ( m_type == CONNECTION_TYPE::NET );
  106. }
  107. bool IsUnconnected() const { return ( m_type == CONNECTION_TYPE::NONE ); }
  108. bool IsDirty() const { return m_dirty; }
  109. void SetDirty() { m_dirty = true; }
  110. void ClearDirty() { m_dirty = false; }
  111. bool HasDriverChanged() const;
  112. void ClearDriverChanged();
  113. void* GetLastDriver() const { return m_lastDriver; }
  114. wxString Name( bool aIgnoreSheet = false ) const;
  115. wxString LocalName() const { return m_local_name; }
  116. wxString FullLocalName() const
  117. {
  118. return m_local_prefix + m_local_name + m_suffix;
  119. }
  120. void SetName( const wxString& aName )
  121. {
  122. m_name = aName;
  123. recacheName();
  124. }
  125. wxString Prefix() const { return m_prefix; }
  126. void SetPrefix( const wxString& aPrefix );
  127. wxString BusPrefix() const { return m_bus_prefix; }
  128. wxString Suffix() const { return m_suffix; }
  129. void SetSuffix( const wxString& aSuffix );
  130. CONNECTION_TYPE Type() const { return m_type; }
  131. void SetType( CONNECTION_TYPE aType )
  132. {
  133. m_type = aType;
  134. recacheName();
  135. }
  136. int NetCode() const { return m_net_code; }
  137. void SetNetCode( int aCode ) { m_net_code = aCode; }
  138. int BusCode() const { return m_bus_code; }
  139. void SetBusCode( int aCode ) { m_bus_code = aCode; }
  140. int SubgraphCode() const { return m_subgraph_code; }
  141. void SetSubgraphCode( int aCode ) { m_subgraph_code = aCode; }
  142. long VectorStart() const { return m_vector_start; }
  143. long VectorEnd() const { return m_vector_end; }
  144. long VectorIndex() const { return m_vector_index; }
  145. wxString VectorPrefix() const { return m_vector_prefix; }
  146. std::vector< std::shared_ptr< SCH_CONNECTION > >& Members()
  147. {
  148. return m_members;
  149. }
  150. const std::vector< std::shared_ptr< SCH_CONNECTION > >& Members() const
  151. {
  152. return m_members;
  153. }
  154. const std::vector< std::shared_ptr< SCH_CONNECTION > > AllMembers() const;
  155. static wxString PrintBusForUI( const wxString& aString );
  156. /**
  157. * Returns true if this connection is contained within aOther (but not the same as aOther)
  158. * @return true if this connection is a member of aOther
  159. */
  160. bool IsSubsetOf( SCH_CONNECTION* aOther ) const;
  161. /**
  162. * Returns true if this connection is a member of bus connection aOther
  163. *
  164. * Will always return false if aOther is not a bus connection
  165. */
  166. bool IsMemberOfBus( SCH_CONNECTION* aOther ) const;
  167. /**
  168. * Adds information about the connection object to aList
  169. */
  170. void AppendInfoToMsgPanel( std::vector<MSG_PANEL_ITEM>& aList ) const;
  171. /**
  172. * Test if \a aLabel has a bus notation.
  173. *
  174. * @param aLabel A wxString object containing the label to test.
  175. * @return true if text is a bus notation format otherwise false is returned.
  176. */
  177. static bool IsBusLabel( const wxString& aLabel );
  178. /**
  179. * Test if \a aLabel looks like a bus notation.
  180. * This check is much less expensive than IsBusLabel.
  181. *
  182. * @param aLabel A wxString object containing the label to test.
  183. * @return true if text might be a bus label
  184. */
  185. static bool MightBeBusLabel( const wxString& aLabel );
  186. private:
  187. void recacheName();
  188. bool m_dirty;
  189. SCH_SHEET_PATH m_sheet; ///< The hierarchical sheet this connection is on
  190. /**
  191. * When a connection is overridden by one on a different hierarchical sheet, it will be cloned
  192. * and m_sheet will point to the parent sheet. This member stores the original sheet so that it
  193. * can be used for applications such as net highlighting to retrieve the sheet of the parent
  194. * item from the highlighted connection.
  195. */
  196. SCH_SHEET_PATH m_local_sheet;
  197. SCH_ITEM* m_parent; ///< The SCH_ITEM this connection is owned by
  198. void* m_lastDriver; ///< WEAK POINTER (there is no guarantee it is still allocated)
  199. ///< Equality comparisons are OK, but that's pretty much it
  200. SCH_ITEM* m_driver; ///< The SCH_ITEM that drives this connection's net
  201. CONNECTION_TYPE m_type; ///< @see enum CONNECTION_TYPE
  202. wxString m_name; ///< Name of the connection.
  203. wxString m_cached_name; ///< Full name, including prefix and suffix
  204. wxString m_cached_name_with_path; ///< Full name including sheet path (if not global)
  205. /**
  206. * For bus members, we want to keep track of the "local" name of a member, that is,
  207. * the name it takes on from its parent bus name. This is because we always want to use
  208. * the local name for bus unfolding, matching within buses, etc. The actual resolved name
  209. * of this bus member might change, for example if it's connected elsewhere to some other
  210. * item with higher priority.
  211. */
  212. wxString m_local_name;
  213. /// Prefix if connection is member of a labeled bus group (or "" if not)
  214. wxString m_prefix;
  215. /// Local prefix for group bus members (used with m_local_name)
  216. wxString m_local_prefix;
  217. /// Optional prefix of a bux group (always empty for nets and vector buses)
  218. wxString m_bus_prefix;
  219. wxString m_suffix; ///< Name suffix (used only for disambiguation)
  220. int m_net_code; // TODO(JE) remove if unused
  221. int m_bus_code; // TODO(JE) remove if unused
  222. int m_subgraph_code; ///< Groups directly-connected items
  223. long m_vector_index; ///< Index of bus vector member nets
  224. long m_vector_start; ///< Highest member of a vector bus
  225. long m_vector_end; ///< Lowest member of a vector bus
  226. ///< Prefix name of the vector, if m_type == CONNECTION_BUS (or "" if not)
  227. wxString m_vector_prefix;
  228. /**
  229. * For bus connections, store a list of member connections
  230. *
  231. * NOTE: All connections that Clone() others share the list of member
  232. * pointers. This seems fine at the moment.
  233. */
  234. std::vector< std::shared_ptr< SCH_CONNECTION > > m_members;
  235. /**
  236. * Pointer to the connection graph for the schematic this connection exists on.
  237. * Needed for bus alias lookups.
  238. */
  239. CONNECTION_GRAPH* m_graph;
  240. };
  241. #endif