← Back to team overview

kicad-developers team mailing list archive

[PATCH] Separate ElectricPinType and TypeSheetLabel

 

These enums are silently treated as equivalent in the ERC, because the
values are casted to integers. This works for the most part, because the
values are similar, but this is in no way enforced, and there is a small
inconsistency (NET_UNSPECIFIED vs PIN_PASSIVE).

This turns the storage into a discriminated union, and provides an accessor
method that checks that a meaningful value can be returned and returns an
ElectricPinType in any case.
---
 eeschema/class_netlist_object.cpp | 36 ++++++++++++++++++++++++++++++++++--
 eeschema/class_netlist_object.h   |  3 +++
 eeschema/erc.cpp                  |  8 ++++----
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/eeschema/class_netlist_object.cpp b/eeschema/class_netlist_object.cpp
index 1f53224..8955f82 100644
--- a/eeschema/class_netlist_object.cpp
+++ b/eeschema/class_netlist_object.cpp
@@ -173,8 +173,6 @@ NETLIST_OBJECT::NETLIST_OBJECT()
                                      * contains this pin
                                      */
     m_Flag = 0;                     /* flag used in calculations */
-    m_ElectricalType = 0;           /* Has meaning only for Pins and hierarchical pins: electrical
-                                     * type */
     m_netCode    = 0;               /* net code for all items except BUS labels because a BUS
                                      * label has as many net codes as bus members
                                      */
@@ -202,6 +200,40 @@ NETLIST_OBJECT::~NETLIST_OBJECT()
 }
 
 
+int NETLIST_OBJECT::GetElectricalType() const
+{
+    if( m_Type == NET_PIN )
+        return m_ElectricalType;
+
+    if( IsLabelType() )
+    {
+        switch( m_ElectricalType )
+        {
+        case NET_INPUT:
+            return PIN_INPUT;
+
+        case NET_OUTPUT:
+            return PIN_OUTPUT;
+
+        case NET_BIDI:
+            return PIN_BIDI;
+
+        case NET_TRISTATE:
+            return PIN_TRISTATE;
+
+        case NET_UNSPECIFIED:
+            return PIN_UNSPECIFIED;
+        }
+
+        assert( ! "Invalid net type" );
+    }
+
+    assert( ! "No electrical type mapping defined for netlist object type" );
+
+    return PIN_NC;
+}
+
+
 // return true if the object is a label of any type
 bool NETLIST_OBJECT::IsLabelType() const
 {
diff --git a/eeschema/class_netlist_object.h b/eeschema/class_netlist_object.h
index 68dc2b8..7c9e987 100644
--- a/eeschema/class_netlist_object.h
+++ b/eeschema/class_netlist_object.h
@@ -33,6 +33,7 @@
 
 
 #include <sch_sheet_path.h>
+#include <sch_text.h>
 #include <lib_pin.h>      // LIB_PIN::PinStringNum( m_PinNum )
 #include <sch_item_struct.h>
 
@@ -160,6 +161,8 @@ public:
         return m_ConnectionType;
     }
 
+    int GetElectricalType() const;
+
     /**
      * Set m_netNameCandidate to a connected item which will
      * be used to calcule the net name of the item
diff --git a/eeschema/erc.cpp b/eeschema/erc.cpp
index 750c012..f63abba 100644
--- a/eeschema/erc.cpp
+++ b/eeschema/erc.cpp
@@ -280,7 +280,7 @@ void Diagnose( NETLIST_OBJECT* aNetItemRef, NETLIST_OBJECT* aNetItemTst,
         return;
     }
 
-    ii = aNetItemRef->m_ElectricalType;
+    ii = aNetItemRef->GetElectricalType();
 
     wxString string_pinnum, cmp_ref;
     char     ascii_buf[5];
@@ -338,7 +338,7 @@ void Diagnose( NETLIST_OBJECT* aNetItemRef, NETLIST_OBJECT* aNetItemTst,
 
     if( aNetItemTst )         /* Error between 2 pins */
     {
-        jj = aNetItemTst->m_ElectricalType;
+        jj = aNetItemTst->GetElectricalType();
         int errortype = ERCE_PIN_TO_PIN_WARNING;
 
         if( aDiag == ERR )
@@ -379,7 +379,7 @@ void TestOthersItems( NETLIST_OBJECT_LIST* aList,
     int erc = OK;
 
     /* Analysis of the table of connections. */
-    int ref_elect_type = aList->GetItem( aNetItemRef )->m_ElectricalType;
+    int ref_elect_type = aList->GetItem( aNetItemRef )->GetElectricalType();
     int local_minconn = NOC;
 
     if( ref_elect_type == PIN_NC )
@@ -477,7 +477,7 @@ void TestOthersItems( NETLIST_OBJECT_LIST* aList,
             break;
 
         case NET_PIN:
-            jj = aList->GetItem( netItemTst )->m_ElectricalType;
+            jj = aList->GetItem( netItemTst )->GetElectricalType();
             local_minconn = std::max( MinimalReq[ref_elect_type][jj], local_minconn );
 
             if( netItemTst <= aNetItemRef )

Follow ups