KiCad PCB EDA Suite
UTIL Namespace Reference

Namespaces

 DETAIL
 

Classes

class  LINK
 
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 GetReferencePrefix (const wxString &aRefDes)
 Get the (non-numeric) prefix 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...
 

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_PCBNEW_DISPLAY_OPTIONS::TransferDataToWindow(), GAL_OPTIONS_PANEL::TransferDataToWindow(), and KIGFX::GAL_DISPLAY_OPTIONS::WriteConfig().

◆ GetReferencePrefix()

wxString UTIL::GetReferencePrefix ( const wxString &  aRefDes)

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

R1 -> R IC34 -> IC

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 character from the back
38  auto res = std::find_if( aRefDes.rbegin(), aRefDes.rend(),
39  []( wxUniChar aChr ) { return !std::isdigit( aChr ); } );
40 
41  return { aRefDes.begin(), res.base() };
42 }

Referenced by MODULE::IncrementReference().

◆ 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::ReadConfig(), PANEL_PCBNEW_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 45 of file refdes_utils.cpp.

46 {
47  // Compare unescaped text
48  wxString strFWord = UnescapeString( aFirst );
49  wxString strSWord = UnescapeString( aSecond );
50 
51  // The different sections of the two strings
52  wxString strFWordBeg, strFWordMid, strFWordEnd;
53  wxString strSWordBeg, strSWordMid, strSWordEnd;
54 
55  // Split the two strings into separate parts
56  SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
57  SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
58 
59  // Compare the Beginning section of the strings
60  int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
61 
62  if( isEqual > 0 )
63  return 1;
64  else if( isEqual < 0 )
65  return -1;
66  else
67  {
68  // If the first sections are equal compare their digits
69  long lFirstDigit = 0;
70  long lSecondDigit = 0;
71 
72  strFWordMid.ToLong( &lFirstDigit );
73  strSWordMid.ToLong( &lSecondDigit );
74 
75  if( lFirstDigit > lSecondDigit )
76  return 1;
77  else if( lFirstDigit < lSecondDigit )
78  return -1;
79  // If the first two sections are equal compare the endings
80  else
81  return strFWordEnd.CmpNoCase( strSWordEnd );
82  }
83 }
int SplitString(wxString strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Function SplitString breaks a string into three parts.
Definition: string.cpp:546
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:95

References SplitString(), and UnescapeString().

Referenced by FIELDS_EDITOR_GRID_DATA_MODEL::cmp(), FIELDS_EDITOR_GRID_DATA_MODEL::GetValue(), operator<(), SCH_REFERENCE_LIST::sortByReferenceOnly(), sortPinsByNum(), and sortPinsByNumber().