KiCad PCB EDA Suite
UTIL Namespace Reference

A model subscriber implementation using links to represent connections. More...

Namespaces

 DETAIL
 

Classes

class  LINK
 Simple RAII-handle to a subscription. More...
 
class  OBSERVABLE
 

Typedefs

template<typename T >
using CFG_MAP = std::vector< std::pair< T, long > >
 A config value table is a list of native values (usually enums) to a different set of values, for example, the values used to represent the enum in a config file, or the index used to represent it in a selection list. More...
 
template<typename MAP >
using CFG_NATIVE_VAL = typename MAP::value_type::first_type
 The "native" type of a CFG_MAP: probably an enum type. More...
 

Functions

wxString GetRefDesPrefix (const wxString &aRefDes)
 Get the (non-numeric) prefix from a refdes - e.g. More...
 
wxString GetRefDesUnannotated (const wxString &aRefDes)
 Return an unannotated refdes from either a prefix or an existing refdes. More...
 
int GetRefDesNumber (const wxString &aRefDes)
 Get the numeric suffix from a refdes - e.g. More...
 
int RefDesStringCompare (const wxString &lhs, const wxString &rhs)
 Acts just like the strcmp function but treats numbers within the string text correctly for sorting. More...
 
template<typename MAP >
static long GetConfigForVal (const MAP &aMap, CFG_NATIVE_VAL< MAP > aVal)
 Get the mapped config value (the one to write to file, or use in an index) from the given native (probably enum) value. More...
 
template<typename MAP >
static CFG_NATIVE_VAL< MAP > GetValFromConfig (const MAP &aMap, long aConf)
 Get the native value corresponding to the config value (read from file or UI, probably) and find it in the mapping table. More...
 

Detailed Description

A model subscriber implementation using links to represent connections.

Subscribers can be removed during notification. If no observers are registered, size is the size of a shared_ptr.

Typedef Documentation

◆ CFG_MAP

template<typename T >
using UTIL::CFG_MAP = typedef std::vector<std::pair<T, long> >

A config value table is a list of native values (usually enums) to a different set of values, for example, the values used to represent the enum in a config file, or the index used to represent it in a selection list.

It can be important to decouple from the internal representation, especially in the case of persistent config files, as adding, removing or modifying the order of items internally can easily result in configs being read incorrectly, and, even if otherwise carefully managed, results in obsolete values being kept in enums as placeholders.

The first item in the list is used default if no matching value is found during lookup.

Definition at line 49 of file config_map.h.

◆ CFG_NATIVE_VAL

template<typename MAP >
using UTIL::CFG_NATIVE_VAL = typedef typename MAP::value_type::first_type

The "native" type of a CFG_MAP: probably an enum type.

Definition at line 55 of file config_map.h.

Function Documentation

◆ GetConfigForVal()

template<typename MAP >
static long UTIL::GetConfigForVal ( const MAP &  aMap,
CFG_NATIVE_VAL< MAP >  aVal 
)
static

Get the mapped config value (the one to write to file, or use in an index) from the given native (probably enum) value.

The default (first item) is returned if the value is not found in the list.

Parameters
aMapthe value-config mapping table
aValthe value to look up

Definition at line 69 of file config_map.h.

70 {
71  // default is first entry
72  long aConf = aMap[0].second;
73 
74  for( const auto& mapping : aMap )
75  {
76  if( mapping.first == aVal )
77  {
78  aConf = mapping.second;
79  break;
80  }
81  }
82 
83  return aConf;
84 }

Referenced by PANEL_DISPLAY_OPTIONS::TransferDataToWindow(), GAL_OPTIONS_PANEL::TransferDataToWindow(), and KIGFX::GAL_DISPLAY_OPTIONS::WriteConfig().

◆ GetRefDesNumber()

int UTIL::GetRefDesNumber ( const wxString &  aRefDes)

Get the numeric suffix from a refdes - e.g.

R1 -> 1 IC34 -> 34 R? -> -1

Parameters
aRefDesfull refdes
Returns
the suffix, or -1 if nothing found

Definition at line 54 of file refdes_utils.cpp.

55 {
56  int retval = -1; // negative to indicate not found
57  size_t firstnum = aRefDes.find_first_of( "0123456789" );
58 
59  if( firstnum != wxString::npos )
60  {
61  wxString candidateValue = aRefDes.Mid( firstnum );
62  long result;
63 
64  if( !candidateValue.ToLong( &result ) )
65  retval = -1;
66  else
67  retval = static_cast<int>( result );
68  }
69 
70  return retval;
71 }

Referenced by BOARD_REANNOTATE_TOOL::ReannotateDuplicatesInSelection().

◆ GetRefDesPrefix()

wxString UTIL::GetRefDesPrefix ( const wxString &  aRefDes)

Get the (non-numeric) prefix from a refdes - e.g.

R1 -> R IC34 -> IC U? -> U

Parameters
aRefDesfull refdes
Returns
the prefix, or empty string if nothing found

Definition at line 35 of file refdes_utils.cpp.

36 {
37  // find the first non-digit, non-question-mark character from the back
38  auto res = std::find_if( aRefDes.rbegin(), aRefDes.rend(),
39  []( wxUniChar aChr )
40  {
41  return aChr != '?' && !std::isdigit( aChr );
42  } );
43 
44  return { aRefDes.begin(), res.base() };
45 }

Referenced by BOOST_AUTO_TEST_CASE(), FOOTPRINT::GetNextPadName(), GetRefDesUnannotated(), FOOTPRINT::IncrementReference(), SCH_SYMBOL::IsReferenceStringValid(), BOARD_REANNOTATE_TOOL::ReannotateDuplicatesInSelection(), SCH_SYMBOL::SCH_SYMBOL(), and SCH_SYMBOL::SetRef().

◆ GetRefDesUnannotated()

wxString UTIL::GetRefDesUnannotated ( const wxString &  aRefDes)

Return an unannotated refdes from either a prefix or an existing refdes.

R -> R? IC34 -> IC? U? -> U?

Parameters
aRefDes
Returns

Definition at line 48 of file refdes_utils.cpp.

49 {
50  return UTIL::GetRefDesPrefix( aSource ) + wxT( "?" );
51 }
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.

References GetRefDesPrefix().

Referenced by SCH_SYMBOL::ClearAnnotation(), SCH_SYMBOL::GetRef(), DIALOG_CHANGE_SYMBOLS::processSymbol(), SCH_SYMBOL::SCH_SYMBOL(), SCH_SYMBOL::SetFootprint(), SCH_SYMBOL::SetUnitSelection(), and SCH_SYMBOL::SetValue().

◆ GetValFromConfig()

template<typename MAP >
static CFG_NATIVE_VAL<MAP> UTIL::GetValFromConfig ( const MAP &  aMap,
long  aConf 
)
static

Get the native value corresponding to the config value (read from file or UI, probably) and find it in the mapping table.

The default item is returned if the mapping fails.

Parameters
aMapthe value-config mapping table
aConfthe config value to look up

Definition at line 96 of file config_map.h.

97 {
98  // default is first entry
99  CFG_NATIVE_VAL<MAP> aVal = aMap[0].first;
100 
101  for( const auto& mapping : aMap )
102  {
103  if( mapping.second == aConf )
104  {
105  aVal = mapping.first;
106  break;
107  }
108  }
109 
110  return aVal;
111 }

Referenced by KIGFX::GAL_DISPLAY_OPTIONS::ReadWindowSettings(), PANEL_DISPLAY_OPTIONS::TransferDataFromWindow(), and GAL_OPTIONS_PANEL::TransferDataFromWindow().

◆ RefDesStringCompare()

int UTIL::RefDesStringCompare ( const wxString &  lhs,
const wxString &  rhs 
)

Acts just like the strcmp function but treats numbers within the string text correctly for sorting.

eg. A10 > A2 return -1 if first string is less than the second return 0 if the strings are equal return 1 if the first string is greater than the second

Definition at line 74 of file refdes_utils.cpp.

75 {
76  // Compare unescaped text
77  wxString strFWord = UnescapeString( aFirst );
78  wxString strSWord = UnescapeString( aSecond );
79 
80  // The different sections of the two strings
81  wxString strFWordBeg, strFWordMid, strFWordEnd;
82  wxString strSWordBeg, strSWordMid, strSWordEnd;
83 
84  // Split the two strings into separate parts
85  SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
86  SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
87 
88  // Compare the Beginning section of the strings
89  int isEqual = strFWordBeg.Cmp( strSWordBeg );
90 
91  if( isEqual > 0 )
92  return 1;
93  else if( isEqual < 0 )
94  return -1;
95  else
96  {
97  // If the first sections are equal compare their digits
98  long lFirstDigit = 0;
99  long lSecondDigit = 0;
100 
101  strFWordMid.ToLong( &lFirstDigit );
102  strSWordMid.ToLong( &lSecondDigit );
103 
104  if( lFirstDigit > lSecondDigit )
105  return 1;
106  else if( lFirstDigit < lSecondDigit )
107  return -1;
108  // If the first two sections are equal compare the endings
109  else
110  return strFWordEnd.Cmp( strSWordEnd );
111  }
112 }
int SplitString(const wxString &strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Break a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending.
Definition: string.cpp:758
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:222

References SplitString(), and UnescapeString().

Referenced by REGULATOR_LIST::Add(), BOOST_AUTO_TEST_CASE(), FIELDS_EDITOR_GRID_DATA_MODEL::cmp(), FIELDS_EDITOR_GRID_DATA_MODEL::GetValue(), NETLIST_EXPORTER_XML::makeSymbols(), operator<(), BOARD_REANNOTATE_TOOL::ReannotateDuplicatesInSelection(), SCH_REFERENCE_LIST::sortByReferenceOnly(), sortPinsByNum(), and sortPinsByNumber().